Skip to content

Commit

Permalink
Fix too many parens in @supports serialization.
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonSapin committed Jan 9, 2017
1 parent dbc35f4 commit b5d9210
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 11 deletions.
26 changes: 15 additions & 11 deletions components/style/supports.rs
Expand Up @@ -44,7 +44,7 @@ impl SupportsCondition {
let (keyword, wrapper) = match input.next() {
Err(()) => {
// End of input
return Ok(SupportsCondition::Parenthesized(Box::new(in_parens)))
return Ok(in_parens)
}
Ok(Token::Ident(ident)) => {
match_ignore_ascii_case! { ident,
Expand All @@ -71,6 +71,9 @@ impl SupportsCondition {

/// https://drafts.csswg.org/css-conditional-3/#supports_condition_in_parens
fn parse_in_parens(input: &mut Parser) -> Result<SupportsCondition, ()> {
// Whitespace is normally taken care of in `Parser::next`,
// but we want to not include it in `pos` for the SupportsCondition::FutureSyntax cases.
while input.try(Parser::expect_whitespace).is_ok() {}
let pos = input.position();
match input.next()? {
Token::ParenthesisBlock => {
Expand Down Expand Up @@ -106,19 +109,20 @@ impl SupportsCondition {
/// supports_condition | declaration
/// https://drafts.csswg.org/css-conditional/#dom-css-supports-conditiontext-conditiontext
pub fn parse_condition_or_declaration(input: &mut Parser) -> Result<SupportsCondition, ()> {
input.try(SupportsCondition::parse).or_else(|()| {
if let Ok(condition) = input.try(SupportsCondition::parse) {
Ok(SupportsCondition::Parenthesized(Box::new(condition)))
} else {
Declaration::parse(input).map(SupportsCondition::Declaration)
})
}
}

impl ToCss for SupportsCondition {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result
where W: fmt::Write {
match *self {
SupportsCondition::Not(ref cond) => {
dest.write_str("not (")?;
cond.to_css(dest)?;
dest.write_str(")")
dest.write_str("not ")?;
cond.to_css(dest)
}
SupportsCondition::Parenthesized(ref cond) => {
dest.write_str("(")?;
Expand All @@ -132,9 +136,7 @@ impl ToCss for SupportsCondition {
dest.write_str(" and ")?;
}
first = false;
dest.write_str("(")?;
cond.to_css(dest)?;
dest.write_str(")")?;
}
Ok(())
}
Expand All @@ -145,13 +147,15 @@ impl ToCss for SupportsCondition {
dest.write_str(" or ")?;
}
first = false;
dest.write_str("(")?;
cond.to_css(dest)?;
dest.write_str(")")?;
}
Ok(())
}
SupportsCondition::Declaration(ref decl) => decl.to_css(dest),
SupportsCondition::Declaration(ref decl) => {
dest.write_str("(")?;
decl.to_css(dest)?;
dest.write_str(")")
}
SupportsCondition::FutureSyntax(ref s) => dest.write_str(&s),
}
}
Expand Down
20 changes: 20 additions & 0 deletions tests/unit/style/parsing/mod.rs
Expand Up @@ -39,6 +39,25 @@ macro_rules! assert_roundtrip_with_context {
}
}

macro_rules! assert_roundtrip {
($fun:expr, $string:expr) => {
assert_roundtrip!($fun, $string, $string);
};
($fun:expr,$input:expr, $output:expr) => {
let mut parser = Parser::new($input);
let parsed = $fun(&mut parser)
.expect(&format!("Failed to parse {}", $input));
let serialized = ToCss::to_css_string(&parsed);
assert_eq!(serialized, $output);

let mut parser = Parser::new(&serialized);
let re_parsed = $fun(&mut parser)
.expect(&format!("Failed to parse serialization {}", $input));
let re_serialized = ToCss::to_css_string(&re_parsed);
assert_eq!(serialized, re_serialized);
}
}

macro_rules! parse_longhand {
($name:ident, $s:expr) => {{
let url = ::servo_url::ServoUrl::parse("http://localhost").unwrap();
Expand All @@ -58,3 +77,4 @@ mod inherited_text;
mod mask;
mod position;
mod selectors;
mod supports;
14 changes: 14 additions & 0 deletions tests/unit/style/parsing/supports.rs
@@ -0,0 +1,14 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use cssparser::Parser;
use style::supports::SupportsCondition;
use style_traits::ToCss;

#[test]
fn test_supports_condition() {
assert_roundtrip!(SupportsCondition::parse, "(margin: 1px)");
assert_roundtrip!(SupportsCondition::parse, "not (--be: to be)");
assert_roundtrip!(SupportsCondition::parse, "(color: blue) and future-extension(4)");
}

0 comments on commit b5d9210

Please sign in to comment.