Merge pull request #2692 from Martiusweb/style_is_in_doc

HTMLStyleElement only applies CSS in the document; r=Ms2ger
This commit is contained in:
Ms2ger 2014-06-21 19:38:31 +02:00
commit 1a349369f1
3 changed files with 60 additions and 1 deletions

View file

@ -9,7 +9,7 @@ use dom::document::Document;
use dom::element::HTMLStyleElementTypeId;
use dom::eventtarget::{EventTarget, NodeTargetTypeId};
use dom::htmlelement::HTMLElement;
use dom::node::{Node, NodeMethods, ElementNodeTypeId, window_from_node};
use dom::node::{Node, NodeMethods, NodeHelpers, ElementNodeTypeId, window_from_node};
use dom::virtualmethods::VirtualMethods;
use html::cssparse::parse_inline_css;
use layout_interface::{AddStylesheetMsg, LayoutChan};
@ -49,6 +49,11 @@ pub trait StyleElementHelpers {
impl<'a> StyleElementHelpers for JSRef<'a, HTMLStyleElement> {
fn parse_own_css(&self) {
let node: &JSRef<Node> = NodeCast::from_ref(self);
if !node.is_in_doc() {
return;
}
let win = window_from_node(node).root();
let url = win.deref().page().get_url();
@ -72,4 +77,12 @@ impl<'a> VirtualMethods for JSRef<'a, HTMLStyleElement> {
}
self.parse_own_css();
}
fn bind_to_tree(&self) {
match self.super_type() {
Some(ref s) => s.bind_to_tree(),
_ => ()
}
self.parse_own_css();
}
}

View file

@ -0,0 +1,28 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style>
.from_html { color: blue; }
.is_not_in_doc { color: grey; }
</style>
<script type="text/javascript">
var append_later = document.createElement('style');
append_later.textContent = ".append_later { color: green; }";
document.head.appendChild(append_later);
var text_set_later = document.createElement('style');
document.head.appendChild(text_set_later);
text_set_later.textContent = ".text_set_later { color: yellow; }";
var is_not_in_doc = document.createElement('style');
is_not_in_doc = ".is_not_in_doc { color: red; }";
</script>
</head>
<body>
<p class="from_html">Style from html element</p>
<p class="append_later">Style from an element added in javascript</p>
<p class="text_set_later">Style from an element with content set in javascript</p>
<p class="is_not_in_doc">Style that is never in the document</p>
</body>
</html>

View file

@ -0,0 +1,18 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style>
.from_html { color: blue; }
.append_later { color: green; }
.text_set_later { color: yellow; }
.is_not_in_doc { color: grey; }
</style>
</head>
<body>
<p class="from_html">Style from html element</p>
<p class="append_later">Style from an element added in javascript</p>
<p class="text_set_later">Style from an element with content set in javascript</p>
<p class="is_not_in_doc">Style that is never in the document</p>
</body>
</html>