Make sure stylsheets have a base URL.

This commit is contained in:
Simon Sapin 2013-12-11 16:51:08 +00:00
parent 2d6ac33656
commit 4f673f031e
3 changed files with 18 additions and 11 deletions

View file

@ -3,12 +3,16 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use style::{Stylesheet, Stylist, UserAgentOrigin, with_errors_silenced};
use extra::url;
pub fn new_stylist() -> Stylist {
let mut stylist = Stylist::new();
let ua_stylesheet = with_errors_silenced(
|| Stylesheet::from_bytes(include_bin!("user-agent.css"), None, None));
let ua_stylesheet = with_errors_silenced(|| Stylesheet::from_bytes(
include_bin!("user-agent.css"),
url::from_str("chrome:///user-agent.css").unwrap(),
None,
None));
stylist.add_stylesheet(ua_stylesheet, UserAgentOrigin);
stylist
}

View file

@ -48,10 +48,11 @@ pub fn spawn_css_parser(provenance: StylesheetProvenance,
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))
iter, metadata.final_url,
protocol_encoding_label, Some(environment_encoding))
}
InlineProvenance(_, data) => {
Stylesheet::from_str(data, environment_encoding)
InlineProvenance(base_url, data) => {
Stylesheet::from_str(data, base_url, environment_encoding)
}
};
result_chan.send(sheet);

View file

@ -4,6 +4,7 @@
use std::iter::Iterator;
use std::ascii::StrAsciiExt;
use extra::url::Url;
use encoding::EncodingObj;
@ -23,6 +24,7 @@ pub struct Stylesheet {
rules: ~[CSSRule],
namespaces: NamespaceMap,
encoding: EncodingObj,
base_url: Url,
}
@ -40,25 +42,25 @@ pub struct StyleRule {
impl Stylesheet {
pub fn from_bytes_iter<I: Iterator<~[u8]>>(
mut input: I, protocol_encoding_label: Option<&str>,
mut input: I, base_url: Url, protocol_encoding_label: Option<&str>,
environment_encoding: Option<EncodingObj>) -> Stylesheet {
let mut bytes = ~[];
// TODO: incremental decoding and tokinization/parsing
for chunk in input {
bytes.push_all(chunk)
}
Stylesheet::from_bytes(bytes, protocol_encoding_label, environment_encoding)
Stylesheet::from_bytes(bytes, base_url, protocol_encoding_label, environment_encoding)
}
pub fn from_bytes(
bytes: &[u8], protocol_encoding_label: Option<&str>,
bytes: &[u8], base_url: Url, 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)
Stylesheet::from_str(string, base_url, used_encoding)
}
pub fn from_str(css: &str, encoding: EncodingObj) -> Stylesheet {
pub fn from_str(css: &str, base_url: Url, encoding: EncodingObj) -> Stylesheet {
static STATE_CHARSET: uint = 1;
static STATE_IMPORTS: uint = 2;
static STATE_NAMESPACES: uint = 3;
@ -117,7 +119,7 @@ impl Stylesheet {
}
state = next_state;
}
Stylesheet{ rules: rules, namespaces: namespaces, encoding: encoding }
Stylesheet{ rules: rules, namespaces: namespaces, encoding: encoding, base_url: base_url }
}
}