@@ -170,45 +170,59 @@ fn get_useful_next(events: &[Events], pos: &mut usize) -> Option<Events> {
170170fn get_previous_positions ( events : & [ Events ] , mut pos : usize ) -> Vec < usize > {
171171 let mut ret = Vec :: with_capacity ( 3 ) ;
172172
173- ret. push ( events[ pos] . get_pos ( ) - 1 ) ;
173+ ret. push ( events[ pos] . get_pos ( ) ) ;
174174 if pos > 0 {
175175 pos -= 1 ;
176176 }
177177 loop {
178- ret. push ( events[ pos] . get_pos ( ) ) ;
179178 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+ }
180185 break
181186 }
187+ ret. push ( events[ pos] . get_pos ( ) ) ;
182188 pos -= 1 ;
183189 }
184- if events[ pos] . is_comment ( ) {
190+ if ret . len ( ) & 1 != 0 && events[ pos] . is_comment ( ) {
185191 ret. push ( 0 ) ;
186192 }
187193 ret. iter ( ) . rev ( ) . cloned ( ) . collect ( )
188194}
189195
190196fn build_rule ( v : & [ u8 ] , positions : & [ usize ] ) -> String {
191197 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 ( "" ) )
193199 . collect :: < String > ( )
194200 . trim ( )
195201 . 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 ( " " )
196210}
197211
198212fn 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 ) ;
200214
201215 while * pos < events. len ( ) {
202216 if let Some ( Events :: OutBlock ( _) ) = get_useful_next ( events, pos) {
203217 * pos += 1 ;
204218 break
205219 }
206220 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) ) ) ) ;
208222 * pos += 1 ;
209223 }
210224 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 ( ) {
212226 for entry in inner ( v, events, pos) . iter ( ) {
213227 path. children . insert ( entry. clone ( ) ) ;
214228 }
@@ -218,10 +232,10 @@ fn inner(v: &[u8], events: &[Events], pos: &mut usize) -> HashSet<CssPath> {
218232 * pos += 1 ;
219233 }
220234 }
221- pathes . iter ( ) . cloned ( ) . collect ( )
235+ paths . iter ( ) . cloned ( ) . collect ( )
222236}
223237
224- pub fn load_css_pathes ( v : & [ u8 ] ) -> CssPath {
238+ pub fn load_css_paths ( v : & [ u8 ] ) -> CssPath {
225239 let events = load_css_events ( v) ;
226240 let mut pos = 0 ;
227241
@@ -264,9 +278,9 @@ pub fn test_theme_against<P: AsRef<Path>>(f: &P, against: &CssPath) -> (bool, Ve
264278 let mut data = Vec :: with_capacity ( 1000 ) ;
265279
266280 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) ;
268282 let mut ret = Vec :: new ( ) ;
269- get_differences ( against, & pathes , & mut ret) ;
283+ get_differences ( against, & paths , & mut ret) ;
270284 ( true , ret)
271285}
272286
@@ -317,8 +331,11 @@ rule gh i {}
317331rule j end {}
318332"# ;
319333
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( ) ) ;
322339 }
323340
324341 #[ test]
330347c // sdf
331348d {}
332349"# ;
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( ) ) ;
335352 }
336353
337354 #[ test]
@@ -350,10 +367,13 @@ a {
350367}
351368"# ;
352369
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 ( ) ) ;
355372
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( ) ] ) ;
358378 }
359379}
0 commit comments