Add support for non-UTF8 CSS stylesheets, with rust-encoding.

This commit is contained in:
Simon Sapin 2013-12-11 16:40:30 +00:00
parent b98acc2f75
commit 2d6ac33656
8 changed files with 57 additions and 38 deletions

View file

@ -8,7 +8,7 @@ use style::{Stylesheet, Stylist, UserAgentOrigin, with_errors_silenced};
pub fn new_stylist() -> Stylist {
let mut stylist = Stylist::new();
let ua_stylesheet = with_errors_silenced(
|| Stylesheet::from_str(include_str!("user-agent.css")));
|| Stylesheet::from_bytes(include_bin!("user-agent.css"), None, None));
stylist.add_stylesheet(ua_stylesheet, UserAgentOrigin);
stylist
}

View file

@ -8,8 +8,10 @@ use std::cell::Cell;
use std::comm;
use std::comm::Port;
use std::task;
use encoding::EncodingObj;
use encoding::all::UTF_8;
use style::Stylesheet;
use servo_net::resource_task::{Load, ProgressMsg, Payload, Done, ResourceTask};
use servo_net::resource_task::{Load, LoadResponse, ProgressMsg, Payload, Done, ResourceTask};
use extra::url::Url;
/// Where a style sheet comes from.
@ -23,6 +25,9 @@ pub fn spawn_css_parser(provenance: StylesheetProvenance,
-> Port<Stylesheet> {
let (result_port, result_chan) = comm::stream();
// TODO: Get the actual value. http://dev.w3.org/csswg/css-syntax/#environment-encoding
let environment_encoding = UTF_8 as EncodingObj;
let provenance_cell = Cell::new(provenance);
do task::spawn {
// TODO: CSS parsing should take a base URL.
@ -38,12 +43,15 @@ pub fn spawn_css_parser(provenance: StylesheetProvenance,
debug!("cssparse: loading style sheet at {:s}", url.to_str());
let (input_port, input_chan) = comm::stream();
resource_task.send(Load(url, input_chan));
Stylesheet::from_iter(ProgressMsgPortIterator {
progress_port: input_port.recv().progress_port
})
let LoadResponse { metadata: metadata, progress_port: progress_port }
= input_port.recv();
let protocol_encoding_label = metadata.charset.as_ref().map(|s| s.as_slice());
let iter = ProgressMsgPortIterator { progress_port: progress_port };
Stylesheet::from_bytes_iter(
iter, protocol_encoding_label, Some(environment_encoding))
}
InlineProvenance(_, data) => {
Stylesheet::from_str(data)
Stylesheet::from_str(data, environment_encoding)
}
};
result_chan.send(sheet);

View file

@ -15,6 +15,7 @@
extern mod geom = "rust-geom";
extern mod hubbub;
extern mod encoding;
extern mod js;
extern mod servo_net (name = "net");
extern mod servo_util (name = "util");

View file

@ -651,28 +651,27 @@ fn match_attribute<N: TreeNode<T>, T: TreeNodeRefAsElement<N, E>, E: ElementLike
}
}
}
fn get_rules(css_string: &str) -> ~[~[Rule]] {
let device = &Device { media_type: Screen };
let sheet = Stylesheet::from_str(css_string);
let mut index = 0u;
let mut results = ~[];
do iter_style_rules(sheet.rules.as_slice(), device) |style_rule| {
results.push(style_rule.selectors.iter().map(|s| Rule {
selector: Arc::new(s.clone()),
declarations: style_rule.declarations.normal.clone(),
index: index,
stylesheet_index: 0u,
}).collect());
index += 1u;
}
results
}
/// Helper method to get some Rules from selector strings.
/// Each sublist of the result contains the Rules for one StyleRule.
fn get_mock_rules(css_selectors: &[&str]) -> ~[~[Rule]] {
let css_string = css_selectors.map(|s| s + " { color: red; } ").concat();
get_rules(css_string)
use namespaces::NamespaceMap;
use selectors::parse_selector_list;
use cssparser::tokenize;
let namespaces = NamespaceMap::new();
css_selectors.iter().enumerate().map(|(i, selectors)| {
parse_selector_list(tokenize(*selectors).map(|(c, _)| c).to_owned_vec(), &namespaces)
.unwrap().move_iter().map(|s| {
Rule {
selector: Arc::new(s),
declarations: Arc::new(~[]),
index: i,
stylesheet_index: 0u,
}
}).to_owned_vec()
}).to_owned_vec()
}
#[test]

View file

@ -15,6 +15,7 @@
extern mod extra;
extern mod cssparser;
extern mod encoding;
extern mod servo_util (name = "util");

View file

@ -2,10 +2,12 @@
* 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::iter::Iterator;
use std::ascii::StrAsciiExt;
use cssparser::{tokenize, parse_stylesheet_rules, ToCss};
use encoding::EncodingObj;
use cssparser::{decode_stylesheet_bytes, tokenize, parse_stylesheet_rules, ToCss};
use cssparser::ast::*;
use selectors;
use properties;
@ -20,6 +22,7 @@ pub struct Stylesheet {
/// cascading order)
rules: ~[CSSRule],
namespaces: NamespaceMap,
encoding: EncodingObj,
}
@ -36,19 +39,26 @@ pub struct StyleRule {
impl Stylesheet {
pub fn from_iter<I: Iterator<~[u8]>>(input: I) -> Stylesheet {
let mut string = ~"";
let mut input = input;
// TODO: incremental tokinization/parsing
pub fn from_bytes_iter<I: Iterator<~[u8]>>(
mut input: I, protocol_encoding_label: Option<&str>,
environment_encoding: Option<EncodingObj>) -> Stylesheet {
let mut bytes = ~[];
// TODO: incremental decoding and 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))
bytes.push_all(chunk)
}
Stylesheet::from_str(string)
Stylesheet::from_bytes(bytes, protocol_encoding_label, environment_encoding)
}
pub fn from_str(css: &str) -> Stylesheet {
pub fn from_bytes(
bytes: &[u8], protocol_encoding_label: Option<&str>,
environment_encoding: Option<EncodingObj>) -> Stylesheet {
let (string, used_encoding) = decode_stylesheet_bytes(
bytes, protocol_encoding_label, environment_encoding);
Stylesheet::from_str(string, used_encoding)
}
pub fn from_str(css: &str, encoding: EncodingObj) -> Stylesheet {
static STATE_CHARSET: uint = 1;
static STATE_IMPORTS: uint = 2;
static STATE_NAMESPACES: uint = 3;
@ -107,7 +117,7 @@ impl Stylesheet {
}
state = next_state;
}
Stylesheet{ rules: rules, namespaces: namespaces }
Stylesheet{ rules: rules, namespaces: namespaces, encoding: encoding }
}
}

@ -1 +1 @@
Subproject commit 1a7b4ca6d368f6035a4135504f89d66c5e225d09
Subproject commit 4eb52d5cb22e39c9edff848420c910b0b7ecafa5

@ -1 +1 @@
Subproject commit 2c486a34dedba1faace3de641e0ace103c7f3145
Subproject commit 47bef0fe76e094e7886f3eaabb652d0f43af1913