aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorbors-servo <release+servo@mozilla.com>2014-03-15 06:14:14 -0400
committerbors-servo <release+servo@mozilla.com>2014-03-15 06:14:14 -0400
commit2d2fae5fc5053d5c0cea70768d6a43f006e436a8 (patch)
tree1cc50ba7698a9690fcbba089a98e0b0c61b44eca /src
parent508fe57f8dc4cedf9f4035b4e6311bf97d674adb (diff)
parent574fba310fc040ee3ca016093ed6b329decf1af7 (diff)
downloadservo-2d2fae5fc5053d5c0cea70768d6a43f006e436a8.tar.gz
servo-2d2fae5fc5053d5c0cea70768d6a43f006e436a8.zip
auto merge of #1894 : khodzha/servo/node_normalize, r=Ms2ger
To resolve issue #1823 (without DOM Range updates)
Diffstat (limited to 'src')
-rw-r--r--src/components/script/dom/bindings/codegen/Bindings.conf1
-rw-r--r--src/components/script/dom/node.rs24
-rw-r--r--src/test/content/test_node_normalize.html35
3 files changed, 58 insertions, 2 deletions
diff --git a/src/components/script/dom/bindings/codegen/Bindings.conf b/src/components/script/dom/bindings/codegen/Bindings.conf
index 7403c2256e1..2380a0eaeed 100644
--- a/src/components/script/dom/bindings/codegen/Bindings.conf
+++ b/src/components/script/dom/bindings/codegen/Bindings.conf
@@ -90,6 +90,7 @@ DOMInterfaces = {
'isEqualNode',
'nodeName',
'nodeValue',
+ 'normalize',
'removeChild',
'replaceChild',
'textContent',
diff --git a/src/components/script/dom/node.rs b/src/components/script/dom/node.rs
index 000a19be847..9d01ad9d8db 100644
--- a/src/components/script/dom/node.rs
+++ b/src/components/script/dom/node.rs
@@ -1552,8 +1552,28 @@ impl Node {
}
// http://dom.spec.whatwg.org/#dom-node-normalize
- pub fn Normalize(&mut self) {
- // FIXME (#1823) implement.
+ pub fn Normalize(&mut self, abstract_self: &mut JS<Node>) {
+ let mut prev_text = None;
+ for mut child in self.children() {
+ if child.is_text() {
+ let characterdata: JS<CharacterData> = CharacterDataCast::to(&child);
+ if characterdata.get().Length() == 0 {
+ abstract_self.remove_child(&mut child);
+ } else {
+ match prev_text {
+ Some(ref text_node) => {
+ let mut prev_characterdata: JS<CharacterData> = CharacterDataCast::to(text_node);
+ prev_characterdata.get_mut().AppendData(characterdata.get().Data());
+ abstract_self.remove_child(&mut child);
+ },
+ None => prev_text = Some(child)
+ }
+ }
+ } else {
+ prev_text = None;
+ }
+
+ }
}
// http://dom.spec.whatwg.org/#dom-node-clonenode
diff --git a/src/test/content/test_node_normalize.html b/src/test/content/test_node_normalize.html
new file mode 100644
index 00000000000..b2e9a367128
--- /dev/null
+++ b/src/test/content/test_node_normalize.html
@@ -0,0 +1,35 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <script src="harness.js"></script>
+ </head>
+ <body>
+ <script>
+ // test1: Node.normalize
+ var parent1 = document.createElement("div");
+ var child1 = document.createTextNode("aaa");
+ var child2 = document.createTextNode("");
+ var child3 = document.createTextNode("bb");
+
+ var parent2 = document.createElement("div");
+
+ parent1.appendChild(child1);
+ parent1.appendChild(child2);
+ parent1.appendChild(child3);
+
+ parent2.appendChild(document.createTextNode(""));
+
+ parent1.normalize();
+ parent2.normalize();
+
+ is(Array.prototype.map.call(parent1.childNodes, function(el) {return el.length}).indexOf(0), -1, "Node.normalize removes empty text nodes");
+ is(parent1.childNodes.length, 1, "Node.normalize merges text nodes in one");
+ is(parent1.childNodes[0].length, 5, "test 1-2, Node.normalize merges text nodes values");
+ is(parent2.childNodes.length, 0, "Node.normalize removes empty text nodes even if there is only one text node");
+ is(child2.textContent, "", "Node.normalize doesn't change removed children original content")
+ is(child3.textContent, "bb", "Node.normalize doesn't change removed children original content")
+
+ finish();
+ </script>
+ </body>
+</html>