1
- use std:: { cell:: RefCell , collections:: HashMap , convert:: Infallible , rc:: Rc } ;
1
+ use std:: { cell:: RefCell , collections:: HashMap , convert:: Infallible , rc:: Rc , hash :: Hash } ;
2
2
3
3
use lightningcss:: {
4
4
declaration:: DeclarationBlock ,
@@ -11,6 +11,11 @@ use lightningcss::{
11
11
12
12
use crate :: { document:: JSXDocument , visitor:: SpanKey } ;
13
13
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
+
14
19
#[ derive( Debug , Clone ) ]
15
20
pub struct StyleDeclaration < ' i > {
16
21
pub specificity : u32 ,
@@ -19,16 +24,19 @@ pub struct StyleDeclaration<'i> {
19
24
20
25
pub struct StyleVisitor < ' i > {
21
26
pub style_record : Rc < RefCell < HashMap < SpanKey , Vec < StyleDeclaration < ' i > > > > > ,
27
+ pub all_style : Rc < RefCell < HashMap < String , Vec < StyleDeclaration < ' i > > > > > ,
22
28
pub document : & ' i JSXDocument ,
23
29
}
24
30
25
31
impl < ' i > StyleVisitor < ' i > {
26
32
pub fn new (
27
33
document : & ' i JSXDocument ,
28
34
style_record : Rc < RefCell < HashMap < SpanKey , Vec < StyleDeclaration < ' i > > > > > ,
35
+ all_style : Rc < RefCell < HashMap < String , Vec < StyleDeclaration < ' i > > > > > ,
29
36
) -> Self {
30
37
StyleVisitor {
31
38
style_record,
39
+ all_style,
32
40
document,
33
41
}
34
42
}
@@ -44,6 +52,15 @@ impl<'i> Visitor<'i> for StyleVisitor<'i> {
44
52
let selectors = selectors_str. split ( "," ) . collect :: < Vec < & str > > ( ) ;
45
53
for index in 0 ..selectors. len ( ) {
46
54
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
+ }
47
64
let elements = self . document . select ( selector. as_str ( ) ) ;
48
65
for element in elements {
49
66
let mut style_record = self . style_record . borrow_mut ( ) ;
@@ -64,26 +81,26 @@ impl<'i> Visitor<'i> for StyleVisitor<'i> {
64
81
65
82
pub struct StyleParser < ' i > {
66
83
pub style_record : Rc < RefCell < HashMap < SpanKey , Vec < StyleDeclaration < ' i > > > > > ,
84
+ pub all_style : Rc < RefCell < HashMap < String , Vec < StyleDeclaration < ' i > > > > > ,
67
85
pub document : & ' i JSXDocument ,
68
86
}
69
87
70
88
impl < ' i > StyleParser < ' i > {
71
89
pub fn new ( document : & ' i JSXDocument ) -> Self {
72
90
StyleParser {
73
91
style_record : Rc :: new ( RefCell :: new ( HashMap :: new ( ) ) ) ,
92
+ all_style : Rc :: new ( RefCell :: new ( HashMap :: new ( ) ) ) ,
74
93
document,
75
94
}
76
95
}
77
96
78
97
pub fn parse ( & mut self , css : & ' i str ) {
79
98
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 ) ) ;
81
100
stylesheet. visit ( & mut style_visitor) . unwrap ( ) ;
82
101
}
83
102
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 > > {
87
104
let mut final_style_record = HashMap :: new ( ) ;
88
105
for ( id, declarations) in style_record. iter_mut ( ) {
89
106
declarations. sort_by ( |a, b| a. specificity . cmp ( & b. specificity ) ) ;
@@ -117,7 +134,7 @@ impl<'i> StyleParser<'i> {
117
134
}
118
135
}
119
136
final_style_record. insert (
120
- * id,
137
+ ( * id) . clone ( ) ,
121
138
StyleDeclaration {
122
139
specificity : 0 ,
123
140
declaration : DeclarationBlock {
@@ -129,4 +146,16 @@ impl<'i> StyleParser<'i> {
129
146
}
130
147
final_style_record
131
148
}
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
+ }
132
161
}
0 commit comments