Node-normalize.html (3312B)
1 <!DOCTYPE html> 2 <title>Node.normalize()</title> 3 <script src="/resources/testharness.js"></script> 4 <script src="/resources/testharnessreport.js"></script> 5 <div id=log></div> 6 <script> 7 test(function() { 8 var df = document.createDocumentFragment(), 9 t1 = document.createTextNode("1"), 10 t2 = document.createTextNode("2"), 11 t3 = document.createTextNode("3"), 12 t4 = document.createTextNode("4") 13 df.appendChild(t1) 14 df.appendChild(t2) 15 assert_equals(df.childNodes.length, 2) 16 assert_equals(df.textContent, "12") 17 var el = document.createElement('x') 18 df.appendChild(el) 19 el.appendChild(t3) 20 el.appendChild(t4) 21 document.normalize() 22 assert_equals(el.childNodes.length, 2) 23 assert_equals(el.textContent, "34") 24 assert_equals(df.childNodes.length, 3) 25 assert_equals(t1.data, "1") 26 df.normalize() 27 assert_equals(df.childNodes.length, 2) 28 assert_equals(df.firstChild, t1) 29 assert_equals(t1.data, "12") 30 assert_equals(t2.data, "2") 31 assert_equals(el.firstChild, t3) 32 assert_equals(t3.data, "34") 33 assert_equals(t4.data, "4") 34 }) 35 36 // https://www.w3.org/Bugs/Public/show_bug.cgi?id=19837 37 test(function() { 38 var div = document.createElement("div") 39 var t1 = div.appendChild(document.createTextNode("")) 40 var t2 = div.appendChild(document.createTextNode("a")) 41 var t3 = div.appendChild(document.createTextNode("")) 42 assert_array_equals(div.childNodes, [t1, t2, t3]) 43 div.normalize(); 44 assert_array_equals(div.childNodes, [t2]) 45 }, "Empty text nodes separated by a non-empty text node") 46 test(function() { 47 var div = document.createElement("div") 48 var t1 = div.appendChild(document.createTextNode("")) 49 var t2 = div.appendChild(document.createTextNode("")) 50 assert_array_equals(div.childNodes, [t1, t2]) 51 div.normalize(); 52 assert_array_equals(div.childNodes, []) 53 }, "Empty text nodes") 54 55 // The specification for normalize is clear that only "exclusive Text 56 // nodes" are to be modified. This excludes CDATASection nodes, which 57 // derive from Text. Naïve implementations may fail to skip 58 // CDATASection nodes, or even worse, try to test textContent or 59 // nodeValue without taking care to check the node type. They will 60 // fail this test. 61 test(function() { 62 // We create an XML document so that we can create CDATASection. 63 // Except for the CDATASection the result should be the same for 64 // an HTML document. (No non-Text node should be touched.) 65 var doc = new DOMParser().parseFromString("<div/>", "text/xml") 66 var div = doc.documentElement 67 var t1 = div.appendChild(doc.createTextNode("a")) 68 // The first parameter is the "target" of the processing 69 // instruction, and the 2nd is the text content. 70 var t2 = div.appendChild(doc.createProcessingInstruction("pi", "")) 71 var t3 = div.appendChild(doc.createTextNode("b")) 72 var t4 = div.appendChild(doc.createCDATASection("")) 73 var t5 = div.appendChild(doc.createTextNode("c")) 74 var t6 = div.appendChild(doc.createComment("")) 75 var t7 = div.appendChild(doc.createTextNode("d")) 76 var t8 = div.appendChild(doc.createElement("el")) 77 var t9 = div.appendChild(doc.createTextNode("e")) 78 var expected = [t1, t2, t3, t4, t5, t6, t7, t8, t9] 79 assert_array_equals(div.childNodes, expected) 80 div.normalize() 81 assert_array_equals(div.childNodes, expected) 82 }, "Non-text nodes with empty textContent values.") 83 </script>