@@ -170,45 +170,59 @@ fn get_useful_next(events: &[Events], pos: &mut usize) -> Option<Events> {
170
170
fn get_previous_positions ( events : & [ Events ] , mut pos : usize ) -> Vec < usize > {
171
171
let mut ret = Vec :: with_capacity ( 3 ) ;
172
172
173
- ret. push ( events[ pos] . get_pos ( ) - 1 ) ;
173
+ ret. push ( events[ pos] . get_pos ( ) ) ;
174
174
if pos > 0 {
175
175
pos -= 1 ;
176
176
}
177
177
loop {
178
- ret. push ( events[ pos] . get_pos ( ) ) ;
179
178
if pos < 1 || !events[ pos] . is_comment ( ) {
179
+ let x = events[ pos] . get_pos ( ) ;
180
+ if * ret. last ( ) . unwrap ( ) != x {
181
+ ret. push ( x) ;
182
+ } else {
183
+ ret. push ( 0 ) ;
184
+ }
180
185
break
181
186
}
187
+ ret. push ( events[ pos] . get_pos ( ) ) ;
182
188
pos -= 1 ;
183
189
}
184
- if events[ pos] . is_comment ( ) {
190
+ if ret . len ( ) & 1 != 0 && events[ pos] . is_comment ( ) {
185
191
ret. push ( 0 ) ;
186
192
}
187
193
ret. iter ( ) . rev ( ) . cloned ( ) . collect ( )
188
194
}
189
195
190
196
fn build_rule ( v : & [ u8 ] , positions : & [ usize ] ) -> String {
191
197
positions. chunks ( 2 )
192
- . map ( |x| :: std:: str:: from_utf8 ( & v[ x[ 0 ] ..x[ 1 ] ] ) . unwrap_or ( "" ) . to_owned ( ) )
198
+ . map ( |x| :: std:: str:: from_utf8 ( & v[ x[ 0 ] ..x[ 1 ] ] ) . unwrap_or ( "" ) )
193
199
. collect :: < String > ( )
194
200
. trim ( )
195
201
. replace ( "\n " , " " )
202
+ . replace ( "/" , "" )
203
+ . replace ( "\t " , " " )
204
+ . replace ( "{" , "" )
205
+ . replace ( "}" , "" )
206
+ . split ( " " )
207
+ . filter ( |s| s. len ( ) > 0 )
208
+ . collect :: < Vec < & str > > ( )
209
+ . join ( " " )
196
210
}
197
211
198
212
fn inner ( v : & [ u8 ] , events : & [ Events ] , pos : & mut usize ) -> HashSet < CssPath > {
199
- let mut pathes = Vec :: with_capacity ( 50 ) ;
213
+ let mut paths = Vec :: with_capacity ( 50 ) ;
200
214
201
215
while * pos < events. len ( ) {
202
216
if let Some ( Events :: OutBlock ( _) ) = get_useful_next ( events, pos) {
203
217
* pos += 1 ;
204
218
break
205
219
}
206
220
if let Some ( Events :: InBlock ( _) ) = get_useful_next ( events, pos) {
207
- pathes . push ( CssPath :: new ( build_rule ( v, & get_previous_positions ( events, * pos) ) ) ) ;
221
+ paths . push ( CssPath :: new ( build_rule ( v, & get_previous_positions ( events, * pos) ) ) ) ;
208
222
* pos += 1 ;
209
223
}
210
224
while let Some ( Events :: InBlock ( _) ) = get_useful_next ( events, pos) {
211
- if let Some ( ref mut path) = pathes . last_mut ( ) {
225
+ if let Some ( ref mut path) = paths . last_mut ( ) {
212
226
for entry in inner ( v, events, pos) . iter ( ) {
213
227
path. children . insert ( entry. clone ( ) ) ;
214
228
}
@@ -218,10 +232,10 @@ fn inner(v: &[u8], events: &[Events], pos: &mut usize) -> HashSet<CssPath> {
218
232
* pos += 1 ;
219
233
}
220
234
}
221
- pathes . iter ( ) . cloned ( ) . collect ( )
235
+ paths . iter ( ) . cloned ( ) . collect ( )
222
236
}
223
237
224
- pub fn load_css_pathes ( v : & [ u8 ] ) -> CssPath {
238
+ pub fn load_css_paths ( v : & [ u8 ] ) -> CssPath {
225
239
let events = load_css_events ( v) ;
226
240
let mut pos = 0 ;
227
241
@@ -264,9 +278,9 @@ pub fn test_theme_against<P: AsRef<Path>>(f: &P, against: &CssPath) -> (bool, Ve
264
278
let mut data = Vec :: with_capacity ( 1000 ) ;
265
279
266
280
try_something ! ( file. read_to_end( & mut data) , ( false , Vec :: new( ) ) ) ;
267
- let pathes = load_css_pathes ( & data) ;
281
+ let paths = load_css_paths ( & data) ;
268
282
let mut ret = Vec :: new ( ) ;
269
- get_differences ( against, & pathes , & mut ret) ;
283
+ get_differences ( against, & paths , & mut ret) ;
270
284
( true , ret)
271
285
}
272
286
@@ -317,8 +331,11 @@ rule gh i {}
317
331
rule j end {}
318
332
"# ;
319
333
320
- assert ! ( get_differences( & load_css_pathes( against. as_bytes( ) ) ,
321
- & load_css_pathes( text. as_bytes( ) ) ) . is_empty( ) ) ;
334
+ let mut ret = Vec :: new ( ) ;
335
+ get_differences ( & load_css_paths ( against. as_bytes ( ) ) ,
336
+ & load_css_paths ( text. as_bytes ( ) ) ,
337
+ & mut ret) ;
338
+ assert ! ( ret. is_empty( ) ) ;
322
339
}
323
340
324
341
#[ test]
330
347
c // sdf
331
348
d {}
332
349
"# ;
333
- let pathes = load_css_pathes ( text. as_bytes ( ) ) ;
334
- assert ! ( pathes . children. get( "a b c d" ) . is_some( ) ) ;
350
+ let paths = load_css_paths ( text. as_bytes ( ) ) ;
351
+ assert ! ( paths . children. get( & CssPath :: new ( "a b c d" . to_owned ( ) ) ) . is_some( ) ) ;
335
352
}
336
353
337
354
#[ test]
@@ -350,10 +367,13 @@ a {
350
367
}
351
368
"# ;
352
369
353
- let against = load_css_pathes ( y. as_bytes ( ) ) ;
354
- let other = load_css_pathes ( x. as_bytes ( ) ) ;
370
+ let against = load_css_paths ( y. as_bytes ( ) ) ;
371
+ let other = load_css_paths ( x. as_bytes ( ) ) ;
355
372
356
- assert ! ( get_differences( & against, & other) . is_empty( ) ) ;
357
- assert_eq ! ( get_differences( & other, & against) , vec![ " Missing \" c\" rule" . to_owned( ) ] )
373
+ let mut ret = Vec :: new ( ) ;
374
+ get_differences ( & against, & other, & mut ret) ;
375
+ assert ! ( ret. is_empty( ) ) ;
376
+ get_differences ( & other, & against, & mut ret) ;
377
+ assert_eq ! ( ret, vec![ " Missing \" c\" rule" . to_owned( ) ] ) ;
358
378
}
359
379
}
0 commit comments