@@ -36,6 +36,7 @@ pub struct RuleItem {
3636 pub selector : String ,
3737 pub media : u32 ,
3838 pub declarations : Vec < StyleValueType > ,
39+ pub important_declarections : Vec < StyleValueType > ,
3940 pub variables : Vec < CssVariable > ,
4041 pub has_env : bool
4142}
@@ -69,7 +70,7 @@ impl KeyFrameItem {
6970 key: PropName :: Str ( "event" . into( ) ) ,
7071 value: Box :: new( Expr :: Array ( ArrayLit {
7172 span: DUMMY_SP ,
72- elems: parse_style_values( self . declarations. clone( ) , Platform :: Harmony ) ,
73+ elems: parse_style_values( self . declarations. clone( ) , vec! [ ] , Platform :: Harmony ) ,
7374 } ) ) ,
7475 } ) ) ) ,
7576 ] ;
@@ -357,41 +358,54 @@ impl<'i> StyleParser<'i> {
357358 binding
358359 . iter_mut ( )
359360 . for_each ( |( media_index, selector, style_value) | {
360- let properties = style_value
361- . declaration
362- . declarations
363- . iter ( )
364- . map ( |property| {
365- (
366- to_camel_case (
367- property
368- . property_id ( )
369- . to_css_string ( PrinterOptions :: default ( ) )
370- . unwrap ( )
371- . as_str ( ) ,
372- false ,
373- ) ,
374- property. clone ( ) ,
375- )
376- } )
377- . collect :: < Vec < ( _ , _ ) > > ( ) ; // Specify the lifetime of the tuple elements to match the input data
378- final_all_style. push ( ( media_index, selector. to_owned ( ) , properties) ) ;
361+ // 辅助函数,用于处理属性转换,减少代码重复
362+ let convert_properties = |props : & Vec < Property < ' i > > | -> Vec < ( String , Property < ' i > ) > {
363+ props
364+ . iter ( )
365+ . map ( |property| {
366+ (
367+ to_camel_case (
368+ property
369+ . property_id ( )
370+ . to_css_string ( PrinterOptions :: default ( ) )
371+ . unwrap ( )
372+ . as_str ( ) ,
373+ false ,
374+ ) ,
375+ property. clone ( ) ,
376+ )
377+ } )
378+ . collect ( )
379+ } ;
380+
381+ // 处理普通属性和important属性
382+ let properties = convert_properties ( & style_value. declaration . declarations ) ;
383+ let important_properties = convert_properties ( & style_value. declaration . important_declarations ) ;
384+
385+ final_all_style. push ( ( media_index, selector. to_owned ( ) , properties, important_properties) ) ;
379386 } ) ;
380387
381388 // 进行样式解析优化,提前解析 ArkUI 的样式,减少运行时的计算
382389 let final_all_style = final_all_style
383390 . iter_mut ( )
384- . map ( |( media_index, selector, properties) | {
391+ . map ( |( media_index, selector, properties, important_properties ) | {
385392 let decls_and_vars = parse_style_properties (
386393 & properties
387394 . iter ( )
388395 . map ( |( k, v) | ( k. to_owned ( ) , v. clone ( ) ) )
389396 . collect :: < Vec < _ > > ( )
390397 ) ;
398+ let import_decls_and_vars = parse_style_properties (
399+ & important_properties
400+ . iter ( )
401+ . map ( |( k, v) | ( k. to_owned ( ) , v. clone ( ) ) )
402+ . collect :: < Vec < _ > > ( )
403+ ) ;
391404 RuleItem {
392405 selector : selector. to_owned ( ) ,
393406 media : media_index. to_owned ( ) ,
394407 declarations : decls_and_vars. decls ,
408+ important_declarections : import_decls_and_vars. decls ,
395409 variables : decls_and_vars. vars ,
396410 has_env : decls_and_vars. has_env
397411 }
@@ -431,28 +445,15 @@ impl<'i> StyleParser<'i> {
431445 let declaration = & declaration. declaration ;
432446 let declarations = & declaration. declarations ;
433447 for declaration in declarations. iter ( ) {
434- let has_property_index = final_properties
435- . iter ( )
436- . position ( |property| property. property_id ( ) == declaration. property_id ( ) ) ;
437- if let Some ( index) = has_property_index {
438- final_properties[ index] = declaration. clone ( ) ;
439- } else {
440- final_properties. push ( declaration. clone ( ) ) ;
441- }
448+ final_properties. push ( declaration. clone ( ) ) ;
442449 }
443450 }
451+ let mut important_properties: Vec < Property < ' i > > = Vec :: new ( ) ;
444452 for declaration in declarations. iter ( ) {
445453 let declaration = & declaration. declaration ;
446454 let important_declarations = & declaration. important_declarations ;
447455 for declaration in important_declarations. iter ( ) {
448- let has_property_index = final_properties
449- . iter ( )
450- . position ( |property| property. property_id ( ) == declaration. property_id ( ) ) ;
451- if let Some ( index) = has_property_index {
452- final_properties[ index] = declaration. clone ( ) ;
453- } else {
454- final_properties. push ( declaration. clone ( ) ) ;
455- }
456+ important_properties. push ( declaration. clone ( ) ) ;
456457 }
457458 }
458459 final_style_record. push ( (
@@ -462,7 +463,7 @@ impl<'i> StyleParser<'i> {
462463 specificity : 0 ,
463464 declaration : DeclarationBlock {
464465 declarations : final_properties,
465- important_declarations : vec ! [ ] ,
466+ important_declarations : important_properties ,
466467 } ,
467468 } ,
468469 ) ) ;
0 commit comments