Strip whitespace inside pseudo paren selectors

This commit is contained in:
ConnorSkees 2020-02-02 15:21:47 -05:00
parent ee44982198
commit 9c97c734ef
2 changed files with 16 additions and 7 deletions

View File

@ -88,7 +88,7 @@ pub(crate) enum SelectorKind {
/// Pseudo element selector: `::before` /// Pseudo element selector: `::before`
PseudoElement(String), PseudoElement(String),
/// Pseudo selector with additional parens: `:any(h1, h2, h3, h4, h5, h6)` /// Pseudo selector with additional parens: `:any(h1, h2, h3, h4, h5, h6)`
PseudoParen(String, Vec<TokenKind>), PseudoParen(String, String),
/// Use the super selector: `&.red` /// Use the super selector: `&.red`
Super, Super,
/// Super selector in an interpolated context: `a #{&}` /// Super selector in an interpolated context: `a #{&}`
@ -127,11 +127,11 @@ impl Display for SelectorKind {
SelectorKind::Attribute(attr) => write!(f, "{}", attr), SelectorKind::Attribute(attr) => write!(f, "{}", attr),
SelectorKind::Pseudo(s) => write!(f, ":{}", s), SelectorKind::Pseudo(s) => write!(f, ":{}", s),
SelectorKind::PseudoElement(s) => write!(f, "::{}", s), SelectorKind::PseudoElement(s) => write!(f, "::{}", s),
SelectorKind::PseudoParen(s, toks) => write!( SelectorKind::PseudoParen(s, val) => write!(
f, f,
":{}({})", ":{}({})",
s, s,
toks.iter().map(ToString::to_string).collect::<String>() val
), ),
SelectorKind::Super | SelectorKind::None | SelectorKind::InterpolatedSuper => { SelectorKind::Super | SelectorKind::None | SelectorKind::InterpolatedSuper => {
write!(f, "") write!(f, "")
@ -226,16 +226,20 @@ impl<'a> SelectorParser<'a> {
}) = tokens.peek() }) = tokens.peek()
{ {
tokens.next(); tokens.next();
let mut toks = Vec::new(); devour_whitespace_or_comment(tokens);
let mut toks = String::new();
while let Some(Token { kind, .. }) = tokens.peek() { while let Some(Token { kind, .. }) = tokens.peek() {
if kind == &TokenKind::Symbol(Symbol::CloseParen) { if kind == &TokenKind::Symbol(Symbol::CloseParen) {
tokens.next();
break; break;
} }
let tok = tokens.next().unwrap(); let tok = tokens.next().unwrap();
toks.push(tok.kind); toks.push_str(&tok.kind.to_string());
if devour_whitespace(tokens) {
toks.push(' ');
}
} }
tokens.next(); self.selectors.push(SelectorKind::PseudoParen(s, toks.trim_end().to_owned()))
self.selectors.push(SelectorKind::PseudoParen(s, toks))
} else { } else {
self.selectors.push(SelectorKind::Pseudo(s)) self.selectors.push(SelectorKind::Pseudo(s))
} }

View File

@ -127,6 +127,11 @@ test!(
selector_pseudo_paren_space, selector_pseudo_paren_space,
":pseudo(a b c) {\n color: red;\n}\n" ":pseudo(a b c) {\n color: red;\n}\n"
); );
test!(
selector_pseudo_paren_whitespacespace,
":pseudo( -2n+1 ) {\n color: red;\n}\n",
":pseudo(-2n+1) {\n color: red;\n}\n"
);
test!( test!(
selector_el_pseudo_paren_and, selector_el_pseudo_paren_and,
"a:pseudo(a, b, c) {\n color: red;\n}\n" "a:pseudo(a, b, c) {\n color: red;\n}\n"