Skip to content

Commit b1a5410

Browse files
authored
Merge pull request #25 from NervJS/feat/flat-structure
Feat/flat structure
2 parents 228c492 + d9d38e3 commit b1a5410

27 files changed

+1608
-1943
lines changed

__test__/index.spec.mjs.md

Lines changed: 590 additions & 669 deletions
Large diffs are not rendered by default.

__test__/index.spec.mjs.snap

-64 Bytes
Binary file not shown.

src/style_parser.rs

Lines changed: 273 additions & 428 deletions
Large diffs are not rendered by default.

src/style_transform/background/background.rs

Lines changed: 32 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,20 @@ use lightningcss::{
66
values::color::CssColor,
77
};
88
use smallvec::SmallVec;
9-
use swc_common::DUMMY_SP;
10-
use swc_ecma_ast::{Expr, Ident, KeyValueProp, ObjectLit, Prop, PropName, PropOrSpread};
11-
12-
use crate::style_transform::traits::ToExpr;
139

1410
use super::{
15-
background_color::BackgroundColor,
1611
background_image::{parse_background_image_item, BackgroundImage},
17-
background_position::{parse_background_position_item, BackgroundImagePosition},
18-
background_size::{parse_background_size_item, BackgroundImageSize},
12+
background_position::{parse_background_position_item, BackgroundPosition},
13+
background_size::{parse_background_size_item, BackgroundSize}, background_color::BackgroundColor, background_repeat::{parse_background_repeat_item, BackgroundRepeat},
1914
};
2015

2116
fn parse_background(background: &SmallVec<[LNBackground<'_>; 1]>) -> Background {
2217
let mut background_image = vec![];
2318
let mut background_position = vec![];
2419
let mut background_size = vec![];
2520
let mut background_color = None;
21+
let mut background_repeat = vec![];
22+
2623
for item in background.iter() {
2724
if let Some(image) = parse_background_image_item(&item.image, &item.repeat) {
2825
background_image.push(image);
@@ -31,6 +28,7 @@ fn parse_background(background: &SmallVec<[LNBackground<'_>; 1]>) -> Background
3128
if let Some(size) = parse_background_size_item(&item.size) {
3229
background_size.push(size);
3330
}
31+
background_repeat.push(parse_background_repeat_item(&item.repeat));
3432
if item.color != CssColor::default() {
3533
background_color = Some(
3634
item
@@ -47,75 +45,49 @@ fn parse_background(background: &SmallVec<[LNBackground<'_>; 1]>) -> Background
4745
);
4846
}
4947
}
50-
Background {
51-
image: BackgroundImage(background_image),
52-
position: BackgroundImagePosition(background_position),
53-
size: BackgroundImageSize(background_size),
54-
color: BackgroundColor(background_color.unwrap_or("".to_string())),
48+
let mut bg = Background::new();
49+
if background_image.len() > 0 {
50+
bg.image = Some(BackgroundImage(background_image));
51+
}
52+
if background_position.len() > 0 {
53+
bg.position = Some(BackgroundPosition(background_position));
54+
}
55+
if background_size.len() > 0 {
56+
bg.size = Some(BackgroundSize(background_size));
5557
}
58+
if background_repeat.len() > 0 {
59+
bg.repeat = Some(BackgroundRepeat(background_repeat));
60+
}
61+
if let Some(color) = background_color {
62+
bg.color = Some(BackgroundColor(color));
63+
}
64+
bg
5665
}
5766

5867
#[derive(Debug, Clone)]
5968
pub struct Background {
60-
pub image: BackgroundImage,
61-
pub color: BackgroundColor,
62-
pub size: BackgroundImageSize,
63-
pub position: BackgroundImagePosition,
69+
pub image: Option<BackgroundImage>,
70+
pub color: Option<BackgroundColor>,
71+
pub size: Option<BackgroundSize>,
72+
pub position: Option<BackgroundPosition>,
73+
pub repeat: Option<BackgroundRepeat>
6474
}
6575

6676
impl Background {
6777
pub fn new() -> Self {
6878
Background {
69-
image: BackgroundImage(vec![]),
70-
color: BackgroundColor("".to_string()),
71-
size: BackgroundImageSize(vec![]),
72-
position: BackgroundImagePosition(vec![]),
73-
}
74-
}
75-
}
76-
77-
impl ToExpr for Background {
78-
fn to_expr(&self) -> Expr {
79-
let mut arr = vec![];
80-
if self.image.0.len() > 0 {
81-
arr.push(PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp {
82-
key: PropName::Ident(Ident::new("image".into(), DUMMY_SP)),
83-
value: self.image.to_expr().into(),
84-
}))))
85-
}
86-
if self.color.to_string() != "" {
87-
arr.push(PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp {
88-
key: PropName::Ident(Ident::new("color".into(), DUMMY_SP)),
89-
value: self.color.to_expr().into(),
90-
}))))
91-
}
92-
if self.size.0.len() > 0 {
93-
arr.push(PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp {
94-
key: PropName::Ident(Ident::new("size".into(), DUMMY_SP)),
95-
value: self.size.to_expr().into(),
96-
}))))
97-
}
98-
if self.position.0.len() > 0 {
99-
arr.push(PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp {
100-
key: PropName::Ident(Ident::new("position".into(), DUMMY_SP)),
101-
value: self.position.to_expr().into(),
102-
}))))
79+
image: None,
80+
color: None,
81+
size: None,
82+
position: None,
83+
repeat: None
10384
}
104-
Expr::Object(ObjectLit {
105-
span: DUMMY_SP,
106-
props: arr.into(),
107-
})
10885
}
10986
}
11087

