Auto merge of #17792 - upsuper:supports-any-value, r=SimonSapin

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 -->
This commit is contained in:
bors-servo 2017-07-20 13:08:58 -07:00 committed by GitHub
commit e19fefcb47
2 changed files with 18 additions and 25 deletions

View file

@ -128,21 +128,18 @@ impl SupportsCondition {
let pos = input.position(); let pos = input.position();
match input.next()? { match input.next()? {
Token::ParenthesisBlock => { Token::ParenthesisBlock => {
input.parse_nested_block(|input| { let nested = input.try(|input| {
// `input.try()` not needed here since the alternative uses `consume_all()`. input.parse_nested_block(|i| parse_condition_or_declaration(i))
parse_condition_or_declaration(input).or_else(|_| { });
consume_all(input); if nested.is_ok() {
Ok(SupportsCondition::FutureSyntax(input.slice_from(pos).to_owned())) return nested;
}) }
})
} }
Token::Function(_) => { Token::Function(_) => {}
let result: Result<_, ParseError> = input.parse_nested_block(|i| Ok(consume_all(i))); t => return Err(CssParseError::Basic(BasicParseError::UnexpectedToken(t))),
result.unwrap();
Ok(SupportsCondition::FutureSyntax(input.slice_from(pos).to_owned()))
}
t => 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 /// Evaluate a supports condition
@ -235,16 +232,9 @@ impl ToCss for Declaration {
} }
} }
/// Slurps up input till exhausted, return string from source position /// https://drafts.csswg.org/css-syntax-3/#typedef-any-value
fn parse_anything(input: &mut Parser) -> String { fn consume_any_value<'i, 't>(input: &mut Parser<'i, 't>) -> Result<(), ParseError<'i>> {
let pos = input.position(); input.expect_no_error_token().map_err(|err| err.into())
consume_all(input);
input.slice_from(pos).to_owned()
}
/// Consume input till done
fn consume_all(input: &mut Parser) {
while let Ok(_) = input.next() {}
} }
impl Declaration { impl Declaration {
@ -252,8 +242,9 @@ impl Declaration {
pub fn parse<'i, 't>(input: &mut Parser<'i, 't>) -> Result<Declaration, ParseError<'i>> { pub fn parse<'i, 't>(input: &mut Parser<'i, 't>) -> Result<Declaration, ParseError<'i>> {
let prop = input.expect_ident()?.into_owned(); let prop = input.expect_ident()?.into_owned();
input.expect_colon()?; input.expect_colon()?;
let val = parse_anything(input); let pos = input.position();
Ok(Declaration { prop: prop, val: val }) consume_any_value(input)?;
Ok(Declaration { prop: prop, val: input.slice_from(pos).to_owned() })
} }
/// Determine if a declaration parses /// Determine if a declaration parses

View file

@ -11,4 +11,6 @@ fn test_supports_condition() {
assert_roundtrip!(SupportsCondition::parse, "(margin: 1px)"); assert_roundtrip!(SupportsCondition::parse, "(margin: 1px)");
assert_roundtrip!(SupportsCondition::parse, "not (--be: to be)"); assert_roundtrip!(SupportsCondition::parse, "not (--be: to be)");
assert_roundtrip!(SupportsCondition::parse, "(color: blue) and future-extension(4)"); assert_roundtrip!(SupportsCondition::parse, "(color: blue) and future-extension(4)");
assert_roundtrip!(SupportsCondition::parse, "future-\\1 extension(4)");
assert_roundtrip!(SupportsCondition::parse, "((test))");
} }