Skip to content

Commit feba800

Browse files
committed
fix: 修复嵌套和伪类的bug
1 parent 7c1f57b commit feba800

File tree

8 files changed

+194
-147
lines changed

8 files changed

+194
-147
lines changed

__test__/index.spec.mjs.md

Lines changed: 56 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,10 @@ Generated by [AVA](https://avajs.dev).
156156
]␊
157157
]␊
158158
},␊
159-
_backgroundSize: ImageSize.Auto,␊
159+
_backgroundSize: {␊
160+
width: "auto",␊
161+
height: "auto"␊
162+
},␊
160163
_backgroundPosition: {␊
161164
x: "0%",␊
162165
y: "0%"␊
@@ -200,7 +203,10 @@ Generated by [AVA](https://avajs.dev).
200203
_backgroundImage: {␊
201204
src: "https://img.alicdn.com/imgextra/i1/O1CN01QYQY2e1Q1QYQY2e1Q_!!6000000000000-2-tps-18-18.png"␊
202205
},␊
203-
_backgroundSize: ImageSize.Auto,␊
206+
_backgroundSize: {␊
207+
width: "auto",␊
208+
height: "auto"␊
209+
},␊
204210
_backgroundPosition: Alignment.Center,␊
205211
_backgroundRepeat: ImageRepeat.NoRepeat,␊
206212
_backgroundColor: "#FF0000",␊
@@ -343,7 +349,10 @@ Generated by [AVA](https://avajs.dev).
343349
y: "0%"␊
344350
},␊
345351
_backgroundRepeat: ImageRepeat.XY,␊
346-
_backgroundSize: ImageSize.Auto,␊
352+
_backgroundSize: {␊
353+
width: "auto",␊
354+
height: "auto"␊
355+
},␊
347356
_columnGap: convertNumber2VP(10),␊
348357
_display: "flex",␊
349358
_height: convertNumber2VP(50),␊
@@ -371,7 +380,10 @@ Generated by [AVA](https://avajs.dev).
371380
]␊
372381
]␊
373382
},␊
374-
_backgroundSize: ImageSize.Auto,␊
383+
_backgroundSize: {␊
384+
width: "auto",␊
385+
height: "auto"␊
386+
},␊
375387
_backgroundPosition: {␊
376388
x: "0%",␊
377389
y: "0%"␊
@@ -401,7 +413,10 @@ Generated by [AVA](https://avajs.dev).
401413
_backgroundImage: {␊
402414
src: "https://img.alicdn.com/imgextra/i1/O1CN01QYQY2e1Q1QYQY2e1Q_!!6000000000000-2-tps-18-18.png"␊
403415
},␊
404-
_backgroundSize: ImageSize.Auto,␊
416+
_backgroundSize: {␊
417+
width: "auto",␊
418+
height: "auto"␊
419+
},␊
405420
_backgroundPosition: Alignment.Center,␊
406421
_backgroundRepeat: ImageRepeat.NoRepeat,␊
407422
_backgroundColor: "#FF0000",␊
@@ -429,7 +444,10 @@ Generated by [AVA](https://avajs.dev).
429444
_backgroundImage: {␊
430445
src: "https://img.alicdn.com/imgextra/i1/O1CN01QYQY2e1Q1QYQY2e1Q_!!6000000000000-2-tps-18-18.png"␊
431446
},␊
432-
_backgroundSize: ImageSize.Auto,␊
447+
_backgroundSize: {␊
448+
width: "auto",␊
449+
height: "auto"␊
450+
},␊
433451
_backgroundPosition: Alignment.Center,␊
434452
_backgroundRepeat: ImageRepeat.NoRepeat,␊
435453
_backgroundColor: "#FF0000",␊
@@ -516,7 +534,10 @@ Generated by [AVA](https://avajs.dev).
516534
_backgroundImage: {␊
517535
src: "https://img.alicdn.com/imgextra/i1/O1CN01QYQY2e1Q1QYQY2e1Q_!!6000000000000-2-tps-18-18.png"␊
518536
},␊
519-
_backgroundSize: ImageSize.Auto,␊
537+
_backgroundSize: {␊
538+
width: "auto",␊
539+
height: "auto"␊
540+
},␊
520541
_backgroundPosition: Alignment.Center,␊
521542
_backgroundRepeat: ImageRepeat.NoRepeat,␊
522543
_backgroundColor: "#FF0000",␊
@@ -555,7 +576,10 @@ Generated by [AVA](https://avajs.dev).
555576
_backgroundImage: {␊
556577
src: "https://img.alicdn.com/imgextra/i1/O1CN01QYQY2e1Q1QYQY2e1Q_!!6000000000000-2-tps-18-18.png"␊
557578
},␊
558-
_backgroundSize: ImageSize.Auto,␊
579+
_backgroundSize: {␊
580+
width: "auto",␊
581+
height: "auto"␊
582+
},␊
559583
_backgroundPosition: Alignment.Center,␊
560584
_backgroundRepeat: ImageRepeat.NoRepeat,␊
561585
_backgroundColor: "#FF0000",␊
@@ -597,7 +621,10 @@ Generated by [AVA](https://avajs.dev).
597621
_backgroundImage: {␊
598622
src: "https://img.alicdn.com/imgextra/i1/O1CN01QYQY2e1Q1QYQY2e1Q_!!6000000000000-2-tps-18-18.png"␊
599623
},␊
600-
_backgroundSize: ImageSize.Auto,␊
624+
_backgroundSize: {␊
625+
width: "auto",␊
626+
height: "auto"␊
627+
},␊
601628
_backgroundPosition: Alignment.Center,␊
602629
_backgroundRepeat: ImageRepeat.NoRepeat,␊
603630
_backgroundColor: "#FF0000",␊
@@ -692,7 +719,10 @@ Generated by [AVA](https://avajs.dev).
692719
_backgroundImage: {␊
693720
src: "https://img.alicdn.com/imgextra/i1/O1CN01QYQY2e1Q1QYQY2e1Q_!!6000000000000-2-tps-18-18.png"␊
694721
},␊
695-
_backgroundSize: ImageSize.Auto,␊
722+
_backgroundSize: {␊
723+
width: "auto",␊
724+
height: "auto"␊
725+
},␊
696726
_backgroundPosition: Alignment.Center,␊
697727
_backgroundRepeat: ImageRepeat.NoRepeat,␊
698728
_backgroundColor: "#FF0000",␊
@@ -729,7 +759,10 @@ Generated by [AVA](https://avajs.dev).
729759
y: "0%"␊
730760
},␊
731761
_backgroundRepeat: ImageRepeat.XY,␊
732-
_backgroundSize: ImageSize.Auto,␊
762+
_backgroundSize: {␊
763+
width: "auto",␊
764+
height: "auto"␊
765+
},␊
733766
_columnGap: convertNumber2VP(10),␊
734767
_display: "flex",␊
735768
_height: convertNumber2VP(50),␊
@@ -755,7 +788,10 @@ Generated by [AVA](https://avajs.dev).
755788
]␊
756789
]␊
757790
},␊
758-
_backgroundSize: ImageSize.Auto,␊
791+
_backgroundSize: {␊
792+
width: "auto",␊
793+
height: "auto"␊
794+
},␊
759795
_backgroundPosition: {␊
760796
x: "0%",␊
761797
y: "0%"␊
@@ -783,7 +819,10 @@ Generated by [AVA](https://avajs.dev).
783819
_backgroundImage: {␊
784820
src: "https://img.alicdn.com/imgextra/i1/O1CN01QYQY2e1Q1QYQY2e1Q_!!6000000000000-2-tps-18-18.png"␊
785821
},␊
786-
_backgroundSize: ImageSize.Auto,␊
822+
_backgroundSize: {␊
823+
width: "auto",␊
824+
height: "auto"␊
825+
},␊
787826
_backgroundPosition: Alignment.Center,␊
788827
_backgroundRepeat: ImageRepeat.NoRepeat,␊
789828
_backgroundColor: "#FF0000",␊
@@ -810,7 +849,10 @@ Generated by [AVA](https://avajs.dev).
810849
_backgroundImage: {␊
811850
src: "https://img.alicdn.com/imgextra/i1/O1CN01QYQY2e1Q1QYQY2e1Q_!!6000000000000-2-tps-18-18.png"␊
812851
},␊
813-
_backgroundSize: ImageSize.Auto,␊
852+
_backgroundSize: {␊
853+
width: "auto",␊
854+
height: "auto"␊
855+
},␊
814856
_backgroundPosition: Alignment.Center,␊
815857
_backgroundRepeat: ImageRepeat.NoRepeat,␊
816858
_backgroundColor: "#FF0000",␊

__test__/index.spec.mjs.snap

32 Bytes
Binary file not shown.

src/parse_style_properties.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,15 @@ pub fn parse_style_properties(properties: &Vec<(String, Property)>) -> Vec<Style
164164
"background" => {
165165
final_properties.push(StyleValueType::Background(Background::from((id.to_string(), value))));
166166
}
167+
"content" => {
168+
// 判断content内容是否是空字符串
169+
let content_value = value.value_to_css_string(PrinterOptions::default()).unwrap().trim().to_string();
170+
if content_value != "\"\"" {
171+
// 替换字符串,将左右两边的"干掉
172+
let content_value = content_value.trim_matches('"');
173+
final_properties.push(StyleValueType::Normal(Normal::new(id.to_string(), content_value.to_string())));
174+
}
175+
}
167176
_ => {
168177
// position、zIndex等... 会自动处理 单位、数字等相关信息
169178
final_properties.push(StyleValueType::Normal(Normal::new(id.to_string(), value.value_to_css_string(PrinterOptions::default()).unwrap())));

src/style_propetries/background_size.rs

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use swc_ecma_ast::{
1111
PropOrSpread,
1212
};
1313

14-
use crate::generate_invalid_expr;
14+
use crate::{generate_expr_by_length_percentage_or_auto, generate_invalid_expr};
1515

1616
use super::{traits::ToExpr, unit::{generate_expr_with_css_input, Platform, PropertyTuple}};
1717

@@ -20,24 +20,28 @@ pub fn parse_background_size_item(size_item: &LNBackgroundSize) -> Option<ImageS
2020
LNBackgroundSize::Contain => Some(ImageSize::Contain),
2121
LNBackgroundSize::Cover => Some(ImageSize::Cover),
2222
LNBackgroundSize::Explicit { width, height } => {
23-
match width {
24-
LengthPercentageOrAuto::Auto => match height {
25-
LengthPercentageOrAuto::Auto => Some(ImageSize::Auto),
26-
_ => None,
27-
},
28-
LengthPercentageOrAuto::LengthPercentage(x) => {
29-
let x_str = x.to_css_string(PrinterOptions::default()).unwrap();
30-
match height {
31-
LengthPercentageOrAuto::LengthPercentage(y) => {
32-
let y_str = y.to_css_string(PrinterOptions::default()).unwrap();
33-
Some(ImageSize::ImageSizeWH(x_str, Some(y_str)))
34-
},
35-
LengthPercentageOrAuto::Auto => {
36-
Some(ImageSize::ImageSizeWH(x_str, None))
37-
}
38-
}
39-
},
40-
}
23+
Some(ImageSize::ImageSizeWH(
24+
width.clone(),
25+
height.clone()
26+
))
27+
// match width {
28+
// LengthPercentageOrAuto::Auto => match height {
29+
// LengthPercentageOrAuto::Auto => Some(ImageSize::Auto),
30+
// _ => None,
31+
// },
32+
// LengthPercentageOrAuto::LengthPercentage(x) => {
33+
// let x_str = x.to_css_string(PrinterOptions::default()).unwrap();
34+
// match height {
35+
// LengthPercentageOrAuto::LengthPercentage(y) => {
36+
// let y_str = y.to_css_string(PrinterOptions::default()).unwrap();
37+
// Some(ImageSize::ImageSizeWH(x_str, Some(y_str)))
38+
// },
39+
// LengthPercentageOrAuto::Auto => {
40+
// Some(ImageSize::ImageSizeWH(x_str, None))
41+
// }
42+
// }
43+
// },
44+
// }
4145
}
4246
}
4347
}
@@ -59,7 +63,7 @@ pub enum ImageSize {
5963
Cover,
6064
Contain,
6165
Auto,
62-
ImageSizeWH(String, Option<String>),
66+
ImageSizeWH(LengthPercentageOrAuto, LengthPercentageOrAuto),
6367
}
6468

6569
#[derive(Debug, Clone)]
@@ -102,22 +106,17 @@ impl ToExpr for BackgroundSize {
102106
})
103107
.into(),
104108
ImageSize::ImageSizeWH(width, height) => {
105-
let width_str = width.to_string();
106-
let height_str = height.as_ref().map(|h| h.to_string());
107-
108-
let mut props = vec![
109+
110+
let props = vec![
109111
PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp {
110112
key: PropName::Ident(Ident::new("width".into(), DUMMY_SP)),
111-
value: generate_expr_with_css_input(width_str, Platform::Harmony).into(),
112-
})))
113-
];
114-
115-
if let Some(height_str) = height_str {
116-
props.push(PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp {
113+
value: Box::new(generate_expr_by_length_percentage_or_auto!(width, Platform::Harmony))
114+
}))),
115+
PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp {
117116
key: PropName::Ident(Ident::new("height".into(), DUMMY_SP)),
118-
value: generate_expr_with_css_input(height_str, Platform::Harmony).into(),
119-
}))))
120-
}
117+
value: Box::new(generate_expr_by_length_percentage_or_auto!(height, Platform::Harmony))
118+
}))),
119+
];
121120

