Skip to content

Commit

Permalink
implemented Node.normalize()
Browse files Browse the repository at this point in the history
  • Loading branch information
khodzha committed Mar 14, 2014
1 parent af616db commit 574fba3
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/components/script/dom/bindings/codegen/Bindings.conf
Expand Up @@ -87,6 +87,7 @@ DOMInterfaces = {
'isEqualNode',
'nodeName',
'nodeValue',
'normalize',
'removeChild',
'replaceChild',
'textContent',
Expand Down
24 changes: 22 additions & 2 deletions src/components/script/dom/node.rs
Expand Up @@ -1422,8 +1422,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
Expand Down
35 changes: 35 additions & 0 deletions 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>

5 comments on commit 574fba3

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from Ms2ger
at khodzha@574fba3

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging khodzha/servo/node_normalize = 574fba3 into auto

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

khodzha/servo/node_normalize = 574fba3 merged ok, testing candidate = 2d2fae5

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 2d2fae5

Please sign in to comment.