Skip to content

Commit 0f3648b

Browse files
committed
feat: 支持多类选择器、修复rgba等小问题
1 parent 48879a4 commit 0f3648b

19 files changed

+740
-1525
lines changed

__test__/index.spec.mjs.md

Lines changed: 402 additions & 1055 deletions
Large diffs are not rendered by default.

__test__/index.spec.mjs.snap

-824 Bytes
Binary file not shown.

src/constants.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
pub const CONVERT_STYLE_PREFIX: &'static str = "_";
22
pub const CONVERT_STYLE_PX_FN: &'static str = "convertNumber2VP";
3+
pub const HM_STYLE: &'static str = "__hmStyle";
34
pub const INNER_STYLE: &'static str = "__inner_style__";
45
pub const INNER_STYLE_DATA: &'static str = "__inner_style_data__";
56
pub const NESTING_STYLE: &'static str = "__nesting_style__";
67
pub const COMBINE_NESTING_STYLE: &'static str = "__combine_nesting_style__";
78
pub const NESTINT_STYLE_DATA: &'static str = "__nesting_style_data__";
89
pub const CALC_DYMAMIC_STYLE: &'static str = "calcDynamicStyle";
10+
pub const CALC_STATIC_STYLE: &'static str = "calcStaticStyle";
11+
912

1013
pub const RN_CONVERT_STYLE_PX_FN: &'static str = "scalePx2dp";
1114
pub const RN_CONVERT_STYLE_VU_FN: &'static str = "scaleVu2dp";

src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ pub fn parse(component: String, styles: Vec<String>, options: ParseOptions) -> S
6565
let mut style_write = StyleWrite::new(
6666
program.clone(),
6767
jsx_record.clone(),
68-
style_data.style_record.clone(),
6968
style_data.pesudo_style_record.clone(),
7069
style_data.all_style.clone(),
7170
is_enable_nesting,

src/style_parser.rs

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use super::parse_style_properties::parse_style_properties;
99
pub type StyleValue = Vec<StyleValueType>;
1010

1111
pub struct StyleData<'i> {
12-
pub style_record: Rc<RefCell<HashMap<SpanKey, Vec<(String, Property<'i>)>>>>,
1312
pub pesudo_style_record: Rc<RefCell<HashMap<SpanKey, Vec<(String, Vec<(String, Property<'i>)>)>>>>,
1413
pub all_style: Rc<RefCell<HashMap<String, StyleValue>>>,
1514
pub has_nesting: bool
@@ -46,7 +45,7 @@ impl<'i> Visitor<'i> for StyleVisitor<'i> {
4645
let selectors_str = style.selectors.to_string();
4746
let selectors: Vec<&str> = selectors_str.split(",").collect::<Vec<&str>>();
4847
for index in 0..selectors.len() {
49-
let selector = selectors[index].trim().replace(".", "");
48+
let selector = selectors[index].trim().to_string();
5049
let mut all_style = self.all_style.borrow_mut();
5150
let decorations = all_style.iter_mut().find(|(id, _)| id == &selector);
5251
if let Some((_, declarations)) = decorations {
@@ -118,7 +117,7 @@ impl<'i> StyleParser<'i> {
118117
})
119118
.collect::<Vec<(_, _)>>(); // Specify the lifetime of the tuple elements to match the input data
120119
// 判断是否含有嵌套选择器
121-
if selector.contains(" ") {
120+
if selector.contains(" ") || selector.chars().filter(|&c| c == '.').count() > 1 {
122121
has_nesting = true
123122
}
124123
(selector.to_owned(), properties)
@@ -168,35 +167,9 @@ impl<'i> StyleParser<'i> {
168167
})
169168
.collect::<HashMap<_, _>>();
170169

171-
172-
let final_style_record = style_record
173-
.iter_mut()
174-
.map(|(selector, style_value)| {
175-
(
176-
selector.to_owned(),
177-
style_value
178-
.iter_mut()
179-
.reduce(|a, b| {
180-
for (key, value) in b.iter() {
181-
let has_property_index = a.iter().position(|property| property.0 == key.to_owned());
182-
if let Some(index) = has_property_index {
183-
a[index] = (key.to_owned(), value.clone());
184-
} else {
185-
a.push((key.to_owned(), value.clone()));
186-
}
187-
}
188-
a
189-
})
190-
.unwrap()
191-
.to_owned()
192-
)
193-
})
194-
.collect::<HashMap<_, _>>();
195-
196-
let final_pesudo_style_record = pesudo_style_record;
170+
let final_pesudo_style_record = pesudo_style_record;
197171

198172
StyleData {
199-
style_record: Rc::new(RefCell::new(final_style_record)),
200173
pesudo_style_record: Rc::new(RefCell::new(final_pesudo_style_record)),
201174
all_style: Rc::new(RefCell::new(final_all_style)),
202175
has_nesting

src/style_propetries/background_image.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use swc_ecma_ast::{
2222

2323
use crate::generate_invalid_expr;
2424

25-
use super::{linear_gradient::{LinearGradientDirection, LinearGradientItem}, traits::ToExpr, unit::PropertyTuple};
25+
use super::{linear_gradient::{LinearGradientDirection, LinearGradientItem}, traits::ToExpr, unit::{convert_color_keywords_to_hex, PropertyTuple}};
2626

2727
pub fn parse_background_image_item(image: &Image) -> Option<BackgroundImageKind> {
2828
match image {
@@ -40,7 +40,7 @@ pub fn parse_background_image_item(image: &Image) -> Option<BackgroundImageKind>
4040
.clone()
4141
.unwrap_or(DimensionPercentage::Dimension(LengthValue::Px(0.0)));
4242
color_stops.push((
43-
color_stop
43+
convert_color_keywords_to_hex(color_stop
4444
.color
4545
.to_css_string(PrinterOptions {
4646
minify: false,
@@ -50,7 +50,7 @@ pub fn parse_background_image_item(image: &Image) -> Option<BackgroundImageKind>
5050
},
5151
..PrinterOptions::default()
5252
})
53-
.unwrap(),
53+
.unwrap()),
5454
match &color_stop_position {
5555
DimensionPercentage::Dimension(length) => {
5656
length.to_css_string(PrinterOptions::default()).unwrap()

src/style_propetries/linear_gradient.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ impl LinearGradientItem {
4848
expr: Expr::Array(ArrayLit {
4949
span: DUMMY_SP,
5050
elems: vec![
51-
Some(Expr::Lit(Lit::Str(Str::from(fix_rgba(&item.0)))).into()),
51+
Some(Expr::Lit(Lit::Str(Str::from(fix_rgba(item.0.clone())))).into()),
5252
Some(Expr::Lit(Lit::Str(Str::from(item.1.to_string()))).into()),
5353
],
5454
})

src/style_propetries/macros.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ macro_rules! generate_expr_based_on_platform {
116116
#[macro_export]
117117
macro_rules! generate_color_property {
118118
($class:ident, $( $property_name:ident ), *) => {
119+
use $crate::utils::fix_rgba;
120+
119121
#[derive(Debug, Clone)]
120122
pub struct $class {
121123
pub id: String,
@@ -126,7 +128,7 @@ macro_rules! generate_color_property {
126128
fn to_expr(&self) -> PropertyTuple {
127129
PropertyTuple::One(
128130
self.id.clone(),
129-
swc_ecma_ast::Expr::Lit(swc_ecma_ast::Lit::Str(self.value.clone().into())).into()
131+
swc_ecma_ast::Expr::Lit(swc_ecma_ast::Lit::Str(fix_rgba(self.value.clone()).into())).into()
130132
)
131133
}
132134
fn to_rn_expr(&self) -> PropertyTuple {

src/style_propetries/text_decoration.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use swc_ecma_ast::{Expr, Ident, KeyValueProp, MemberExpr, MemberProp, ObjectLit,
44

55
use crate::{style_propetries::traits::ToExpr, generate_invalid_expr, generate_expr_lit_str, generate_prop_name};
66

7-
use super::unit::PropertyTuple;
7+
use super::unit::{convert_color_keywords_to_hex, PropertyTuple};
88

99

1010
#[derive(Debug, Clone)]
@@ -142,7 +142,7 @@ impl From<(String, &Property<'_>)> for TextDecoration {
142142
if c == "currentColor" {
143143
color = None
144144
} else {
145-
color = Some(TextDecorationColor(c));
145+
color = Some(TextDecorationColor(convert_color_keywords_to_hex(c)));
146146
}
147147
} else {
148148
color = None
@@ -198,7 +198,7 @@ impl From<(String, &Property<'_>)> for TextDecoration {
198198
if c == "currentColor" {
199199
color = None
200200
} else {
201-
color = Some(TextDecorationColor(c));
201+
color = Some(TextDecorationColor(convert_color_keywords_to_hex(c)));
202202
}
203203
} else {
204204
color = None

src/style_propetries/transform.rs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::vec;
22

33
use lightningcss::properties::{transform::Transform as LNTransform, Property};
4-
use swc_ecma_ast::{ArrayLit, Expr, ExprOrSpread};
4+
use swc_ecma_ast::{ArrayLit, Expr, ExprOrSpread, ObjectLit};
55

66
use crate::style_propetries::traits::ToExpr;
77

@@ -29,32 +29,25 @@ impl ToExpr for Transform {
2929
self.value.iter().for_each(|item| {
3030
match item {
3131
Matrix4::Translates(value) => {
32-
props.extend(value.to_expr());
32+
props.push(value.to_expr());
3333
},
3434
Matrix4::Rotates(value) => {
35-
props.extend(value.to_expr());
35+
props.push(value.to_expr());
3636
}
3737
Matrix4::Scales(value) => {
38-
props.extend(value.to_expr());
38+
props.push(value.to_expr());
3939
}
40-
Matrix4::Matrix(value) => {
41-
props.extend(value.to_expr());
42-
},
40+
// Matrix4::Matrix(value) => {
41+
// props.extend(value.to_expr());
42+
// },
4343
_ => {}
4444
}
4545
});
4646
PropertyTuple::One(
4747
"transform".to_string(),
48-
Expr::Array(ArrayLit {
48+
Expr::Object(ObjectLit {
4949
span: Default::default(),
50-
elems: props.into_iter().map(Some).map(
51-
|item| {
52-
Some(ExprOrSpread {
53-
spread: None,
54-
expr: Box::new(item.unwrap()),
55-
})
56-
}
57-
).collect::<Vec<_>>(),
50+
props:props,
5851
})
5952
)
6053
}

0 commit comments

Comments
 (0)