From 94d5b28fe95bda5d8caa734e04f27b1d978d607c Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Thu, 25 Aug 2016 15:46:03 +0200 Subject: [PATCH] Add a separate NamespaceRule type --- components/style/stylesheets.rs | 26 +++++++++++++++----------- tests/unit/style/stylesheets.rs | 6 +++--- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/components/style/stylesheets.rs b/components/style/stylesheets.rs index 2873d803eba..56566fd2e91 100644 --- a/components/style/stylesheets.rs +++ b/components/style/stylesheets.rs @@ -67,11 +67,7 @@ pub enum CSSRule { // No Charset here, CSSCharsetRule has been removed from CSSOM // https://drafts.csswg.org/cssom/#changes-from-5-december-2013 - Namespace { - /// `None` for the default Namespace - prefix: Option, - url: Namespace, - }, + Namespace(NamespaceRule), Style(StyleRule), Media(MediaRule), FontFace(FontFaceRule), @@ -80,6 +76,14 @@ pub enum CSSRule { } +#[derive(Debug, PartialEq)] +#[cfg_attr(feature = "servo", derive(HeapSizeOf))] +pub struct NamespaceRule { + /// `None` for the default Namespace + pub prefix: Option, + pub url: Namespace, +} + #[derive(Debug, PartialEq)] #[cfg_attr(feature = "servo", derive(HeapSizeOf))] pub struct KeyframesRule { @@ -156,13 +160,13 @@ impl Stylesheet { while let Some(result) = iter.next() { match result { Ok(rule) => { - if let CSSRule::Namespace { ref prefix, ref url } = rule { - if let Some(prefix) = prefix.as_ref() { + if let CSSRule::Namespace(ref rule) = rule { + if let Some(ref prefix) = rule.prefix { iter.parser.context.selector_context.namespace_prefixes.insert( - prefix.clone(), url.clone()); + prefix.clone(), rule.url.clone()); } else { iter.parser.context.selector_context.default_namespace = - Some(url.clone()); + Some(rule.url.clone()); } } @@ -440,10 +444,10 @@ impl<'a> AtRuleParser for TopLevelRuleParser<'a> { let prefix = input.try(|input| input.expect_ident()).ok().map(|p| p.into()); let url = Namespace(Atom::from(try!(input.expect_url_or_string()))); - return Ok(AtRuleType::WithoutBlock(CSSRule::Namespace { + return Ok(AtRuleType::WithoutBlock(CSSRule::Namespace(NamespaceRule { prefix: prefix, url: url, - })) + }))) } else { return Err(()) // "@namespace must be before any rule but @charset and @import" } diff --git a/tests/unit/style/stylesheets.rs b/tests/unit/style/stylesheets.rs index 528dcd67084..0b74df734e8 100644 --- a/tests/unit/style/stylesheets.rs +++ b/tests/unit/style/stylesheets.rs @@ -15,7 +15,7 @@ use style::parser::ParserContextExtraData; use style::properties::{PropertyDeclaration, PropertyDeclarationBlock, DeclaredValue, longhands}; use style::properties::Importance; use style::properties::longhands::animation_play_state; -use style::stylesheets::{Stylesheet, CSSRule, StyleRule, KeyframesRule, Origin}; +use style::stylesheets::{Stylesheet, NamespaceRule, CSSRule, StyleRule, KeyframesRule, Origin}; use style::values::specified::{LengthOrPercentageOrAuto, Percentage}; use url::Url; @@ -65,10 +65,10 @@ fn test_parse_stylesheet() { media: None, dirty_on_viewport_size_change: false, rules: vec![ - CSSRule::Namespace { + CSSRule::Namespace(NamespaceRule { prefix: None, url: NsAtom(Atom::from("http://www.w3.org/1999/xhtml")) - }, + }), CSSRule::Style(StyleRule { selectors: vec![ Selector {