11188
impl From<&Property<'_>> for Background {
11289
fn from(value: &Property<'_>) -> Self {
113-
let mut background = Background {
114-
image: BackgroundImage(vec![]),
115-
color: BackgroundColor("".to_string()),
116-
size: BackgroundImageSize(vec![]),
117-
position: BackgroundImagePosition(vec![]),
118-
};
90+
let mut background = Background::new();
11991
if let Property::Background(value) = value {
12092
background = parse_background(&value);
12193
}

src/style_transform/background/background_image.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -200,23 +200,15 @@ impl ToExpr for BackgroundImage {
200200
.0
201201
.iter()
202202
.enumerate()
203-
.map(|(index, item)| match &item.image {
203+
.map(|(_, item)| match &item.image {
204204
BackgroundImageKind::String(src) => Some(
205205
Expr::Object(ObjectLit {
206206
span: DUMMY_SP,
207207
props: vec![
208208
PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp {
209209
key: PropName::Ident(Ident::new("src".into(), DUMMY_SP)),
210210
value: Expr::Lit(Lit::Str(Str::from(src.to_string()))).into(),
211-
}))),
212-
PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp {
213-
key: PropName::Ident(Ident::new("repeat".into(), DUMMY_SP)),
214-
value: if let Some(repeat) = &self.0[index].repeat {
215-
repeat.to_expr().into()
216-
} else {
217-
Expr::Lit(Lit::Str(Str::from("NoRepeat"))).into()
218-
},
219-
}))),
211+
})))
220212
]
221213
.into(),
222214
})
@@ -250,4 +242,4 @@ impl From<(&Property<'_>, Option<&Property<'_>>)> for BackgroundImage {
250242
}
251243
background_image_res
252244
}
253-
}
245+
}

