Derive ToCss for UrlMatchingFunction

This commit is contained in:
Anthony Ramine 2018-03-02 20:19:48 +01:00
parent 0eb8b1f4c0
commit ba1d3d4b81

View file

@ -71,7 +71,7 @@ impl DeepCloneWithLock for DocumentRule {
}
/// A URL matching function for a `@document` rule's condition.
#[derive(Clone, Debug)]
#[derive(Clone, Debug, ToCss)]
pub enum UrlMatchingFunction {
/// Exact URL matching function. It evaluates to true whenever the
/// URL of the document being styled is exactly the URL given.
@ -81,6 +81,7 @@ pub enum UrlMatchingFunction {
/// function as an initial substring (which is true when the two
/// strings are equal). When the argument is the empty string,
/// it evaluates to true for all documents.
#[css(function)]
UrlPrefix(String),
/// Domain matching function. It evaluates to true whenever the URL
/// of the document being styled has a host subcomponent and that
@ -88,11 +89,13 @@ pub enum UrlMatchingFunction {
/// function or a final substring of the host component is a
/// period (U+002E) immediately followed by the argument to the
/// domain() function.
#[css(function)]
Domain(String),
/// Regular expression matching function. It evaluates to true
/// whenever the regular expression matches the entirety of the URL
/// of the document being styled.
RegExp(String),
#[css(function)]
Regexp(String),
}
macro_rules! parse_quoted_or_unquoted_string {
@ -125,7 +128,7 @@ impl UrlMatchingFunction {
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()))
Ok(UrlMatchingFunction::Regexp(input.expect_string()?.as_ref().to_owned()))
})
} else if let Ok(url) = input.try(|input| SpecifiedUrl::parse(context, input)) {
Ok(UrlMatchingFunction::Url(url))
@ -145,14 +148,14 @@ impl UrlMatchingFunction {
UrlMatchingFunction::Url(_) => GeckoUrlMatchingFunction::eURL,
UrlMatchingFunction::UrlPrefix(_) => GeckoUrlMatchingFunction::eURLPrefix,
UrlMatchingFunction::Domain(_) => GeckoUrlMatchingFunction::eDomain,
UrlMatchingFunction::RegExp(_) => GeckoUrlMatchingFunction::eRegExp,
UrlMatchingFunction::Regexp(_) => GeckoUrlMatchingFunction::eRegExp,
};
let pattern = nsCStr::from(match *self {
UrlMatchingFunction::Url(ref url) => url.as_str(),
UrlMatchingFunction::UrlPrefix(ref pat) |
UrlMatchingFunction::Domain(ref pat) |
UrlMatchingFunction::RegExp(ref pat) => pat,
UrlMatchingFunction::Regexp(ref pat) => pat,
});
unsafe {
Gecko_DocumentRule_UseForPresentation(device.pres_context(), &*pattern, func)
@ -166,34 +169,6 @@ impl UrlMatchingFunction {
}
}
impl ToCss for UrlMatchingFunction {
fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
where
W: Write,
{
match *self {
UrlMatchingFunction::Url(ref url) => {
url.to_css(dest)
},
UrlMatchingFunction::UrlPrefix(ref url_prefix) => {
dest.write_str("url-prefix(")?;
url_prefix.to_css(dest)?;
dest.write_str(")")
},
UrlMatchingFunction::Domain(ref domain) => {
dest.write_str("domain(")?;
domain.to_css(dest)?;
dest.write_str(")")
},
UrlMatchingFunction::RegExp(ref regex) => {
dest.write_str("regexp(")?;
regex.to_css(dest)?;
dest.write_str(")")
},
}
}
}
/// A `@document` rule's condition.
///
/// <https://www.w3.org/TR/2012/WD-css3-conditional-20120911/#at-document>