mirror of
https://github.com/servo/servo.git
synced 2025-06-16 20:34:30 +00:00
Move inline stylesheet parsing out of HTML parser.
Instead, use shared code to parse stylesheet content when it is inserted, whether during parsing or dynamically by script. Based on work by sanools in #1350.
This commit is contained in:
parent
ce877e4f5b
commit
8c794c6739
8 changed files with 83 additions and 46 deletions
|
@ -18,34 +18,45 @@ pub enum StylesheetProvenance {
|
|||
InlineProvenance(Url, ~str),
|
||||
}
|
||||
|
||||
// Parses the style data and returns the stylesheet
|
||||
pub fn parse_inline_css(url: Url, data: ~str) -> Stylesheet {
|
||||
let resource_task = ResourceTask(); // Resource task is not used for inline parsing
|
||||
parse_css(InlineProvenance(url, data), resource_task)
|
||||
}
|
||||
|
||||
fn parse_css(provenance: StylesheetProvenance,
|
||||
resource_task: ResourceTask) -> Stylesheet {
|
||||
// TODO: Get the actual value. http://dev.w3.org/csswg/css-syntax/#environment-encoding
|
||||
let environment_encoding = UTF_8 as EncodingRef;
|
||||
|
||||
match provenance {
|
||||
UrlProvenance(url) => {
|
||||
debug!("cssparse: loading style sheet at {:s}", url.to_str());
|
||||
let (input_chan, input_port) = channel();
|
||||
resource_task.send(Load(url, input_chan));
|
||||
let LoadResponse { metadata: metadata, progress_port: progress_port }
|
||||
= input_port.recv();
|
||||
let final_url = &metadata.final_url;
|
||||
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, final_url.clone(),
|
||||
protocol_encoding_label, Some(environment_encoding))
|
||||
}
|
||||
InlineProvenance(base_url, data) => {
|
||||
debug!("cssparse: loading inline stylesheet {:s}", data);
|
||||
Stylesheet::from_str(data, base_url, environment_encoding)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn spawn_css_parser(provenance: StylesheetProvenance,
|
||||
resource_task: ResourceTask)
|
||||
-> Receiver<Stylesheet> {
|
||||
let (result_chan, result_port) = channel();
|
||||
|
||||
// TODO: Get the actual value. http://dev.w3.org/csswg/css-syntax/#environment-encoding
|
||||
let environment_encoding = UTF_8 as EncodingRef;
|
||||
|
||||
spawn_named("cssparser", proc() {
|
||||
let sheet = match provenance {
|
||||
UrlProvenance(url) => {
|
||||
debug!("cssparse: loading style sheet at {:s}", url.to_str());
|
||||
let (input_chan, input_port) = channel();
|
||||
resource_task.send(Load(url, input_chan));
|
||||
let LoadResponse { metadata: metadata, progress_port: progress_port }
|
||||
= input_port.recv();
|
||||
let final_url = &metadata.final_url;
|
||||
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, final_url.clone(),
|
||||
protocol_encoding_label, Some(environment_encoding))
|
||||
}
|
||||
InlineProvenance(base_url, data) => {
|
||||
Stylesheet::from_str(data, base_url, environment_encoding)
|
||||
}
|
||||
};
|
||||
result_chan.send(sheet);
|
||||
result_chan.send(parse_css(provenance, resource_task));
|
||||
});
|
||||
|
||||
return result_port;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue