Skip to content

Commit d3529ce

Browse files
Correctly handle parens
1 parent a528f68 commit d3529ce

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

src/librustdoc/theme.rs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,31 @@ pub(crate) struct CssPath {
1717
}
1818

1919
/// When encountering a `"` or a `'`, returns the whole string, including the quote characters.
20-
fn get_string(iter: &mut Peekable<Chars<'_>>, string_start: char) -> String {
21-
let mut s = String::with_capacity(2);
22-
23-
s.push(string_start);
20+
fn get_string(iter: &mut Peekable<Chars<'_>>, string_start: char, buffer: &mut String) {
21+
buffer.push(string_start);
2422
while let Some(c) = iter.next() {
25-
s.push(c);
23+
buffer.push(c);
2624
if c == '\\' {
2725
iter.next();
2826
} else if c == string_start {
2927
break;
3028
}
3129
}
32-
s
30+
}
31+
32+
fn get_inside_paren(
33+
iter: &mut Peekable<Chars<'_>>,
34+
paren_start: char,
35+
paren_end: char,
36+
buffer: &mut String,
37+
) {
38+
buffer.push(paren_start);
39+
while let Some(c) = iter.next() {
40+
handle_common_chars(c, buffer, iter);
41+
if c == paren_end {
42+
break;
43+
}
44+
}
3345
}
3446

3547
/// Skips a `/*` comment.
@@ -52,9 +64,11 @@ fn skip_line_comment(iter: &mut Peekable<Chars<'_>>) {
5264

5365
fn handle_common_chars(c: char, buffer: &mut String, iter: &mut Peekable<Chars<'_>>) {
5466
match c {
55-
'"' | '\'' => buffer.push_str(&get_string(iter, c)),
67+
'"' | '\'' => get_string(iter, c, buffer),
5668
'/' if iter.peek() == Some(&'*') => skip_comment(iter),
5769
'/' if iter.peek() == Some(&'/') => skip_line_comment(iter),
70+
'(' => get_inside_paren(iter, c, ')', buffer),
71+
'[' => get_inside_paren(iter, c, ']', buffer),
5872
_ => buffer.push(c),
5973
}
6074
}

src/librustdoc/theme/tests.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ fn test_media() {
138138
assert!(p.children.get("a:hover").is_some());
139139
assert!(p.children.get("b").is_some());
140140

141-
eprintln!("{:?}", paths);
142141
let p = paths.get("@media (max-width:1001px)");
143142
assert!(p.is_some());
144143
let p = p.unwrap();
@@ -169,3 +168,20 @@ fn test_css_variables() {
169168
get_differences(&other, &against, &mut ret);
170169
assert_eq!(ret, vec![" Missing CSS variable `--b` in `:root`".to_owned()]);
171170
}
171+
172+
#[test]
173+
fn test_weird_rule_value() {
174+
let x = r#"
175+
a[text=("a")] {
176+
b: url({;}.png);
177+
c: #fff
178+
}
179+
"#;
180+
181+
let paths = load_css_paths(&x).unwrap();
182+
let p = paths.get("a[text=(\"a\")]");
183+
assert!(p.is_some());
184+
let p = p.unwrap();
185+
assert_eq!(p.rules.get("b"), Some(&"url({;}.png)".to_owned()));
186+
assert_eq!(p.rules.get("c"), Some(&"#fff".to_owned()));
187+
}

0 commit comments

Comments
 (0)