mirror of
https://github.com/servo/servo.git
synced 2025-08-06 14:10:11 +01:00
Add a recursive style rule iterator for stylesheets.
This commit is contained in:
parent
5758c31df7
commit
e1bde72c59
1 changed files with 39 additions and 2 deletions
|
@ -10,10 +10,11 @@ use properties;
|
|||
use errors::{ErrorLoggerIterator, log_css_error};
|
||||
use namespaces::{NamespaceMap, parse_namespace_rule};
|
||||
use media_queries::{MediaRule, parse_media_rule};
|
||||
use media_queries;
|
||||
|
||||
|
||||
pub struct Stylesheet {
|
||||
style_rules: ~[CSSRule],
|
||||
rules: ~[CSSRule],
|
||||
namespaces: NamespaceMap,
|
||||
}
|
||||
|
||||
|
@ -88,7 +89,7 @@ fn parse_stylesheet(css: &str) -> Stylesheet {
|
|||
}
|
||||
state = next_state;
|
||||
}
|
||||
Stylesheet{ style_rules: rules, namespaces: namespaces }
|
||||
Stylesheet{ rules: rules, namespaces: namespaces }
|
||||
}
|
||||
|
||||
|
||||
|
@ -113,3 +114,39 @@ pub fn parse_nested_at_rule(lower_name: &str, rule: AtRule,
|
|||
_ => log_css_error(rule.location, fmt!("Unsupported at-rule: @%s", lower_name))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl Stylesheet {
|
||||
fn iter_style_rules<'a>(&'a self, device: media_queries::Device) -> StyleRuleIterator<'a> {
|
||||
StyleRuleIterator { device: device, stack: ~[(self.rules.as_slice(), 0)] }
|
||||
}
|
||||
}
|
||||
|
||||
struct StyleRuleIterator<'self> {
|
||||
device: 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))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue