Skip to content

Commit

Permalink
Handle @namespace rules when parsing them rather than afterwards.
Browse files Browse the repository at this point in the history
This will avoid dealing with DOMRefCell when we add it around NamespaceRule.
  • Loading branch information
SimonSapin authored and Ms2ger committed Sep 8, 2016
1 parent 8bfe978 commit 45af168
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions components/style/stylesheets.rs
Expand Up @@ -160,19 +160,7 @@ impl Stylesheet {
let mut iter = RuleListParser::new_for_stylesheet(&mut input, rule_parser);
while let Some(result) = iter.next() {
match result {
Ok(rule) => {
if let CSSRule::Namespace(ref rule) = rule {
if let Some(ref prefix) = rule.prefix {
iter.parser.context.selector_context.namespace_prefixes.insert(
prefix.clone(), rule.url.clone());
} else {
iter.parser.context.selector_context.default_namespace =
Some(rule.url.clone());
}
}

rules.push(rule);
}
Ok(rule) => rules.push(rule),
Err(range) => {
let pos = range.start;
let message = format!("Invalid rule: '{}'", iter.input.slice(range));
Expand Down Expand Up @@ -443,10 +431,21 @@ impl<'a> AtRuleParser for TopLevelRuleParser<'a> {
if self.state.get() <= State::Namespaces {
self.state.set(State::Namespaces);

let prefix = input.try(|input| input.expect_ident()).ok().map(|p| p.into());
let prefix_result = input.try(|input| input.expect_ident());
let url = Namespace(Atom::from(try!(input.expect_url_or_string())));

let opt_prefix = if let Ok(prefix) = prefix_result {
let prefix: Atom = prefix.into();
self.context.selector_context.namespace_prefixes.insert(
prefix.clone(), url.clone());
Some(prefix)
} else {
self.context.selector_context.default_namespace = Some(url.clone());
None
};

return Ok(AtRuleType::WithoutBlock(CSSRule::Namespace(Arc::new(NamespaceRule {
prefix: prefix,
prefix: opt_prefix,
url: url,
}))))
} else {
Expand Down

0 comments on commit 45af168

Please sign in to comment.