@@ -767,6 +767,12 @@ impl ToExpr for BackgroundColor {
767
767
}
768
768
}
769
769
770
+ impl Display for BackgroundColor {
771
+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
772
+ write ! ( f, "{}" , self . 0 . to_string( ) )
773
+ }
774
+ }
775
+
770
776
pub struct BackgroundImageStr {
771
777
pub src : String ,
772
778
pub repeat : Option < String > ,
@@ -864,6 +870,30 @@ impl From<&BackgroundImageStr> for BackgroundImage {
864
870
}
865
871
}
866
872
873
+ impl Display for BackgroundImage {
874
+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
875
+ let mut background_image = "" . to_string ( ) ;
876
+ for ( index, item) in self . 0 . iter ( ) . enumerate ( ) {
877
+ if let BackgroundImageKind :: String ( src) = & item. image {
878
+ background_image. push_str ( src) ;
879
+ if let Some ( repeat) = & item. repeat {
880
+ background_image. push_str ( " " ) ;
881
+ background_image. push_str ( match repeat {
882
+ ImageRepeat :: XY => "repeat" ,
883
+ ImageRepeat :: X => "repeat-x" ,
884
+ ImageRepeat :: Y => "repeat-y" ,
885
+ ImageRepeat :: NoRepeat => "no-repeat" ,
886
+ } ) ;
887
+ }
888
+ if index != self . 0 . len ( ) - 1 {
889
+ background_image. push_str ( ", " ) ;
890
+ }
891
+ }
892
+ }
893
+ write ! ( f, "{}" , background_image)
894
+ }
895
+ }
896
+
867
897
#[ derive( Debug , Clone ) ]
868
898
pub enum BackgroundImageKind {
869
899
String ( String ) ,
@@ -960,6 +990,27 @@ impl From<&str> for BackgroundImageSize {
960
990
}
961
991
}
962
992
993
+ impl Display for BackgroundImageSize {
994
+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
995
+ let mut background_image_size = "" . to_string ( ) ;
996
+ for ( index, item) in self . 0 . iter ( ) . enumerate ( ) {
997
+ background_image_size. push_str (
998
+ match item {
999
+ ImageSize :: Cover => "cover" . to_string ( ) ,
1000
+ ImageSize :: Contain => "contain" . to_string ( ) ,
1001
+ ImageSize :: Auto => "auto" . to_string ( ) ,
1002
+ ImageSize :: ImageSizeWH ( width, height) => format ! ( "{} {}" , width, height) ,
1003
+ }
1004
+ . as_str ( ) ,
1005
+ ) ;
1006
+ if index != self . 0 . len ( ) - 1 {
1007
+ background_image_size. push_str ( ", " ) ;
1008
+ }
1009
+ }
1010
+ write ! ( f, "{}" , background_image_size)
1011
+ }
1012
+ }
1013
+
963
1014
#[ derive( Debug , Clone ) ]
964
1015
pub enum ImageRepeat {
965
1016
XY ,
@@ -1159,6 +1210,33 @@ impl From<&str> for BackgroundImagePosition {
1159
1210
}
1160
1211
}
1161
1212
1213
+ impl Display for BackgroundImagePosition {
1214
+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
1215
+ let mut background_image_position = "" . to_string ( ) ;
1216
+ for ( index, item) in self . 0 . iter ( ) . enumerate ( ) {
1217
+ background_image_position. push_str (
1218
+ match item {
1219
+ ImagePosition :: ImagePositionXY ( x, y) => format ! ( "{} {}" , x, y) ,
1220
+ ImagePosition :: TopStart => "top left" . to_string ( ) ,
1221
+ ImagePosition :: Top => "top center" . to_string ( ) ,
1222
+ ImagePosition :: TopEnd => "top right" . to_string ( ) ,
1223
+ ImagePosition :: Start => "center left" . to_string ( ) ,
1224
+ ImagePosition :: Center => "center center" . to_string ( ) ,
1225
+ ImagePosition :: End => "center right" . to_string ( ) ,
1226
+ ImagePosition :: BottomStart => "bottom left" . to_string ( ) ,
1227
+ ImagePosition :: Bottom => "bottom center" . to_string ( ) ,
1228
+ ImagePosition :: BottomEnd => "bottom right" . to_string ( ) ,
1229
+ }
1230
+ . as_str ( ) ,
1231
+ ) ;
1232
+ if index != self . 0 . len ( ) - 1 {
1233
+ background_image_position. push_str ( ", " ) ;
1234
+ }
1235
+ }
1236
+ write ! ( f, "{}" , background_image_position)
1237
+ }
1238
+ }
1239
+
1162
1240
#[ derive( Debug , Clone ) ]
1163
1241
pub struct Background {
1164
1242
pub image : BackgroundImage ,
@@ -1167,6 +1245,44 @@ pub struct Background {
1167
1245
pub position : BackgroundImagePosition ,
1168
1246
}
1169
1247
1248
+ impl Background {
1249
+ pub fn new ( ) -> Self {
1250
+ Background {
1251
+ image : BackgroundImage ( vec ! [ ] ) ,
1252
+ color : BackgroundColor ( "" . to_string ( ) ) ,
1253
+ size : BackgroundImageSize ( vec ! [ ] ) ,
1254
+ position : BackgroundImagePosition ( vec ! [ ] ) ,
1255
+ }
1256
+ }
1257
+ }
1258
+
1259
+ impl ToExpr for Background {
1260
+ fn to_expr ( & self ) -> Expr {
1261
+ Expr :: Object ( ObjectLit {
1262
+ span : DUMMY_SP ,
1263
+ props : vec ! [
1264
+ PropOrSpread :: Prop ( Box :: new( Prop :: KeyValue ( KeyValueProp {
1265
+ key: PropName :: Ident ( Ident :: new( "image" . into( ) , DUMMY_SP ) ) ,
1266
+ value: self . image. to_expr( ) . into( ) ,
1267
+ } ) ) ) ,
1268
+ PropOrSpread :: Prop ( Box :: new( Prop :: KeyValue ( KeyValueProp {
1269
+ key: PropName :: Ident ( Ident :: new( "color" . into( ) , DUMMY_SP ) ) ,
1270
+ value: self . color. to_expr( ) . into( ) ,
1271
+ } ) ) ) ,
1272
+ PropOrSpread :: Prop ( Box :: new( Prop :: KeyValue ( KeyValueProp {
1273
+ key: PropName :: Ident ( Ident :: new( "size" . into( ) , DUMMY_SP ) ) ,
1274
+ value: self . size. to_expr( ) . into( ) ,
1275
+ } ) ) ) ,
1276
+ PropOrSpread :: Prop ( Box :: new( Prop :: KeyValue ( KeyValueProp {
1277
+ key: PropName :: Ident ( Ident :: new( "position" . into( ) , DUMMY_SP ) ) ,
1278
+ value: self . position. to_expr( ) . into( ) ,
1279
+ } ) ) ) ,
1280
+ ]
1281
+ . into ( ) ,
1282
+ } )
1283
+ }
1284
+ }
1285
+
1170
1286
impl From < & str > for Background {
1171
1287
fn from ( value : & str ) -> Self {
1172
1288
let background_parsed = Property :: parse_string (
@@ -1692,10 +1808,7 @@ pub enum StyleValueType {
1692
1808
TextDecoration ( TextDecoration ) ,
1693
1809
BorderRadius ( BorderRadius ) ,
1694
1810
MarginPadding ( MarginPadding ) ,
1695
- BackgroundImage ( BackgroundImage ) ,
1696
- BackgroundColor ( BackgroundColor ) ,
1697
- BackgroundImageSize ( BackgroundImageSize ) ,
1698
- BackgroundImagePosition ( BackgroundImagePosition ) ,
1811
+ Background ( Background ) ,
1699
1812
LinearGradient ( LinearGradient ) ,
1700
1813
FlexOptions ( FlexOptions ) ,
1701
1814
AlignSelf ( ItemAlign ) ,
@@ -1725,97 +1838,12 @@ impl Display for StyleValueType {
1725
1838
value. top, value. right, value. bottom, value. left
1726
1839
)
1727
1840
}
1728
- StyleValueType :: BackgroundImage ( value) => {
1729
- let mut image = "" . to_string ( ) ;
1730
- for item in value. 0 . iter ( ) {
1731
- if let BackgroundImageKind :: String ( src) = & item. image {
1732
- image. push_str ( src. as_str ( ) ) ;
1733
- image. push_str ( " " ) ;
1734
- if let Some ( repeat) = & item. repeat {
1735
- match repeat {
1736
- ImageRepeat :: XY => {
1737
- image. push_str ( "repeat" ) ;
1738
- }
1739
- ImageRepeat :: X => {
1740
- image. push_str ( "repeat-x" ) ;
1741
- }
1742
- ImageRepeat :: Y => {
1743
- image. push_str ( "repeat-y" ) ;
1744
- }
1745
- ImageRepeat :: NoRepeat => {
1746
- image. push_str ( "no-repeat" ) ;
1747
- }
1748
- }
1749
- image. push_str ( " " ) ;
1750
- }
1751
- }
1752
- }
1753
- write ! ( f, "{}" , image)
1754
- }
1755
- StyleValueType :: BackgroundColor ( value) => write ! ( f, "{}" , value. 0 ) ,
1756
- StyleValueType :: BackgroundImageSize ( value) => {
1757
- let mut size = "" . to_string ( ) ;
1758
- for item in value. 0 . iter ( ) {
1759
- match item {
1760
- ImageSize :: Cover => {
1761
- size. push_str ( "cover" ) ;
1762
- }
1763
- ImageSize :: Contain => {
1764
- size. push_str ( "contain" ) ;
1765
- }
1766
- ImageSize :: Auto => {
1767
- size. push_str ( "auto" ) ;
1768
- }
1769
- ImageSize :: ImageSizeWH ( width, height) => {
1770
- size. push_str ( width. as_str ( ) ) ;
1771
- size. push_str ( " " ) ;
1772
- size. push_str ( height. as_str ( ) ) ;
1773
- }
1774
- }
1775
- size. push_str ( " " ) ;
1776
- }
1777
- write ! ( f, "{}" , size)
1778
- }
1779
- StyleValueType :: BackgroundImagePosition ( value) => {
1780
- let mut position = "" . to_string ( ) ;
1781
- for item in value. 0 . iter ( ) {
1782
- match item {
1783
- ImagePosition :: ImagePositionXY ( x, y) => {
1784
- position. push_str ( x. as_str ( ) ) ;
1785
- position. push_str ( " " ) ;
1786
- position. push_str ( y. as_str ( ) ) ;
1787
- }
1788
- ImagePosition :: TopStart => {
1789
- position. push_str ( "top left" ) ;
1790
- }
1791
- ImagePosition :: Top => {
1792
- position. push_str ( "top center" ) ;
1793
- }
1794
- ImagePosition :: TopEnd => {
1795
- position. push_str ( "top right" ) ;
1796
- }
1797
- ImagePosition :: Start => {
1798
- position. push_str ( "center left" ) ;
1799
- }
1800
- ImagePosition :: Center => {
1801
- position. push_str ( "center center" ) ;
1802
- }
1803
- ImagePosition :: End => {
1804
- position. push_str ( "center right" ) ;
1805
- }
1806
- ImagePosition :: BottomStart => {
1807
- position. push_str ( "bottom left" ) ;
1808
- }
1809
- ImagePosition :: Bottom => {
1810
- position. push_str ( "bottom center" ) ;
1811
- }
1812
- ImagePosition :: BottomEnd => {
1813
- position. push_str ( "bottom right" ) ;
1814
- }
1815
- }
1816
- position. push_str ( " " ) ;
1817
- }
1818
- write ! ( f, "{}" , position)
1841
+ StyleValueType :: Background ( value) => {
1842
+ write ! (
1843
+ f,
1844
+ "{} {} {} {}" ,
1845
+ value. image, value. color, value. size, value. position
1846
+ )
1819
1847
}
1820
1848
StyleValueType :: LinearGradient ( linear_gradient) => {
1821
1849
let mut linear_gradient_str = "" . to_string ( ) ;
@@ -1927,12 +1955,7 @@ impl ToExpr for StyleValueType {
1927
1955
StyleValueType :: TextDecoration ( text_decoration) => text_decoration. to_expr ( ) . into ( ) ,
1928
1956
StyleValueType :: BorderRadius ( border_radius) => border_radius. to_expr ( ) . into ( ) ,
1929
1957
StyleValueType :: MarginPadding ( margin_padding) => margin_padding. to_expr ( ) . into ( ) ,
1930
- StyleValueType :: BackgroundImage ( background_image) => background_image. to_expr ( ) . into ( ) ,
1931
- StyleValueType :: BackgroundImageSize ( background_size) => background_size. to_expr ( ) . into ( ) ,
1932
- StyleValueType :: BackgroundImagePosition ( background_position) => {
1933
- background_position. to_expr ( ) . into ( )
1934
- }
1935
- StyleValueType :: BackgroundColor ( background_color) => background_color. to_expr ( ) . into ( ) ,
1958
+ StyleValueType :: Background ( background) => background. to_expr ( ) . into ( ) ,
1936
1959
StyleValueType :: LinearGradient ( linear_gradient) => linear_gradient. to_expr ( ) . into ( ) ,
1937
1960
StyleValueType :: FlexOptions ( flex_options) => flex_options. to_expr ( ) . into ( ) ,
1938
1961
StyleValueType :: AlignSelf ( align_self) => align_self. to_expr ( ) . into ( ) ,
@@ -2396,7 +2419,7 @@ impl<'i> StyleParser<'i> {
2396
2419
}
2397
2420
"background" => match value {
2398
2421
Property :: Background ( value) => {
2399
- let background = parse_background ( value) ;
2422
+ let mut background = parse_background ( value) ;
2400
2423
let mut images = vec ! [ ] ;
2401
2424
let mut linear_gradient = vec ! [ ] ;
2402
2425
for item in background. image . 0 . iter ( ) {
@@ -2406,38 +2429,28 @@ impl<'i> StyleParser<'i> {
2406
2429
linear_gradient. push ( gradient. clone ( ) ) ;
2407
2430
}
2408
2431
}
2432
+ final_properties. remove ( "background" ) ;
2433
+ final_properties. remove ( "linearGradient" ) ;
2409
2434
if images. len ( ) > 0 {
2410
- final_properties. insert (
2411
- "backgroundImage" . to_string ( ) ,
2412
- StyleValueType :: BackgroundImage ( BackgroundImage ( images) ) ,
2413
- ) ;
2435
+ background. image = BackgroundImage ( images) ;
2436
+ final_properties. insert ( id. to_string ( ) , StyleValueType :: Background ( background) ) ;
2414
2437
}
2415
2438
if linear_gradient. len ( ) > 0 {
2416
2439
final_properties. insert (
2417
2440
"linearGradient" . to_string ( ) ,
2418
2441
StyleValueType :: LinearGradient ( LinearGradient ( linear_gradient) ) ,
2419
2442
) ;
2420
2443
}
2421
- final_properties. insert (
2422
- "backgroundImagePosition" . to_string ( ) ,
2423
- StyleValueType :: BackgroundImagePosition ( background. position ) ,
2424
- ) ;
2425
- final_properties. insert (
2426
- "backgroundImageSize" . to_string ( ) ,
2427
- StyleValueType :: BackgroundImageSize ( background. size ) ,
2428
- ) ;
2429
- final_properties. insert (
2430
- "backgroundColor" . to_string ( ) ,
2431
- StyleValueType :: BackgroundColor ( background. color ) ,
2432
- ) ;
2433
2444
}
2434
2445
_ => { }
2435
2446
} ,
2436
2447
"backgroundColor" => match value {
2437
2448
Property :: BackgroundColor ( value) => {
2438
- final_properties. insert (
2439
- id. to_string ( ) ,
2440
- StyleValueType :: BackgroundColor ( BackgroundColor (
2449
+ let background = final_properties
2450
+ . entry ( "background" . to_string ( ) )
2451
+ . or_insert ( StyleValueType :: Background ( Background :: new ( ) ) ) ;
2452
+ if let StyleValueType :: Background ( background) = background {
2453
+ background. color = BackgroundColor (
2441
2454
value
2442
2455
. to_css_string ( PrinterOptions {
2443
2456
minify : false ,
@@ -2448,8 +2461,8 @@ impl<'i> StyleParser<'i> {
2448
2461
..PrinterOptions :: default ( )
2449
2462
} )
2450
2463
. unwrap ( ) ,
2451
- ) ) ,
2452
- ) ;
2464
+ ) ;
2465
+ }
2453
2466
}
2454
2467
_ => { }
2455
2468
} ,
@@ -2472,10 +2485,12 @@ impl<'i> StyleParser<'i> {
2472
2485
}
2473
2486
}
2474
2487
if images. len ( ) > 0 {
2475
- final_properties. insert (
2476
- id. to_string ( ) ,
2477
- StyleValueType :: BackgroundImage ( BackgroundImage ( images) ) ,
2478
- ) ;
2488
+ let background = final_properties
2489
+ . entry ( "background" . to_string ( ) )
2490
+ . or_insert ( StyleValueType :: Background ( Background :: new ( ) ) ) ;
2491
+ if let StyleValueType :: Background ( background) = background {
2492
+ background. image = BackgroundImage ( images) ;
2493
+ }
2479
2494
}
2480
2495
if linear_gradient. len ( ) > 0 {
2481
2496
final_properties. insert (
@@ -2490,10 +2505,12 @@ impl<'i> StyleParser<'i> {
2490
2505
Property :: BackgroundPosition ( value) => {
2491
2506
let background_position = parse_background_position ( value) ;
2492
2507
if background_position. 0 . len ( ) > 0 {
2493
- final_properties. insert (
2494
- "backgroundImagePosition" . to_string ( ) ,
2495
- StyleValueType :: BackgroundImagePosition ( background_position) ,
2496
- ) ;
2508
+ let background = final_properties
2509
+ . entry ( "background" . to_string ( ) )
2510
+ . or_insert ( StyleValueType :: Background ( Background :: new ( ) ) ) ;
2511
+ if let StyleValueType :: Background ( background) = background {
2512
+ background. position = background_position;
2513
+ }
2497
2514
}
2498
2515
}
2499
2516
_ => { }
@@ -2502,10 +2519,12 @@ impl<'i> StyleParser<'i> {
2502
2519
Property :: BackgroundSize ( value) => {
2503
2520
let background_size = parse_background_size ( value) ;
2504
2521
if background_size. 0 . len ( ) > 0 {
2505
- final_properties. insert (
2506
- "backgroundImageSize" . to_string ( ) ,
2507
- StyleValueType :: BackgroundImageSize ( background_size) ,
2508
- ) ;
2522
+ let background = final_properties
2523
+ . entry ( "background" . to_string ( ) )
2524
+ . or_insert ( StyleValueType :: Background ( Background :: new ( ) ) ) ;
2525
+ if let StyleValueType :: Background ( background) = background {
2526
+ background. size = background_size;
2527
+ }
2509
2528
}
2510
2529
}
2511
2530
_ => { }
0 commit comments