diff --git a/src/components/style/selector_matching.rs b/src/components/style/selector_matching.rs index e7ef0ab1955..a203df620e1 100644 --- a/src/components/style/selector_matching.rs +++ b/src/components/style/selector_matching.rs @@ -7,7 +7,7 @@ use std::ascii::StrAsciiExt; use extra::sort::tim_sort; use selectors::*; -use stylesheets::Stylesheet; +use stylesheets::{Stylesheet, iter_style_rules}; use media_queries::{Device, Screen}; use properties::{PropertyDeclaration, PropertyDeclarationBlock}; use servo_util::tree::{TreeNodeRefAsElement, TreeNode, ElementLike}; @@ -62,7 +62,7 @@ impl Stylist { ) let device = &Device { media_type: Screen }; // TODO, use Print when printing - for style_rule in stylesheet.iter_style_rules(device) { + do iter_style_rules(stylesheet.rules.as_slice(), device) |style_rule| { append!(normal, added_normal_declarations); append!(important, added_important_declarations); } diff --git a/src/components/style/stylesheets.rs b/src/components/style/stylesheets.rs index b54ad06e5ca..7709e551549 100644 --- a/src/components/style/stylesheets.rs +++ b/src/components/style/stylesheets.rs @@ -132,37 +132,13 @@ pub fn parse_nested_at_rule(lower_name: &str, rule: AtRule, } -impl Stylesheet { - pub fn iter_style_rules<'a>(&'a self, device: &'a media_queries::Device) - -> StyleRuleIterator<'a> { - StyleRuleIterator { device: device, stack: ~[(self.rules.as_slice(), 0)] } - } -} - -struct StyleRuleIterator<'self> { - device: &'self media_queries::Device, - // FIXME: I couldn't get this to borrow-check with a stack of VecIterator - stack: ~[(&'self [CSSRule], uint)], -} - -impl<'self> Iterator<&'self StyleRule> for StyleRuleIterator<'self> { - fn next(&mut self) -> Option<&'self StyleRule> { - loop { - match self.stack.pop_opt() { - None => return None, - Some((rule_list, i)) => { - if i + 1 < rule_list.len() { - self.stack.push((rule_list, i + 1)) - } - match rule_list[i] { - CSSStyleRule(ref rule) => return Some(rule), - CSSMediaRule(ref rule) => { - if rule.media_queries.evaluate(self.device) { - self.stack.push((rule.rules.as_slice(), 0)) - } - } - } - } +pub fn iter_style_rules<'a>(rules: &[CSSRule], device: &media_queries::Device, + callback: &fn(&StyleRule)) { + for rule in rules.iter() { + match *rule { + CSSStyleRule(ref rule) => callback(rule), + CSSMediaRule(ref rule) => if rule.media_queries.evaluate(device) { + iter_style_rules(rule.rules.as_slice(), device, |s| callback(s)) } } }