@@ -3,8 +3,7 @@ use lightningcss::{
33 angle:: Angle ,
44 gradient:: { Gradient , GradientItem , LineDirection } ,
55 image:: Image ,
6- length:: LengthValue ,
7- percentage:: DimensionPercentage ,
6+ percentage:: { DimensionPercentage , Percentage } ,
87 position:: { HorizontalPositionKeyword , VerticalPositionKeyword } ,
98 }
109} ;
@@ -13,9 +12,9 @@ use smallvec::SmallVec;
1312use swc_core:: ecma:: ast:: * ;
1413use swc_core:: common:: DUMMY_SP ;
1514
16- use crate :: generate_invalid_expr;
15+ use crate :: { generate_expr_lit_num , generate_expr_lit_str , generate_invalid_expr} ;
1716
18- use super :: { graident_properties:: linear_gradient:: { LinearGradientDirection , LinearGradientItem } , traits:: ToExpr , unit:: { convert_color_keywords_to_hex, PropertyTuple } } ;
17+ use super :: { graident_properties:: { linear_gradient:: { LinearGradientDirection , LinearGradientItem } , radial_gradient :: { RadialGradientItem , RadialGradientPoint } } , traits:: ToExpr , unit:: { convert_color_keywords_to_hex, PropertyTuple } } ;
1918
2019pub fn parse_background_image_item ( image : & Image ) -> Option < BackgroundImageKind > {
2120 match image {
@@ -26,15 +25,18 @@ pub fn parse_background_image_item(image: &Image) -> Option<BackgroundImageKind>
2625 match & * * gradient {
2726 Gradient :: Linear ( gradient) => {
2827 let mut color_stops = vec ! [ ] ;
29- for item in & gradient. items {
28+ let mut now_percentage = 0.0 ;
29+ let colors_len = gradient. items . len ( ) - 1 ;
30+ for ( index, item) in gradient. items . clone ( ) . into_iter ( ) . enumerate ( ) {
3031 match item {
3132 GradientItem :: ColorStop ( color_stop) => {
33+ let item_pecentage = now_percentage + ( ( ( 1.0 - now_percentage) / colors_len as f32 ) * index as f32 ) ;
3234 let color_stop_position = color_stop
3335 . position
3436 . clone ( )
35- . unwrap_or ( DimensionPercentage :: Dimension ( LengthValue :: Px ( 0.0 ) ) ) ;
37+ . unwrap_or ( DimensionPercentage :: Percentage ( Percentage ( item_pecentage ) ) ) ;
3638 color_stops. push ( (
37- convert_color_keywords_to_hex ( color_stop
39+ generate_expr_lit_str ! ( convert_color_keywords_to_hex( color_stop
3840 . color
3941 . to_css_string( PrinterOptions {
4042 minify: false ,
@@ -44,15 +46,14 @@ pub fn parse_background_image_item(image: &Image) -> Option<BackgroundImageKind>
4446 } ,
4547 ..PrinterOptions :: default ( )
4648 } )
47- . unwrap ( ) ) ,
49+ . unwrap( ) ) ) ,
4850 match & color_stop_position {
49- DimensionPercentage :: Dimension ( length) => {
50- length. to_css_string ( PrinterOptions :: default ( ) ) . unwrap ( )
51- }
52- DimensionPercentage :: Percentage ( percentage) => percentage. 0 . to_string ( ) ,
53- _ => color_stop_position
54- . to_css_string ( PrinterOptions :: default ( ) )
55- . unwrap ( ) ,
51+ DimensionPercentage :: Dimension ( _) => generate_expr_lit_num ! ( 0.0 ) ,
52+ DimensionPercentage :: Percentage ( percentage) => {
53+ now_percentage = percentage. 0 ;
54+ generate_expr_lit_num ! ( percentage. 0 as f64 )
55+ } ,
56+ DimensionPercentage :: Calc ( _) => generate_expr_lit_num ! ( 0.0 ) ,
5657 } ,
5758 ) ) ;
5859 }
@@ -117,17 +118,20 @@ pub fn parse_background_image_item(image: &Image) -> Option<BackgroundImageKind>
117118 } ,
118119 Gradient :: RepeatingLinear ( _) => None ,
119120 Gradient :: Radial ( gradient) => {
120- // Radial 话啊喂的半径需要
121+ // Radial 华为的半径需要具体的单位
121122 let mut color_stops = vec ! [ ] ;
122- for item in & gradient. items {
123+ let mut now_percentage = 0.0 ;
124+ let colors_len = gradient. items . len ( ) - 1 ;
125+ for ( index, item) in gradient. items . clone ( ) . into_iter ( ) . enumerate ( ) {
123126 match item {
124127 GradientItem :: ColorStop ( color_stop) => {
128+ let item_pecentage = now_percentage + ( ( ( 1.0 - now_percentage) / colors_len as f32 ) * index as f32 ) ;
125129 let color_stop_position = color_stop
126130 . position
127131 . clone ( )
128- . unwrap_or ( DimensionPercentage :: Dimension ( LengthValue :: Px ( 0.0 ) ) ) ;
132+ . unwrap_or ( DimensionPercentage :: Percentage ( Percentage ( item_pecentage ) ) ) ;
129133 color_stops. push ( (
130- convert_color_keywords_to_hex ( color_stop
134+ generate_expr_lit_str ! ( convert_color_keywords_to_hex( color_stop
131135 . color
132136 . to_css_string( PrinterOptions {
133137 minify: false ,
@@ -137,23 +141,28 @@ pub fn parse_background_image_item(image: &Image) -> Option<BackgroundImageKind>
137141 } ,
138142 ..PrinterOptions :: default ( )
139143 } )
140- . unwrap ( ) ) ,
144+ . unwrap( ) ) ) ,
141145 match & color_stop_position {
142- DimensionPercentage :: Dimension ( length) => {
143- length. to_css_string ( PrinterOptions :: default ( ) ) . unwrap ( )
144- }
145- DimensionPercentage :: Percentage ( percentage) => percentage. 0 . to_string ( ) ,
146- _ => color_stop_position
147- . to_css_string ( PrinterOptions :: default ( ) )
148- . unwrap ( ) ,
146+ DimensionPercentage :: Dimension ( _) => generate_expr_lit_num ! ( 0.0 ) ,
147+ DimensionPercentage :: Percentage ( percentage) => {
148+ now_percentage = percentage. 0 ;
149+ generate_expr_lit_num ! ( percentage. 0 as f64 )
150+ } ,
151+ DimensionPercentage :: Calc ( _) => generate_expr_lit_num ! ( 0.0 ) ,
149152 } ,
150153 ) ) ;
151154 }
152155 _ => { }
153156 } ;
154157 }
155158
156- None
159+
160+
161+ Some ( BackgroundImageKind :: RadialGradient ( RadialGradientItem {
162+ color_stops,
163+ point : RadialGradientPoint { x : gradient. position . x . clone ( ) , y : gradient. position . y . clone ( ) } ,
164+ shape : gradient. shape . clone ( ) ,
165+ } ) )
157166 } ,
158167 Gradient :: RepeatingRadial ( _) => None ,
159168 Gradient :: Conic ( _) => None ,
@@ -178,7 +187,8 @@ pub fn parse_background_image(image: &SmallVec<[Image; 1]>) -> Vec<BackgroundIma
178187#[ derive( Debug , Clone ) ]
179188pub enum BackgroundImageKind {
180189 String ( String ) ,
181- LinearGradient ( LinearGradientItem )
190+ LinearGradient ( LinearGradientItem ) ,
191+ RadialGradient ( RadialGradientItem ) ,
182192}
183193
184194#[ derive( Debug , Clone ) ]
@@ -205,6 +215,9 @@ impl ToExpr for BackgroundImage {
205215 Some ( BackgroundImageKind :: LinearGradient ( linear_gradient) ) => {
206216 linear_gradient. to_expr ( )
207217 } ,
218+ Some ( BackgroundImageKind :: RadialGradient ( radial_gradient) ) => {
219+ radial_gradient. to_expr ( )
220+ } ,
208221 _ => generate_invalid_expr ! ( )
209222 } ;
210223 PropertyTuple :: One (
0 commit comments