Add parsing a stylesheet form an iterator, a style attr form a string.

This commit is contained in:
Simon Sapin 2013-10-17 22:44:55 +01:00
parent a4c2e9dcf1
commit 6ddc2c37d1
4 changed files with 87 additions and 67 deletions

View file

@ -815,10 +815,15 @@ pub struct PropertyDeclarationBlock {
}
pub fn parse_property_declaration_list(input: ~[Node]) -> PropertyDeclarationBlock {
pub fn parse_style_attribute(input: &str) -> PropertyDeclarationBlock {
parse_property_declaration_list(tokenize(input))
}
pub fn parse_property_declaration_list<I: Iterator<Node>>(input: I) -> PropertyDeclarationBlock {
let mut important = ~[];
let mut normal = ~[];
for item in ErrorLoggerIterator(parse_declaration_list(input.move_iter())) {
for item in ErrorLoggerIterator(parse_declaration_list(input)) {
match item {
Decl_AtRule(rule) => log_css_error(
rule.location, fmt!("Unsupported at-rule in declaration list: @%s", rule.name)),

View file

@ -6,7 +6,7 @@ use std::ascii::StrAsciiExt;
use extra::sort::tim_sort;
use selectors::*;
use stylesheets::parse_stylesheet;
use stylesheets::Stylesheet;
use media_queries::{Device, Screen};
use properties::{PropertyDeclaration, PropertyDeclarationBlock};
use servo_util::tree::{TreeNodeRefAsElement, TreeNode, ElementLike};
@ -36,8 +36,7 @@ impl Stylist {
}
}
pub fn add_stylesheet(&mut self, css_source: &str, origin: StylesheetOrigin) {
let stylesheet = parse_stylesheet(css_source);
pub fn add_stylesheet(&mut self, stylesheet: Stylesheet, origin: StylesheetOrigin) {
let rules = match origin {
UserAgentOrigin => &mut self.ua_rules,
AuthorOrigin => &mut self.author_rules,

View file

@ -18,10 +18,10 @@ extern mod servo_util (name = "util");
// The "real" public API
pub use stylesheets::Stylesheet;
pub use selector_matching::{Stylist, StylesheetOrigin};
pub use properties::{cascade, computed_values};
pub use properties::{PropertyDeclarationBlock,
parse_property_declaration_list}; // Style attributes
pub use properties::{cascade, ComputedValues, computed_values};
pub use properties::{PropertyDeclarationBlock, parse_style_attribute}; // Style attributes
// Things that need to be public to make the compiler happy
pub mod stylesheets;

View file

@ -2,6 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use std::str;
use std::iterator::Iterator;
use std::ascii::StrAsciiExt;
use cssparser::*;
@ -31,7 +32,20 @@ pub struct StyleRule {
}
pub fn parse_stylesheet(css: &str) -> Stylesheet {
impl Stylesheet {
pub fn from_iter<I: Iterator<~[u8]>>(input: I) -> Stylesheet {
let mut string = ~"";
let mut input = input;
// TODO: incremental tokinization/parsing
for chunk in input {
// Assume UTF-8. This fails on invalid UTF-8
// TODO: support character encodings (use rust-encodings in rust-cssparser)
string.push_str(str::from_utf8_owned(chunk))
}
Stylesheet::from_str(string)
}
pub fn from_str(css: &str) -> Stylesheet {
static STATE_CHARSET: uint = 1;
static STATE_IMPORTS: uint = 2;
static STATE_NAMESPACES: uint = 3;
@ -65,7 +79,8 @@ pub fn parse_stylesheet(css: &str) -> Stylesheet {
"@import must be before any rule but @charset")
} else {
next_state = STATE_IMPORTS;
log_css_error(rule.location, "@import is not supported yet") // TODO
// TODO: support @import
log_css_error(rule.location, "@import is not supported yet")
}
},
"namespace" => {
@ -91,6 +106,7 @@ pub fn parse_stylesheet(css: &str) -> Stylesheet {
}
Stylesheet{ rules: rules, namespaces: namespaces }
}
}
pub fn parse_style_rule(rule: QualifiedRule, parent_rules: &mut ~[CSSRule],
@ -99,7 +115,7 @@ pub fn parse_style_rule(rule: QualifiedRule, parent_rules: &mut ~[CSSRule],
match selectors::parse_selector_list(prelude, namespaces) {
Some(selectors) => parent_rules.push(CSSStyleRule(StyleRule{
selectors: selectors,
declarations: properties::parse_property_declaration_list(block)
declarations: properties::parse_property_declaration_list(block.move_iter())
})),
None => log_css_error(location, "Unsupported CSS selector."),
}
@ -125,7 +141,7 @@ impl Stylesheet {
struct StyleRuleIterator<'self> {
device: &'self media_queries::Device,
// FIXME: I couldnt get this to borrow-check with a stack of VecIterator
// FIXME: I couldn't get this to borrow-check with a stack of VecIterator
stack: ~[(&'self [CSSRule], uint)],
}