Skip to content

Commit

Permalink
Auto merge of #17792 - upsuper:supports-any-value, r=SimonSapin
Browse files Browse the repository at this point in the history
Fix supports rule parsing issues with <any-value>

This eventually fixes #15482, as well as several reftests in mozilla-central which were added for [bug 883987](https://bugzilla.mozilla.org/show_bug.cgi?id=883987).

The new function should probably be moved into cssparser crate at some point.

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/17792)
<!-- Reviewable:end -->
  • Loading branch information
bors-servo committed Jul 20, 2017
2 parents eba573d + 5eb0613 commit e19fefc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 25 deletions.
41 changes: 16 additions & 25 deletions components/style/stylesheets/supports_rule.rs
Expand Up @@ -128,21 +128,18 @@ impl SupportsCondition {
let pos = input.position();
match input.next()? {
Token::ParenthesisBlock => {
input.parse_nested_block(|input| {
// `input.try()` not needed here since the alternative uses `consume_all()`.
parse_condition_or_declaration(input).or_else(|_| {
consume_all(input);
Ok(SupportsCondition::FutureSyntax(input.slice_from(pos).to_owned()))
})
})
}
Token::Function(_) => {
let result: Result<_, ParseError> = input.parse_nested_block(|i| Ok(consume_all(i)));
result.unwrap();
Ok(SupportsCondition::FutureSyntax(input.slice_from(pos).to_owned()))
let nested = input.try(|input| {
input.parse_nested_block(|i| parse_condition_or_declaration(i))
});
if nested.is_ok() {
return nested;
}
}
t => Err(CssParseError::Basic(BasicParseError::UnexpectedToken(t)))
Token::Function(_) => {}
t => return Err(CssParseError::Basic(BasicParseError::UnexpectedToken(t))),
}
input.parse_nested_block(|i| consume_any_value(i))?;
Ok(SupportsCondition::FutureSyntax(input.slice_from(pos).to_owned()))
}

/// Evaluate a supports condition
Expand Down Expand Up @@ -235,25 +232,19 @@ impl ToCss for Declaration {
}
}

/// Slurps up input till exhausted, return string from source position
fn parse_anything(input: &mut Parser) -> String {
let pos = input.position();
consume_all(input);
input.slice_from(pos).to_owned()
}

/// Consume input till done
fn consume_all(input: &mut Parser) {
while let Ok(_) = input.next() {}
/// https://drafts.csswg.org/css-syntax-3/#typedef-any-value
fn consume_any_value<'i, 't>(input: &mut Parser<'i, 't>) -> Result<(), ParseError<'i>> {
input.expect_no_error_token().map_err(|err| err.into())
}

impl Declaration {
/// Parse a declaration
pub fn parse<'i, 't>(input: &mut Parser<'i, 't>) -> Result<Declaration, ParseError<'i>> {
let prop = input.expect_ident()?.into_owned();
input.expect_colon()?;
let val = parse_anything(input);
Ok(Declaration { prop: prop, val: val })
let pos = input.position();
consume_any_value(input)?;
Ok(Declaration { prop: prop, val: input.slice_from(pos).to_owned() })
}

/// Determine if a declaration parses
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/style/parsing/supports.rs
Expand Up @@ -11,4 +11,6 @@ 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)");
assert_roundtrip!(SupportsCondition::parse, "future-\\1 extension(4)");
assert_roundtrip!(SupportsCondition::parse, "((test))");
}

0 comments on commit e19fefc

Please sign in to comment.