src/style_transform/background/background_position.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use lightningcss::{
2-
properties::{background::BackgroundPosition, Property},
2+
properties::{background::BackgroundPosition as LNBackgroundPosition, Property},
33
stylesheet::PrinterOptions,
44
traits::ToCss,
55
values::position::{
@@ -17,7 +17,7 @@ use swc_ecma_ast::{
1717

1818
use crate::{style_transform::traits::ToExpr, utils::convert_px_to_units};
1919

20-
pub fn parse_background_position_item(position: &BackgroundPosition) -> ImagePosition {
20+
pub fn parse_background_position_item(position: &LNBackgroundPosition) -> ImagePosition {
2121
match &position.x {
2222
Center => match &position.y {
2323
Center => ImagePosition::Center,
@@ -94,13 +94,13 @@ pub fn parse_background_position_item(position: &BackgroundPosition) -> ImagePos
9494
}
9595

9696
pub fn parse_background_position(
97-
position: &SmallVec<[BackgroundPosition; 1]>,
98-
) -> BackgroundImagePosition {
97+
position: &SmallVec<[LNBackgroundPosition; 1]>,
98+
) -> BackgroundPosition {
9999
let mut background_position = vec![];
100100
for item in position {
101101
background_position.push(parse_background_position_item(item));
102102
}
103-
BackgroundImagePosition(background_position)
103+
BackgroundPosition(background_position)
104104
}
105105

106106
#[derive(Debug, Clone)]
@@ -118,9 +118,9 @@ pub enum ImagePosition {
118118
}
119119

120120
#[derive(Debug, Clone)]
121-
pub struct BackgroundImagePosition(pub Vec<ImagePosition>);
121+
pub struct BackgroundPosition(pub Vec<ImagePosition>);
122122

123-
impl ToExpr for BackgroundImagePosition {
123+
impl ToExpr for BackgroundPosition {
124124
fn to_expr(&self) -> Expr {
125125
Expr::Array(ArrayLit {
126126
span: DUMMY_SP,
@@ -241,9 +241,9 @@ impl ToExpr for BackgroundImagePosition {
241241
}
242242
}
243243

244-
impl From<&Property<'_>> for BackgroundImagePosition {
244+
impl From<&Property<'_>> for BackgroundPosition {
245245
fn from(value: &Property<'_>) -> Self {
246-
let mut background_image_position = BackgroundImagePosition(vec![]);
246+
let mut background_image_position = BackgroundPosition(vec![]);
247247
match value {
248248
Property::BackgroundPosition(value) => {
249249
background_image_position = parse_background_position(&value);

src/style_transform/background/background_repeat.rs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
11
use lightningcss::properties::{
2-
background::{BackgroundRepeat, BackgroundRepeatKeyword},
2+
background::{BackgroundRepeat as LNBackgroundRepeat, BackgroundRepeatKeyword},
33
Property,
44
};
55
use swc_common::DUMMY_SP;
66
use swc_ecma_ast::{ArrayLit, Expr, Ident, MemberExpr, MemberProp};
77

88
use crate::style_transform::traits::ToExpr;
99

10+
pub fn parse_background_repeat_item(value: &LNBackgroundRepeat) -> ImageRepeatItem {
11+
if value.x == BackgroundRepeatKeyword::Repeat && value.y == BackgroundRepeatKeyword::Repeat {
12+
ImageRepeatItem::XY
13+
} else if value.x == BackgroundRepeatKeyword::Repeat {
14+
ImageRepeatItem::X
15+
} else if value.y == BackgroundRepeatKeyword::Repeat {
16+
ImageRepeatItem::Y
17+
} else {
18+
ImageRepeatItem::NoRepeat
19+
}
20+
}
21+
1022
#[derive(Debug, Clone)]
1123
pub enum ImageRepeatItem {
1224
XY,
@@ -15,17 +27,9 @@ pub enum ImageRepeatItem {
1527
NoRepeat,
1628
}
1729

18-
impl From<&BackgroundRepeat> for ImageRepeatItem {
19-
fn from(value: &BackgroundRepeat) -> Self {
20-
if value.x == BackgroundRepeatKeyword::Repeat && value.y == BackgroundRepeatKeyword::Repeat {
21-
Self::XY
22-
} else if value.x == BackgroundRepeatKeyword::Repeat {
23-
Self::X
24-
} else if value.y == BackgroundRepeatKeyword::Repeat {
25-
Self::Y
26-
} else {
27-
Self::NoRepeat
28-
}
30+
impl From<&LNBackgroundRepeat> for ImageRepeatItem {
31+
fn from(value: &LNBackgroundRepeat) -> Self {
32+
parse_background_repeat_item(value)
2933
}
3034
}
3135

@@ -49,9 +53,9 @@ impl ToExpr for ImageRepeatItem {
4953
}
5054

5155
#[derive(Debug, Clone)]
52-
pub struct ImageRepeat(pub Vec<ImageRepeatItem>);
56+
pub struct BackgroundRepeat(pub Vec<ImageRepeatItem>);
5357

54-
impl From<&Property<'_>> for ImageRepeat {
58+
impl From<&Property<'_>> for BackgroundRepeat {
5559
fn from(value: &Property<'_>) -> Self {
5660
let mut image_repeat = vec![];
5761
match value {
@@ -62,11 +66,11 @@ impl From<&Property<'_>> for ImageRepeat {
6266
}
6367
_ => {}
6468
}
65-
ImageRepeat(image_repeat)
69+
BackgroundRepeat(image_repeat)
6670
}
6771
}
6872

69-
impl ToExpr for ImageRepeat {
73+
impl ToExpr for BackgroundRepeat {
7074
fn to_expr(&self) -> Expr {
7175
Expr::Array(ArrayLit {
7276
span: DUMMY_SP,

src/style_transform/background/background_size.rs

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use lightningcss::{
2-
properties::{background::BackgroundSize, Property},
2+
properties::{background::BackgroundSize as LNBackgroundSize, Property},
33
stylesheet::PrinterOptions,
44
traits::ToCss,
55
values::length::LengthPercentageOrAuto,
@@ -13,32 +13,34 @@ use swc_ecma_ast::{
1313

1414
use crate::{style_transform::traits::ToExpr, utils::convert_px_to_units};
1515

16-
pub fn parse_background_size_item(size_item: &BackgroundSize) -> Option<ImageSize> {
16+
pub fn parse_background_size_item(size_item: &LNBackgroundSize) -> Option<ImageSize> {
1717
match size_item {
18-
BackgroundSize::Contain => Some(ImageSize::Contain),
19-
BackgroundSize::Cover => Some(ImageSize::Cover),
20-
BackgroundSize::Explicit { width, height } => match width {
21-
LengthPercentageOrAuto::Auto => match height {
22-
LengthPercentageOrAuto::Auto => Some(ImageSize::Auto),
23-
_ => None,
24-
},
25-
LengthPercentageOrAuto::LengthPercentage(x) => {
26-
let x_str = x.to_css_string(PrinterOptions::default()).unwrap();
27-
match height {
28-
LengthPercentageOrAuto::LengthPercentage(y) => {
29-
let y_str = y.to_css_string(PrinterOptions::default()).unwrap();
30-
Some(ImageSize::ImageSizeWH(x_str, Some(y_str)))
31-
},
32-
LengthPercentageOrAuto::Auto => {
33-
Some(ImageSize::ImageSizeWH(x_str, None))
18+
LNBackgroundSize::Contain => Some(ImageSize::Contain),
19+
LNBackgroundSize::Cover => Some(ImageSize::Cover),
20+
LNBackgroundSize::Explicit { width, height } => {
21+
match width {
22+
LengthPercentageOrAuto::Auto => match height {
23+
LengthPercentageOrAuto::Auto => Some(ImageSize::Auto),
24+
_ => None,
25+
},
26+
LengthPercentageOrAuto::LengthPercentage(x) => {
27+
let x_str = x.to_css_string(PrinterOptions::default()).unwrap();
28+
match height {
29+
LengthPercentageOrAuto::LengthPercentage(y) => {
30+
let y_str = y.to_css_string(PrinterOptions::default()).unwrap();
31+
Some(ImageSize::ImageSizeWH(x_str, Some(y_str)))
32+
},
33+
LengthPercentageOrAuto::Auto => {
34+
Some(ImageSize::ImageSizeWH(x_str, None))
35+
}
3436
}
35-
}
36-
},
37-
},
37+
},
38+
}
39+
}
3840
}
3941
}
4042

41-
pub fn parse_background_size(size: &SmallVec<[BackgroundSize; 1]>) -> BackgroundImageSize {
43+
pub fn parse_background_size(size: &SmallVec<[LNBackgroundSize; 1]>) -> BackgroundSize {
4244
let mut background_size = vec![];
4345
for item in size {
4446
let item_size = parse_background_size_item(item);
@@ -47,7 +49,7 @@ pub fn parse_background_size(size: &SmallVec<[BackgroundSize; 1]>) -> Background
4749
}
4850
}
4951

50-
BackgroundImageSize(background_size)
52+
BackgroundSize(background_size)
5153
}
5254

5355
#[derive(Debug, Clone)]
@@ -59,9 +61,9 @@ pub enum ImageSize {
5961
}
6062

6163
#[derive(Debug, Clone)]
62-
pub struct BackgroundImageSize(pub Vec<ImageSize>);
64+
pub struct BackgroundSize(pub Vec<ImageSize>);
6365

64-
impl ToExpr for BackgroundImageSize {
66+
impl ToExpr for BackgroundSize {
6567
fn to_expr(&self) -> Expr {
6668
Expr::Array(ArrayLit {
6769
span: DUMMY_SP,
@@ -130,9 +132,9 @@ impl ToExpr for BackgroundImageSize {
130132
}
131133
}
132134

133-
impl From<&Property<'_>> for BackgroundImageSize {
135+
impl From<&Property<'_>> for BackgroundSize {
134136
fn from(value: &Property<'_>) -> Self {
135-
let mut background_image_size = BackgroundImageSize(vec![]);
137+
let mut background_image_size = BackgroundSize(vec![]);
136138
match value {
137139
Property::BackgroundSize(value) => {
138140
background_image_size = parse_background_size(&value);

0 commit comments

Comments
 (0)