Bug 1475511: Make document condition parsing a bit nicer. r=xidorn draft
authorEmilio Cobos Álvarez <emilio@crisal.io>
Thu, 19 Jul 2018 19:12:18 +0200
changeset 820835 533cc7054254ef69e7ebbe145e0843c6374afaef
parent 820834 f51649498047a4a340341f83c5125dcbf41593b0
child 820836 8983bc9ae92833222f6d7d129e441a208e45f510
push id116953
push userbmo:emilio@crisal.io
push dateFri, 20 Jul 2018 13:44:17 +0000
reviewersxidorn
bugs1475511
milestone63.0a1
Bug 1475511: Make document condition parsing a bit nicer. r=xidorn MozReview-Commit-ID: Gi0FxrEAYcE
servo/components/style/stylesheets/document_rule.rs
--- 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;