1- use std:: { cell:: RefCell , collections:: HashMap , convert:: Infallible , rc:: Rc } ;
1+ use std:: { cell:: RefCell , collections:: HashMap , convert:: Infallible , rc:: Rc , hash :: Hash } ;
22
33use lightningcss:: {
44 declaration:: DeclarationBlock ,
@@ -11,6 +11,11 @@ use lightningcss::{
1111
1212use crate :: { document:: JSXDocument , visitor:: SpanKey } ;
1313
14+ pub struct StyleData < ' i > {
15+ pub style_record : Rc < RefCell < HashMap < SpanKey , StyleDeclaration < ' i > > > > ,
16+ pub all_style : Rc < RefCell < HashMap < String , StyleDeclaration < ' i > > > > ,
17+ }
18+
1419#[ derive( Debug , Clone ) ]
1520pub struct StyleDeclaration < ' i > {
1621 pub specificity : u32 ,
@@ -19,16 +24,19 @@ pub struct StyleDeclaration<'i> {
1924
2025pub struct StyleVisitor < ' i > {
2126 pub style_record : Rc < RefCell < HashMap < SpanKey , Vec < StyleDeclaration < ' i > > > > > ,
27+ pub all_style : Rc < RefCell < HashMap < String , Vec < StyleDeclaration < ' i > > > > > ,
2228 pub document : & ' i JSXDocument ,
2329}
2430
2531impl < ' i > StyleVisitor < ' i > {
2632 pub fn new (
2733 document : & ' i JSXDocument ,
2834 style_record : Rc < RefCell < HashMap < SpanKey , Vec < StyleDeclaration < ' i > > > > > ,
35+ all_style : Rc < RefCell < HashMap < String , Vec < StyleDeclaration < ' i > > > > > ,
2936 ) -> Self {
3037 StyleVisitor {
3138 style_record,
39+ all_style,
3240 document,
3341 }
3442 }
@@ -44,6 +52,15 @@ impl<'i> Visitor<'i> for StyleVisitor<'i> {
4452 let selectors = selectors_str. split ( "," ) . collect :: < Vec < & str > > ( ) ;
4553 for index in 0 ..selectors. len ( ) {
4654 let selector = selectors[ index] . trim ( ) . replace ( "." , "" ) ;
55+ {
56+ let mut all_style = self . all_style . borrow_mut ( ) ;
57+ let declarations: & mut Vec < StyleDeclaration < ' _ > > =
58+ all_style. entry ( selector. clone ( ) ) . or_insert ( vec ! [ ] ) ;
59+ declarations. push ( StyleDeclaration {
60+ specificity : style. selectors . 0 . get ( index) . unwrap ( ) . specificity ( ) ,
61+ declaration : style. declarations . clone ( ) ,
62+ } ) ;
63+ }
4764 let elements = self . document . select ( selector. as_str ( ) ) ;
4865 for element in elements {
4966 let mut style_record = self . style_record . borrow_mut ( ) ;
@@ -64,26 +81,26 @@ impl<'i> Visitor<'i> for StyleVisitor<'i> {
6481
6582pub struct StyleParser < ' i > {
6683 pub style_record : Rc < RefCell < HashMap < SpanKey , Vec < StyleDeclaration < ' i > > > > > ,
84+ pub all_style : Rc < RefCell < HashMap < String , Vec < StyleDeclaration < ' i > > > > > ,
6785 pub document : & ' i JSXDocument ,
6886}
6987
7088impl < ' i > StyleParser < ' i > {
7189 pub fn new ( document : & ' i JSXDocument ) -> Self {
7290 StyleParser {
7391 style_record : Rc :: new ( RefCell :: new ( HashMap :: new ( ) ) ) ,
92+ all_style : Rc :: new ( RefCell :: new ( HashMap :: new ( ) ) ) ,
7493 document,
7594 }
7695 }
7796
7897 pub fn parse ( & mut self , css : & ' i str ) {
7998 let mut stylesheet = StyleSheet :: parse ( css, ParserOptions :: default ( ) ) . expect ( "解析样式失败" ) ;
80- let mut style_visitor = StyleVisitor :: new ( self . document , Rc :: clone ( & self . style_record ) ) ;
99+ let mut style_visitor = StyleVisitor :: new ( self . document , Rc :: clone ( & self . style_record ) , Rc :: clone ( & self . all_style ) ) ;
81100 stylesheet. visit ( & mut style_visitor) . unwrap ( ) ;
82101 }
83102
84- pub fn calc ( & self ) -> HashMap < SpanKey , StyleDeclaration < ' i > > {
85- // 遍历 style_record,计算每个节点的最终样式
86- let mut style_record = self . style_record . borrow_mut ( ) ;
103+ fn calc_style_record < T : Hash + Eq + Clone > ( & self , style_record : & mut HashMap < T , Vec < StyleDeclaration < ' i > > > ) -> HashMap < T , StyleDeclaration < ' i > > {
87104 let mut final_style_record = HashMap :: new ( ) ;
88105 for ( id, declarations) in style_record. iter_mut ( ) {
89106 declarations. sort_by ( |a, b| a. specificity . cmp ( & b. specificity ) ) ;
@@ -117,7 +134,7 @@ impl<'i> StyleParser<'i> {
117134 }
118135 }
119136 final_style_record. insert (
120- * id,
137+ ( * id) . clone ( ) ,
121138 StyleDeclaration {
122139 specificity : 0 ,
123140 declaration : DeclarationBlock {
@@ -129,4 +146,16 @@ impl<'i> StyleParser<'i> {
129146 }
130147 final_style_record
131148 }
149+
150+ pub fn calc ( & self ) -> StyleData < ' i > {
151+ // 遍历 style_record,计算每个节点的最终样式
152+ let mut style_record = self . style_record . borrow_mut ( ) ;
153+ let mut all_style = self . all_style . borrow_mut ( ) ;
154+ let final_style_record = self . calc_style_record ( & mut style_record) ;
155+ let final_all_style = self . calc_style_record ( & mut all_style) ;
156+ StyleData {
157+ style_record : Rc :: new ( RefCell :: new ( final_style_record) ) ,
158+ all_style : Rc :: new ( RefCell :: new ( final_all_style) ) ,
159+ }
160+ }
132161}
0 commit comments