Bug 1475511: Make document condition parsing a bit nicer. r=xidorn
MozReview-Commit-ID: Gi0FxrEAYcE
--- a/servo/components/style/stylesheets/document_rule.rs
+++ b/servo/components/style/stylesheets/document_rule.rs
@@ -117,43 +117,40 @@ macro_rules! parse_quoted_or_unquoted_st
}
impl DocumentMatchingFunction {
/// Parse a URL matching function for a`@document` rule's condition.
pub fn parse<'i, 't>(
context: &ParserContext,
input: &mut Parser<'i, 't>,
) -> Result<Self, ParseError<'i>> {
- if input
- .try(|input| input.expect_function_matching("url-prefix"))
- .is_ok()
- {
- return parse_quoted_or_unquoted_string!(input, DocumentMatchingFunction::UrlPrefix);
+ if let Ok(url) = input.try(|input| CssUrl::parse(context, input)) {
+ return Ok(DocumentMatchingFunction::Url(url))
}
- if input
- .try(|input| input.expect_function_matching("domain"))
- .is_ok()
- {
- return parse_quoted_or_unquoted_string!(input, DocumentMatchingFunction::Domain);
- }
-
- if input
- .try(|input| input.expect_function_matching("regexp"))
- .is_ok()
- {
- return input.parse_nested_block(|input| {
+ let location = input.current_source_location();
+ let function = input.expect_function()?.clone();
+ match_ignore_ascii_case! { &function,
+ "url-prefix" => {
+ parse_quoted_or_unquoted_string!(input, DocumentMatchingFunction::UrlPrefix)
+ }
+ "domain" => {
+ parse_quoted_or_unquoted_string!(input, DocumentMatchingFunction::Domain)
+ }
+ "regex" => {
Ok(DocumentMatchingFunction::Regexp(
input.expect_string()?.as_ref().to_owned(),
))
- });
+ }
+ _ => {
+ return Err(location.new_custom_error(
+ StyleParseErrorKind::UnexpectedFunction(function.clone())
+ ))
+ }
}
-
- let url = CssUrl::parse(context, input)?;
- Ok(DocumentMatchingFunction::Url(url))
}
#[cfg(feature = "gecko")]
/// Evaluate a URL matching function.
pub fn evaluate(&self, device: &Device) -> bool {
use gecko_bindings::bindings::Gecko_DocumentRule_UseForPresentation;
use gecko_bindings::structs::DocumentMatchingFunction as GeckoDocumentMatchingFunction;
use nsstring::nsCStr;