@@ -11,7 +11,11 @@ use super::{Brush, RangedStyle, ResolvedProperty, ResolvedStyle};
11
11
12
12
#[ derive( Debug , Clone ) ]
13
13
struct StyleTreeNode < B : Brush > {
14
- parent : Option < usize > ,
14
+ // An external id provided by the user
15
+ id : u64 ,
16
+ // The index of the parent in the tree Vec (*not* the id of the parent node)
17
+ parent_idx : Option < usize > ,
18
+ // The styles associated with the node
15
19
style : ResolvedStyle < B > ,
16
20
}
17
21
@@ -28,6 +32,9 @@ pub(crate) struct TreeStyleBuilder<B: Brush> {
28
32
}
29
33
30
34
impl < B : Brush > TreeStyleBuilder < B > {
35
+ fn current_span_id ( & self ) -> u64 {
36
+ self . tree [ self . current_span ] . id
37
+ }
31
38
fn current_style ( & self ) -> ResolvedStyle < B > {
32
39
self . tree [ self . current_span ] . style . clone ( )
33
40
}
@@ -49,15 +56,16 @@ impl<B: Brush> Default for TreeStyleBuilder<B> {
49
56
50
57
impl < B : Brush > TreeStyleBuilder < B > {
51
58
/// Prepares the builder for accepting a style tree for text of the specified length.
52
- pub ( crate ) fn begin ( & mut self , root_style : ResolvedStyle < B > ) {
59
+ pub ( crate ) fn begin ( & mut self , id : u64 , root_style : ResolvedStyle < B > ) {
53
60
self . tree . clear ( ) ;
54
61
self . flatted_styles . clear ( ) ;
55
62
self . white_space_collapse = WhiteSpaceCollapse :: Preserve ;
56
63
self . text . clear ( ) ;
57
64
self . uncommitted_text . clear ( ) ;
58
65
59
66
self . tree . push ( StyleTreeNode {
60
- parent : None ,
67
+ id,
68
+ parent_idx : None ,
61
69
style : root_style,
62
70
} ) ;
63
71
self . current_span = 0 ;
@@ -113,8 +121,9 @@ impl<B: Brush> TreeStyleBuilder<B> {
113
121
}
114
122
115
123
let range = self . text . len ( ) ..( self . text . len ( ) + span_text. len ( ) ) ;
124
+ let id = self . current_span_id ( ) ;
116
125
let style = self . current_style ( ) ;
117
- self . flatted_styles . push ( RangedStyle { style, range } ) ;
126
+ self . flatted_styles . push ( RangedStyle { id , style, range } ) ;
118
127
self . text . push_str ( span_text) ;
119
128
self . uncommitted_text . clear ( ) ;
120
129
self . is_span_first = false ;
@@ -124,11 +133,12 @@ impl<B: Brush> TreeStyleBuilder<B> {
124
133
self . text . len ( )
125
134
}
126
135
127
- pub ( crate ) fn push_style_span ( & mut self , style : ResolvedStyle < B > ) {
136
+ pub ( crate ) fn push_style_span ( & mut self , id : u64 , style : ResolvedStyle < B > ) {
128
137
self . push_uncommitted_text ( false ) ;
129
138
130
139
self . tree . push ( StyleTreeNode {
131
- parent : Some ( self . current_span ) ,
140
+ id,
141
+ parent_idx : Some ( self . current_span ) ,
132
142
style,
133
143
} ) ;
134
144
self . current_span = self . tree . len ( ) - 1 ;
@@ -137,20 +147,21 @@ impl<B: Brush> TreeStyleBuilder<B> {
137
147
138
148
pub ( crate ) fn push_style_modification_span (
139
149
& mut self ,
150
+ id : u64 ,
140
151
properties : impl Iterator < Item = ResolvedProperty < B > > ,
141
152
) {
142
153
let mut style = self . current_style ( ) ;
143
154
for prop in properties {
144
155
style. apply ( prop. clone ( ) ) ;
145
156
}
146
- self . push_style_span ( style) ;
157
+ self . push_style_span ( id , style) ;
147
158
}
148
159
149
160
pub ( crate ) fn pop_style_span ( & mut self ) {
150
161
self . push_uncommitted_text ( true ) ;
151
162
152
163
self . current_span = self . tree [ self . current_span ]
153
- . parent
164
+ . parent_idx
154
165
. expect ( "Popped root style" ) ;
155
166
}
156
167
@@ -163,7 +174,7 @@ impl<B: Brush> TreeStyleBuilder<B> {
163
174
164
175
/// Computes the sequence of ranged styles.
165
176
pub ( crate ) fn finish ( & mut self , styles : & mut Vec < RangedStyle < B > > ) -> String {
166
- while self . tree [ self . current_span ] . parent . is_some ( ) {
177
+ while self . tree [ self . current_span ] . parent_idx . is_some ( ) {
167
178
self . pop_style_span ( ) ;
168
179
}
169
180
0 commit comments