aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMartin Richard <martius@martiusweb.net>2014-06-21 18:17:19 +0200
committerMartin Richard <martius@martiusweb.net>2014-06-21 18:17:19 +0200
commitc2345e930dd6575e2293fa77e32b1be95d07fd5f (patch)
treee9673de76b8de48e8882f11eda6262f7b8b908e7
parent1e263f9dece8cc1c50c8a5e653138580877de06d (diff)
downloadservo-c2345e930dd6575e2293fa77e32b1be95d07fd5f.tar.gz
servo-c2345e930dd6575e2293fa77e32b1be95d07fd5f.zip
HTMLStyleElement only applies CSS in the document
HTMLStyleElement will not parse a style element created in Javascript until it is attached to the DOM. I added a reftest for the given cases: * a style element is defined in the HTML code, * a style element is created in Javascript, CSS content is added to the element and the element is later attached to the document, * a style element is created in Javascript, attached to the document and later CSS content is added to the element, * a style element is created in Javascript, CSS content is added to the * element but the element is never attached to the document.
-rw-r--r--src/components/script/dom/htmlstyleelement.rs15
-rw-r--r--src/test/ref/style_is_in_doc.html28
-rw-r--r--src/test/ref/style_is_in_doc_ref.html18
3 files changed, 60 insertions, 1 deletions
diff --git a/src/components/script/dom/htmlstyleelement.rs b/src/components/script/dom/htmlstyleelement.rs
index 485a2990602..b2138b500ad 100644
--- a/src/components/script/dom/htmlstyleelement.rs
+++ b/src/components/script/dom/htmlstyleelement.rs
@@ -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();
+ }
}
diff --git a/src/test/ref/style_is_in_doc.html b/src/test/ref/style_is_in_doc.html
new file mode 100644
index 00000000000..9cc72e43343
--- /dev/null
+++ b/src/test/ref/style_is_in_doc.html
@@ -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>
diff --git a/src/test/ref/style_is_in_doc_ref.html b/src/test/ref/style_is_in_doc_ref.html
new file mode 100644
index 00000000000..023768e8d77
--- /dev/null
+++ b/src/test/ref/style_is_in_doc_ref.html
@@ -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>