@@ -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 ;
@@ -118,8 +126,9 @@ impl<B: Brush> TreeStyleBuilder<B> {
118
126
}
119
127
120
128
let range = self . text . len ( ) ..( self . text . len ( ) + span_text. len ( ) ) ;
129
+ let id = self . current_span_id ( ) ;
121
130
let style = self . current_style ( ) ;
122
- self . flatted_styles . push ( RangedStyle { style, range } ) ;
131
+ self . flatted_styles . push ( RangedStyle { id , style, range } ) ;
123
132
self . text . push_str ( span_text) ;
124
133
self . uncommitted_text . clear ( ) ;
125
134
self . is_span_first = false ;
@@ -129,11 +138,12 @@ impl<B: Brush> TreeStyleBuilder<B> {
129
138
self . text . len ( )
130
139
}
131
140
132
- pub ( crate ) fn push_style_span ( & mut self , style : ResolvedStyle < B > ) {
141
+ pub ( crate ) fn push_style_span ( & mut self , id : u64 , style : ResolvedStyle < B > ) {
133
142
self . push_uncommitted_text ( false ) ;
134
143
135
144
self . tree . push ( StyleTreeNode {
136
- parent : Some ( self . current_span ) ,
145
+ id,
146
+ parent_idx : Some ( self . current_span ) ,
137
147
style,
138
148
} ) ;
139
149
self . current_span = self . tree . len ( ) - 1 ;
@@ -142,20 +152,21 @@ impl<B: Brush> TreeStyleBuilder<B> {
142
152
143
153
pub ( crate ) fn push_style_modification_span (
144
154
& mut self ,
155
+ id : u64 ,
145
156
properties : impl Iterator < Item = ResolvedProperty < B > > ,
146
157
) {
147
158
let mut style = self . current_style ( ) ;
148
159
for prop in properties {
149
160
style. apply ( prop. clone ( ) ) ;
150
161
}
151
- self . push_style_span ( style) ;
162
+ self . push_style_span ( id , style) ;
152
163
}
153
164
154
165
pub ( crate ) fn pop_style_span ( & mut self ) {
155
166
self . push_uncommitted_text ( true ) ;
156
167
157
168
self . current_span = self . tree [ self . current_span ]
158
- . parent
169
+ . parent_idx
159
170
. expect ( "Popped root style" ) ;
160
171
}
161
172
@@ -168,7 +179,7 @@ impl<B: Brush> TreeStyleBuilder<B> {
168
179
169
180
/// Computes the sequence of ranged styles.
170
181
pub ( crate ) fn finish ( & mut self , styles : & mut Vec < RangedStyle < B > > ) -> String {
171
- while self . tree [ self . current_span ] . parent . is_some ( ) {
182
+ while self . tree [ self . current_span ] . parent_idx . is_some ( ) {
172
183
self . pop_style_span ( ) ;
173
184
}
174
185
0 commit comments