From 9c97c734ef0239aabd9261deba7e68eced491326 Mon Sep 17 00:00:00 2001 From: ConnorSkees <39542938+ConnorSkees@users.noreply.github.com> Date: Sun, 2 Feb 2020 15:21:47 -0500 Subject: [PATCH] Strip whitespace inside pseudo paren selectors --- src/selector.rs | 18 +++++++++++------- tests/selectors.rs | 5 +++++ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/selector.rs b/src/selector.rs index 6d531cd..2800e2c 100644 --- a/src/selector.rs +++ b/src/selector.rs @@ -88,7 +88,7 @@ pub(crate) enum SelectorKind { /// Pseudo element selector: `::before` PseudoElement(String), /// Pseudo selector with additional parens: `:any(h1, h2, h3, h4, h5, h6)` - PseudoParen(String, Vec), + PseudoParen(String, String), /// Use the super selector: `&.red` Super, /// Super selector in an interpolated context: `a #{&}` @@ -127,11 +127,11 @@ impl Display for SelectorKind { SelectorKind::Attribute(attr) => write!(f, "{}", attr), SelectorKind::Pseudo(s) => write!(f, ":{}", s), SelectorKind::PseudoElement(s) => write!(f, "::{}", s), - SelectorKind::PseudoParen(s, toks) => write!( + SelectorKind::PseudoParen(s, val) => write!( f, ":{}({})", s, - toks.iter().map(ToString::to_string).collect::() + val ), SelectorKind::Super | SelectorKind::None | SelectorKind::InterpolatedSuper => { write!(f, "") @@ -226,16 +226,20 @@ impl<'a> SelectorParser<'a> { }) = tokens.peek() { tokens.next(); - let mut toks = Vec::new(); + devour_whitespace_or_comment(tokens); + let mut toks = String::new(); while let Some(Token { kind, .. }) = tokens.peek() { if kind == &TokenKind::Symbol(Symbol::CloseParen) { + tokens.next(); break; } 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)) + self.selectors.push(SelectorKind::PseudoParen(s, toks.trim_end().to_owned())) } else { self.selectors.push(SelectorKind::Pseudo(s)) } diff --git a/tests/selectors.rs b/tests/selectors.rs index 3df4451..4f6adf2 100644 --- a/tests/selectors.rs +++ b/tests/selectors.rs @@ -127,6 +127,11 @@ test!( selector_pseudo_paren_space, ":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!( selector_el_pseudo_paren_and, "a:pseudo(a, b, c) {\n color: red;\n}\n"