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:
Matt Brubeck 2014-03-26 10:15:29 -07:00
parent ce877e4f5b
commit 8c794c6739
8 changed files with 83 additions and 46 deletions

View file

@ -12,17 +12,17 @@ use dom::bindings::js::JS;
use dom::bindings::utils::{Reflectable, Reflector};
use dom::bindings::error::{ErrorResult, Fallible, NamespaceError, InvalidCharacter};
use dom::bindings::utils::{QName, Name, InvalidXMLName, xml_name_type};
use dom::htmlcollection::HTMLCollection;
use dom::clientrect::ClientRect;
use dom::clientrectlist::ClientRectList;
use dom::document::Document;
use dom::eventtarget::{EventTarget, NodeTargetTypeId};
use dom::node::{ElementNodeTypeId, Node, NodeHelpers, NodeIterator, document_from_node};
use dom::htmlcollection::HTMLCollection;
use dom::htmlserializer::serialize;
use dom::node::{ElementNodeTypeId, Node, NodeHelpers, NodeIterator, document_from_node};
use dom::virtualmethods::{VirtualMethods, vtable_for};
use layout_interface::{ContentBoxQuery, ContentBoxResponse, ContentBoxesQuery};
use layout_interface::{ContentBoxesResponse, ContentChangedDocumentDamage};
use layout_interface::{MatchSelectorsDocumentDamage};
use layout_interface::MatchSelectorsDocumentDamage;
use style;
use servo_util::namespace;
use servo_util::namespace::{Namespace, Null};

View file

@ -3,14 +3,16 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use dom::bindings::codegen::HTMLStyleElementBinding;
use dom::bindings::codegen::InheritTypes::HTMLStyleElementDerived;
use dom::bindings::codegen::InheritTypes::{HTMLStyleElementDerived, NodeCast};
use dom::bindings::js::JS;
use dom::bindings::error::ErrorResult;
use dom::document::Document;
use dom::element::HTMLStyleElementTypeId;
use dom::eventtarget::{EventTarget, NodeTargetTypeId};
use dom::htmlelement::HTMLElement;
use dom::node::{Node, ElementNodeTypeId};
use dom::node::{Node, ElementNodeTypeId, window_from_node};
use html::cssparse::parse_inline_css;
use layout_interface::{AddStylesheetMsg, LayoutChan};
use servo_util::str::DOMString;
#[deriving(Encodable)]
@ -72,3 +74,20 @@ impl HTMLStyleElement {
Ok(())
}
}
pub trait StyleElementHelpers {
fn parse_own_css(&self);
}
impl StyleElementHelpers for JS<HTMLStyleElement> {
fn parse_own_css(&self) {
let node: JS<Node> = NodeCast::from(self);
let win = window_from_node(&node);
let url = win.get().page().get_url();
let data = node.get().GetTextContent(&node).expect("Element.textContent must be a string");
let sheet = parse_inline_css(url, data);
let LayoutChan(ref layout_chan) = win.get().page().layout_chan;
layout_chan.send(AddStylesheetMsg(sheet));
}
}

View file

@ -6,7 +6,7 @@
use dom::attr::Attr;
use dom::bindings::codegen::InheritTypes::{CommentCast, DocumentCast, DocumentTypeCast};
use dom::bindings::codegen::InheritTypes::{ElementCast, TextCast, NodeCast};
use dom::bindings::codegen::InheritTypes::{ElementCast, HTMLStyleElementCast, TextCast, NodeCast};
use dom::bindings::codegen::InheritTypes::{CharacterDataCast, NodeBase, NodeDerived};
use dom::bindings::codegen::InheritTypes::{ProcessingInstructionCast, EventTargetCast};
use dom::bindings::codegen::NodeBinding::NodeConstants;
@ -21,6 +21,7 @@ use dom::documentfragment::DocumentFragment;
use dom::documenttype::DocumentType;
use dom::element::{Element, ElementTypeId, HTMLAnchorElementTypeId};
use dom::eventtarget::{EventTarget, NodeTargetTypeId};
use dom::htmlstyleelement::StyleElementHelpers;
use dom::nodelist::{NodeList};
use dom::processinginstruction::ProcessingInstruction;
use dom::text::Text;
@ -269,6 +270,8 @@ pub trait NodeHelpers {
fn add_child(&mut self, new_child: &mut JS<Node>, before: Option<JS<Node>>);
fn remove_child(&mut self, child: &mut JS<Node>);
fn child_inserted(&self);
fn get_hover_state(&self) -> bool;
fn set_hover_state(&mut self, state: bool);
@ -408,6 +411,7 @@ impl NodeHelpers for JS<Node> {
}
}
self.parent_node().map(|parent| parent.child_inserted());
document.get().content_changed();
}
@ -499,6 +503,15 @@ impl NodeHelpers for JS<Node> {
child_node.set_parent_node(None);
}
fn child_inserted(&self) {
// Parse text content added to an inline stylesheet.
match HTMLStyleElementCast::to(self) {
Some(elem) => elem.parse_own_css(),
None => ()
}
}
fn get_hover_state(&self) -> bool {
self.get().flags.get_in_hover_state()
}