122121
Expr::Object(ObjectLit {
123122
span: DUMMY_SP,

src/style_propetries/display.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use lightningcss::properties::{Property, display::{Display::{Keyword, Pair}, DisplayKeyword, DisplayInside}};
44

5-
use crate::generate_expr_lit_str;
5+
use crate::{generate_expr_lit_str, generate_invalid_expr};
66

77
use super::{traits::ToExpr, unit::PropertyTuple};
88

@@ -54,7 +54,7 @@ impl ToExpr for Display {
5454
match &self.value {
5555
EnumValue::None => generate_expr_lit_str!("none"),
5656
EnumValue::Flex => generate_expr_lit_str!("flex"),
57-
EnumValue::Invalid => generate_expr_lit_str!("flex"),
57+
EnumValue::Invalid => generate_invalid_expr!(),
5858
}
5959
)
6060
}
@@ -65,7 +65,7 @@ impl ToExpr for Display {
6565
match &self.value {
6666
EnumValue::None => generate_expr_lit_str!("none"),
6767
EnumValue::Flex => generate_expr_lit_str!("flex"),
68-
EnumValue::Invalid => generate_expr_lit_str!("flex"),
68+
EnumValue::Invalid => generate_invalid_expr!(),
6969
}
7070
)
7171
}

src/style_propetries/macros.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,12 @@ macro_rules! generate_expr_by_length {
5656
#[macro_export]
5757
macro_rules! generate_expr_by_length_percentage_or_auto {
5858
($var:expr, $platform:expr) => {{
59-
59+
use $crate::{generate_expr_by_length_percentage, generate_expr_lit_str};
6060
match $var {
6161
LengthPercentageOrAuto::LengthPercentage(length_percent) => {
6262
generate_expr_by_length_percentage!(length_percent, $platform)
6363
},
64-
LengthPercentageOrAuto::Auto => generate_invalid_expr!(),
64+
LengthPercentageOrAuto::Auto => generate_expr_lit_str!("auto")
6565
}
6666
}};
6767
}
@@ -70,7 +70,7 @@ macro_rules! generate_expr_by_length_percentage_or_auto {
7070
#[macro_export]
7171
macro_rules! generate_expr_by_length_percentage {
7272
($var:expr, $platform:expr) => {{
73-
use $crate::style_propetries::unit::{generate_expr_by_length_value};
73+
use $crate::{generate_expr_lit_str, style_propetries::unit::{generate_expr_by_length_value} };
7474
use lightningcss::traits::ToCss;
7575

7676
match $var {

src/style_propetries/transform_properties/translate.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use swc_atoms::Atom;
99
use swc_common::DUMMY_SP;
1010
use swc_ecma_ast::{Expr, Ident, KeyValueProp, Lit, ObjectLit, Prop, PropName, PropOrSpread};
1111

12-
use crate::{style_propetries::unit::{generate_expr_by_length_value, Platform}, utils::to_camel_case};
12+
use crate::{generate_expr_by_length_percentage, style_propetries::unit::{generate_expr_by_length_value, Platform}, utils::to_camel_case};
1313

1414

1515
#[derive(Debug, Clone)]
@@ -35,17 +35,12 @@ impl Translate {
3535

3636
[("x", &self.x), ("y", &self.y)].into_iter().for_each(|item| {
3737
if let (name, Some(side)) = item.borrow() {
38-
match &side {
39-
DimensionPercentage::Dimension(value) => {
40-
props.push(
41-
PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp {
42-
key: PropName::Ident(Ident::new(Atom::new(*name), DUMMY_SP)),
43-
value: Box::new(generate_expr_by_length_value(value, Platform::Harmony))
44-
})))
45-
);
46-
},
47-
_ => {}
48-
}
38+
props.push(
39+
PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp {
40+
key: PropName::Ident(Ident::new(Atom::new(*name), DUMMY_SP)),
41+
value: Box::new(generate_expr_by_length_percentage!(side, Platform::Harmony))
42+
})))
43+
);
4944
}
5045
});
5146

0 commit comments

Comments
 (0)