Skip to content

Commit 583b29f

Browse files
Handle comments in css selector and add tests
1 parent 649715d commit 583b29f

File tree

1 file changed

+80
-23
lines changed

1 file changed

+80
-23
lines changed

src/librustdoc/theme.rs

Lines changed: 80 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -167,37 +167,53 @@ fn get_useful_next(events: &[Events], pos: &mut usize) -> Option<Events> {
167167
None
168168
}
169169

170+
fn get_previous_positions(events: &[Events], mut pos: usize) -> Vec<usize> {
171+
let mut ret = Vec::with_capacity(3);
172+
173+
ret.push(events[pos].get_pos() - 1);
174+
if pos > 0 {
175+
pos -= 1;
176+
}
177+
loop {
178+
ret.push(events[pos].get_pos());
179+
if pos < 1 || !events[pos].is_comment() {
180+
break
181+
}
182+
pos -= 1;
183+
}
184+
if events[pos].is_comment() {
185+
ret.push(0);
186+
}
187+
ret.iter().rev().cloned().collect()
188+
}
189+
190+
fn build_rule(v: &[u8], positions: &[usize]) -> String {
191+
positions.chunks(2)
192+
.map(|x| ::std::str::from_utf8(&v[x[0]..x[1]]).unwrap_or("").to_owned())
193+
.collect::<String>()
194+
.trim()
195+
.replace("\n", " ")
196+
}
197+
170198
fn inner(v: &[u8], events: &[Events], pos: &mut usize) -> HashSet<CssPath> {
171199
let mut pathes = Vec::with_capacity(50);
172200

173201
while *pos < events.len() {
174202
if let Some(Events::OutBlock(_)) = get_useful_next(events, pos) {
175-
println!("00 => {:?}", events[*pos]);
176203
*pos += 1;
177204
break
178205
}
179-
println!("a => {:?}", events[*pos]);
180-
if let Some(Events::InBlock(start_pos)) = get_useful_next(events, pos) {
181-
println!("aa => {:?}", events[*pos]);
182-
pathes.push(CssPath::new(::std::str::from_utf8(if *pos > 0 {
183-
&v[events[*pos - 1].get_pos()..start_pos - 1]
184-
} else {
185-
&v[..start_pos]
186-
}).unwrap_or("").trim().to_owned()));
206+
if let Some(Events::InBlock(_)) = get_useful_next(events, pos) {
207+
pathes.push(CssPath::new(build_rule(v, &get_previous_positions(events, *pos))));
187208
*pos += 1;
188209
}
189-
println!("b => {:?}", events[*pos]);
190210
while let Some(Events::InBlock(_)) = get_useful_next(events, pos) {
191-
println!("bb => {:?}", events[*pos]);
192211
if let Some(ref mut path) = pathes.last_mut() {
193212
for entry in inner(v, events, pos).iter() {
194213
path.children.insert(entry.clone());
195214
}
196215
}
197216
}
198-
if *pos < events.len() {
199-
println!("c => {:?}", events[*pos]);
200-
}
201217
if let Some(Events::OutBlock(_)) = get_useful_next(events, pos) {
202218
*pos += 1;
203219
}
@@ -209,13 +225,12 @@ pub fn load_css_pathes(v: &[u8]) -> CssPath {
209225
let events = load_css_events(v);
210226
let mut pos = 0;
211227

212-
println!("\n======> {:?}", events);
213228
let mut parent = CssPath::new("parent".to_owned());
214229
parent.children = inner(v, &events, &mut pos);
215230
parent
216231
}
217232

218-
fn get_differences(against: &CssPath, other: &CssPath, v: &mut Vec<String>) {
233+
pub fn get_differences(against: &CssPath, other: &CssPath, v: &mut Vec<String>) {
219234
if against.name != other.name {
220235
return
221236
} else {
@@ -250,16 +265,18 @@ pub fn test_theme_against<P: AsRef<Path>>(f: &P, against: &CssPath) -> Vec<Strin
250265

251266
try_something!(file.read_to_end(&mut data), Vec::new());
252267
let pathes = load_css_pathes(&data);
253-
println!("========= {:?}", pathes);
254-
println!("========= {:?}", against);
255268
let mut ret = Vec::new();
256269
get_differences(against, &pathes, &mut ret);
257270
ret
258271
}
259272

260-
/*#[test]
261-
fn test_comments_in_rules() {
262-
let text = r#"
273+
#[cfg(test)]
274+
mod test {
275+
use super::*;
276+
277+
#[test]
278+
fn test_comments_in_rules() {
279+
let text = r#"
263280
rule a {}
264281
265282
rule b, c
@@ -284,6 +301,46 @@ rule j/*commeeeeent
284301
285302
you like things like "{}" in there? :)
286303
*/
287-
end {}
304+
end {}"#;
305+
306+
let against = r#"
307+
rule a {}
308+
309+
rule b, c {}
310+
311+
rule d e {}
312+
313+
rule f {}
314+
315+
rule gh i {}
316+
317+
rule j end {}
318+
"#;
319+
320+
assert!(get_differences(&load_css_pathes(against.as_bytes()),
321+
&load_css_pathes(text.as_bytes())).is_empty());
322+
}
323+
324+
#[test]
325+
fn test_comparison() {
326+
let x = r#"
327+
a {
328+
b {
329+
c {}
330+
}
331+
}
332+
"#;
333+
334+
let y = r#"
335+
a {
336+
b {}
337+
}
288338
"#;
289-
}*/
339+
340+
let against = load_css_pathes(y.as_bytes());
341+
let other = load_css_pathes(x.as_bytes());
342+
343+
assert!(get_differences(&against, &other).is_empty());
344+
assert_eq!(get_differences(&other, &against), vec![" Missing \"c\" rule".to_owned()])
345+
}
346+
}

0 commit comments

Comments
 (0)