Source code

Revision control

Copy as Markdown

Other Tools

Test Info:

<!DOCTYPE html>
<title>Node.normalize()</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<div id=log></div>
<script>
test(function() {
var df = document.createDocumentFragment(),
t1 = document.createTextNode("1"),
t2 = document.createTextNode("2"),
t3 = document.createTextNode("3"),
t4 = document.createTextNode("4")
df.appendChild(t1)
df.appendChild(t2)
assert_equals(df.childNodes.length, 2)
assert_equals(df.textContent, "12")
var el = document.createElement('x')
df.appendChild(el)
el.appendChild(t3)
el.appendChild(t4)
document.normalize()
assert_equals(el.childNodes.length, 2)
assert_equals(el.textContent, "34")
assert_equals(df.childNodes.length, 3)
assert_equals(t1.data, "1")
df.normalize()
assert_equals(df.childNodes.length, 2)
assert_equals(df.firstChild, t1)
assert_equals(t1.data, "12")
assert_equals(t2.data, "2")
assert_equals(el.firstChild, t3)
assert_equals(t3.data, "34")
assert_equals(t4.data, "4")
})
test(function() {
var div = document.createElement("div")
var t1 = div.appendChild(document.createTextNode(""))
var t2 = div.appendChild(document.createTextNode("a"))
var t3 = div.appendChild(document.createTextNode(""))
assert_array_equals(div.childNodes, [t1, t2, t3])
div.normalize();
assert_array_equals(div.childNodes, [t2])
}, "Empty text nodes separated by a non-empty text node")
test(function() {
var div = document.createElement("div")
var t1 = div.appendChild(document.createTextNode(""))
var t2 = div.appendChild(document.createTextNode(""))
assert_array_equals(div.childNodes, [t1, t2])
div.normalize();
assert_array_equals(div.childNodes, [])
}, "Empty text nodes")
// The specification for normalize is clear that only "exclusive Text
// nodes" are to be modified. This excludes CDATASection nodes, which
// derive from Text. Naïve implementations may fail to skip
// CDATASection nodes, or even worse, try to test textContent or
// nodeValue without taking care to check the node type. They will
// fail this test.
test(function() {
// We create an XML document so that we can create CDATASection.
// Except for the CDATASection the result should be the same for
// an HTML document. (No non-Text node should be touched.)
var doc = new DOMParser().parseFromString("<div/>", "text/xml")
var div = doc.documentElement
var t1 = div.appendChild(doc.createTextNode("a"))
// The first parameter is the "target" of the processing
// instruction, and the 2nd is the text content.
var t2 = div.appendChild(doc.createProcessingInstruction("pi", ""))
var t3 = div.appendChild(doc.createTextNode("b"))
var t4 = div.appendChild(doc.createCDATASection(""))
var t5 = div.appendChild(doc.createTextNode("c"))
var t6 = div.appendChild(doc.createComment(""))
var t7 = div.appendChild(doc.createTextNode("d"))
var t8 = div.appendChild(doc.createElement("el"))
var t9 = div.appendChild(doc.createTextNode("e"))
var expected = [t1, t2, t3, t4, t5, t6, t7, t8, t9]
assert_array_equals(div.childNodes, expected)
div.normalize()
assert_array_equals(div.childNodes, expected)
}, "Non-text nodes with empty textContent values.")
</script>