style: Cleanup @-moz-document parsing a bit.

MozReview-Commit-ID: 7vd0BLAqM0v
Bug: 1446470
Reviewed-by: xidorn
This commit is contained in:
Emilio Cobos Álvarez 2018-03-16 18:25:55 +01:00
parent 52205e8483
commit 652633aee2
No known key found for this signature in database
GPG key ID: 056B727BB9C1027C

View file

@ -6,7 +6,7 @@
//! initially in CSS Conditional Rules Module Level 3, @document has been postponed to the level 4.
//! We implement the prefixed `@-moz-document`.
use cssparser::{Parser, Token, SourceLocation};
use cssparser::{Parser, SourceLocation};
#[cfg(feature = "gecko")]
use malloc_size_of::{MallocSizeOfOps, MallocUnconditionalShallowSizeOf};
use media_queries::Device;
@ -15,7 +15,7 @@ use servo_arc::Arc;
use shared_lock::{DeepCloneParams, DeepCloneWithLock, Locked, SharedRwLock, SharedRwLockReadGuard, ToCssWithGuard};
use std::fmt::{self, Write};
use str::CssStringWriter;
use style_traits::{CssWriter, ParseError, StyleParseErrorKind, ToCss};
use style_traits::{CssWriter, ParseError, ToCss};
use stylesheets::CssRules;
use values::CssUrl;
@ -103,13 +103,8 @@ macro_rules! parse_quoted_or_unquoted_string {
$input.parse_nested_block(|input| {
let start = input.position();
input.parse_entirely(|input| {
let location = input.current_source_location();
match *input.next()? {
Token::QuotedString(ref value) => {
Ok($url_matching_function(value.as_ref().to_owned()))
},
ref t => Err(location.new_unexpected_token_error(t.clone())),
}
let string = input.expect_string()?;
Ok($url_matching_function(string.as_ref().to_owned()))
}).or_else(|_: ParseError| {
while let Ok(_) = input.next() {}
Ok($url_matching_function(input.slice_from(start).to_string()))
@ -120,21 +115,26 @@ macro_rules! parse_quoted_or_unquoted_string {
impl UrlMatchingFunction {
/// Parse a URL matching function for a`@document` rule's condition.
pub fn parse<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>)
-> Result<UrlMatchingFunction, ParseError<'i>> {
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() {
parse_quoted_or_unquoted_string!(input, UrlMatchingFunction::UrlPrefix)
} else if input.try(|input| input.expect_function_matching("domain")).is_ok() {
parse_quoted_or_unquoted_string!(input, UrlMatchingFunction::Domain)
} else if input.try(|input| input.expect_function_matching("regexp")).is_ok() {
input.parse_nested_block(|input| {
Ok(UrlMatchingFunction::Regexp(input.expect_string()?.as_ref().to_owned()))
})
} else if let Ok(url) = input.try(|input| CssUrl::parse(context, input)) {
Ok(UrlMatchingFunction::Url(url))
} else {
Err(input.new_custom_error(StyleParseErrorKind::UnspecifiedError))
return parse_quoted_or_unquoted_string!(input, UrlMatchingFunction::UrlPrefix)
}
if input.try(|input| input.expect_function_matching("domain")).is_ok() {
return parse_quoted_or_unquoted_string!(input, UrlMatchingFunction::Domain)
}
if input.try(|input| input.expect_function_matching("regexp")).is_ok() {
return input.parse_nested_block(|input| {
Ok(UrlMatchingFunction::Regexp(input.expect_string()?.as_ref().to_owned()))
});
}
let url = CssUrl::parse(context, input)?;
Ok(UrlMatchingFunction::Url(url))
}
#[cfg(feature = "gecko")]
@ -182,16 +182,18 @@ pub struct DocumentCondition(#[css(iterable)] Vec<UrlMatchingFunction>);
impl DocumentCondition {
/// Parse a document condition.
pub fn parse<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>)
-> Result<Self, ParseError<'i>> {
pub fn parse<'i, 't>(
context: &ParserContext,
input: &mut Parser<'i, 't>,
) -> Result<Self, ParseError<'i>> {
input.parse_comma_separated(|input| UrlMatchingFunction::parse(context, input))
.map(DocumentCondition)
}
/// Evaluate a document condition.
pub fn evaluate(&self, device: &Device) -> bool {
self.0.iter().any(|ref url_matching_function|
self.0.iter().any(|url_matching_function| {
url_matching_function.evaluate(device)
)
})
}
}