@@ -11,7 +11,11 @@ use super::{Brush, RangedStyle, ResolvedProperty, ResolvedStyle};
1111
1212#[ derive( Debug , Clone ) ]
1313struct 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
1519 style : ResolvedStyle < B > ,
1620}
1721
@@ -28,6 +32,9 @@ pub(crate) struct TreeStyleBuilder<B: Brush> {
2832}
2933
3034impl < B : Brush > TreeStyleBuilder < B > {
35+ fn current_span_id ( & self ) -> u64 {
36+ self . tree [ self . current_span ] . id
37+ }
3138 fn current_style ( & self ) -> ResolvedStyle < B > {
3239 self . tree [ self . current_span ] . style . clone ( )
3340 }
@@ -49,15 +56,16 @@ impl<B: Brush> Default for TreeStyleBuilder<B> {
4956
5057impl < B : Brush > TreeStyleBuilder < B > {
5158 /// 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 > ) {
5360 self . tree . clear ( ) ;
5461 self . flatted_styles . clear ( ) ;
5562 self . white_space_collapse = WhiteSpaceCollapse :: Preserve ;
5663 self . text . clear ( ) ;
5764 self . uncommitted_text . clear ( ) ;
5865
5966 self . tree . push ( StyleTreeNode {
60- parent : None ,
67+ id,
68+ parent_idx : None ,
6169 style : root_style,
6270 } ) ;
6371 self . current_span = 0 ;
@@ -118,8 +126,9 @@ impl<B: Brush> TreeStyleBuilder<B> {
118126 }
119127
120128 let range = self . text . len ( ) ..( self . text . len ( ) + span_text. len ( ) ) ;
129+ let id = self . current_span_id ( ) ;
121130 let style = self . current_style ( ) ;
122- self . flatted_styles . push ( RangedStyle { style, range } ) ;
131+ self . flatted_styles . push ( RangedStyle { id , style, range } ) ;
123132 self . text . push_str ( span_text) ;
124133 self . uncommitted_text . clear ( ) ;
125134 self . is_span_first = false ;
@@ -129,11 +138,12 @@ impl<B: Brush> TreeStyleBuilder<B> {
129138 self . text . len ( )
130139 }
131140
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 > ) {
133142 self . push_uncommitted_text ( false ) ;
134143
135144 self . tree . push ( StyleTreeNode {
136- parent : Some ( self . current_span ) ,
145+ id,
146+ parent_idx : Some ( self . current_span ) ,
137147 style,
138148 } ) ;
139149 self . current_span = self . tree . len ( ) - 1 ;
@@ -142,20 +152,21 @@ impl<B: Brush> TreeStyleBuilder<B> {
142152
143153 pub ( crate ) fn push_style_modification_span (
144154 & mut self ,
155+ id : u64 ,
145156 properties : impl Iterator < Item = ResolvedProperty < B > > ,
146157 ) {
147158 let mut style = self . current_style ( ) ;
148159 for prop in properties {
149160 style. apply ( prop. clone ( ) ) ;
150161 }
151- self . push_style_span ( style) ;
162+ self . push_style_span ( id , style) ;
152163 }
153164
154165 pub ( crate ) fn pop_style_span ( & mut self ) {
155166 self . push_uncommitted_text ( true ) ;
156167
157168 self . current_span = self . tree [ self . current_span ]
158- . parent
169+ . parent_idx
159170 . expect ( "Popped root style" ) ;
160171 }
161172
@@ -168,7 +179,7 @@ impl<B: Brush> TreeStyleBuilder<B> {
168179
169180 /// Computes the sequence of ranged styles.
170181 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 ( ) {
172183 self . pop_style_span ( ) ;
173184 }
174185
0 commit comments