mirror of
https://github.com/servo/servo.git
synced 2025-08-05 13:40:08 +01: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;
|
||||
|
|
|
@ -14,7 +14,7 @@ use dom::htmliframeelement::IFrameSize;
|
|||
use dom::htmlformelement::HTMLFormElement;
|
||||
use dom::node::{ElementNodeTypeId, INode, NodeHelpers};
|
||||
use dom::types::*;
|
||||
use html::cssparse::{InlineProvenance, StylesheetProvenance, UrlProvenance, spawn_css_parser};
|
||||
use html::cssparse::{StylesheetProvenance, UrlProvenance, spawn_css_parser};
|
||||
use script_task::Page;
|
||||
|
||||
use hubbub::hubbub;
|
||||
|
@ -298,7 +298,7 @@ pub fn parse_html(page: &Page,
|
|||
parser.enable_scripting(true);
|
||||
parser.enable_styling(true);
|
||||
|
||||
let (css_chan2, css_chan3, js_chan2) = (css_chan.clone(), css_chan.clone(), js_chan.clone());
|
||||
let (css_chan2, js_chan2) = (css_chan.clone(), js_chan.clone());
|
||||
|
||||
let next_subpage_id = RefCell::new(next_subpage_id);
|
||||
|
||||
|
@ -483,22 +483,8 @@ pub fn parse_html(page: &Page,
|
|||
}
|
||||
debug!("complete script");
|
||||
},
|
||||
complete_style: |style| {
|
||||
// We've reached the end of a <style> so we can submit all the text to the parser.
|
||||
unsafe {
|
||||
let style: JS<Node> = NodeWrapping::from_hubbub_node(style);
|
||||
let mut data = ~[];
|
||||
debug!("iterating over children {:?}", style.first_child());
|
||||
for child in style.children() {
|
||||
debug!("child = {:?}", child);
|
||||
let text: JS<Text> = TextCast::to(&child).unwrap();
|
||||
data.push(text.get().characterdata.data.to_str()); // FIXME: Bad copy.
|
||||
}
|
||||
|
||||
debug!("style data = {:?}", data);
|
||||
let provenance = InlineProvenance(base_url.clone(), data.concat());
|
||||
css_chan3.send(CSSTaskNewFile(provenance));
|
||||
}
|
||||
complete_style: |_| {
|
||||
// style parsing is handled in element::notify_child_list_changed.
|
||||
},
|
||||
};
|
||||
parser.set_tree_handler(&tree_handler);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue