1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <link rel="stylesheet" href="../../js/resources/js-test-style.css"> 5 <script src="../../js/resources/js-test-pre.js"></script> 6 </head> 7 <body id="a"> 8 <p id="description"></p> 9 <div id="console"></div> 10 <script> 11 12 description("Test that different ways of changing an element's id all work properly."); 13 14 debug("\n1. Check id after parsing."); 15 shouldBe('document.getElementById("a")', 'document.body'); 16 shouldBe('document.body.id', '"a"'); 17 shouldBe('document.body.getAttributeNode("id").isId', 'true'); 18 shouldBe('document.body.getAttributeNode("id").textContent', '"a"'); 19 20 debug("\n2. Change Attr.value."); 21 document.body.getAttributeNode("id").value = "b"; 22 shouldBe('document.getElementById("a")', 'null'); 23 shouldBe('document.getElementById("b")', 'document.body'); 24 shouldBe('document.body.getAttributeNode("id").textContent', '"b"'); 25 26 debug("\n3. Change HTMLElement.id."); 27 document.body.id = "c"; 28 shouldBe('document.getElementById("b")', 'null'); 29 shouldBe('document.getElementById("c")', 'document.body'); 30 shouldBe('document.body.getAttributeNode("id").textContent', '"c"'); 31 32 debug("\n4. Change id attribute via setAttribute()."); 33 document.body.setAttribute("id", "d"); 34 shouldBe('document.getElementById("c")', 'null'); 35 shouldBe('document.getElementById("d")', 'document.body'); 36 shouldBe('document.body.getAttributeNode("id").textContent', '"d"'); 37 38 debug("\n5. Change id attribute via setAttributeNS()."); 39 document.body.setAttributeNS(null, "id", "e"); 40 shouldBe('document.getElementById("d")', 'null'); 41 shouldBe('document.getElementById("e")', 'document.body'); 42 shouldBe('document.body.getAttributeNode("id").textContent', '"e"'); 43 44 var attrNode = document.body.getAttributeNode("id"); 45 46 debug("\n6. Change Attr.nodeValue."); 47 document.body.getAttributeNode("id").nodeValue = "f"; 48 shouldBe('document.getElementById("e")', 'null'); 49 shouldBe('document.getElementById("f")', 'document.body'); 50 shouldBe('document.body.id', '"f"'); 51 shouldBe('document.body.getAttribute("id")', '"f"'); 52 shouldBe('attrNode.textContent', '"f"'); 53 shouldBe('attrNode.childNodes.length', '1'); 54 55 // Firefox doesn't support these for Attr nodes. 56 debug("\n7. Attr.replaceChild()."); 57 try { 58 attrNode.replaceChild(document.createTextNode("g"), attrNode.firstChild); 59 shouldBe('document.getElementById("f")', 'null'); 60 shouldBe('document.getElementById("g")', 'document.body'); 61 shouldBe('document.body.id', '"g"'); 62 shouldBe('document.body.getAttribute("id")', '"g"'); 63 shouldBe('attrNode.textContent', '"g"'); 64 shouldBe('attrNode.childNodes.length', '1'); 65 } catch (ex) { 66 debug(ex); 67 } 68 69 debug("\n8. Attr.insertBefore()."); 70 try { 71 attrNode.insertBefore(document.createTextNode("0"), attrNode.firstChild); 72 shouldBe('document.getElementById("g")', 'null'); 73 shouldBe('document.getElementById("0g")', 'document.body'); 74 shouldBe('document.body.id', '"0g"'); 75 shouldBe('document.body.getAttribute("id")', '"0g"'); 76 shouldBe('attrNode.textContent', '"0g"'); 77 shouldBe('attrNode.childNodes.length', '2'); 78 } catch (ex) { 79 debug(ex); 80 } 81 82 debug("\n9. attr.appendChild()."); 83 try { 84 attrNode.appendChild(document.createTextNode("2")); 85 shouldBe('document.getElementById("0g")', 'null'); 86 shouldBe('document.getElementById("0g2")', 'document.body'); 87 shouldBe('document.body.id', '"0g2"'); 88 shouldBe('document.body.getAttribute("id")', '"0g2"'); 89 shouldBe('attrNode.textContent', '"0g2"'); 90 shouldBe('attrNode.childNodes.length', '3'); 91 } catch (ex) { 92 debug(ex); 93 } 94 95 debug("\n10. Attr.removeChild()"); 96 attrNode.nodeValue = "h"; 97 attrNode.removeChild(attrNode.firstChild); 98 shouldBe('document.body.getAttributeNode("id").childNodes.length', '0'); 99 shouldBe('document.getElementById("h")', 'null'); 100 shouldBe('document.getElementById("")', 'null'); 101 shouldBe('document.body.id', '""'); 102 shouldBe('document.body.getAttribute("id")', '""'); 103 shouldBe('document.body.getAttributeNode("id").textContent', '""'); 104 105 debug("\n11. Changing Text.nodeValue."); 106 attrNode.nodeValue = "h"; 107 attrNode.firstChild.nodeValue = "i"; 108 shouldBe('attrNode.firstChild.nodeValue', '"i"'); 109 shouldBe('document.getElementById("i")', 'document.body'); 110 shouldBe('document.body.id', '"i"'); 111 shouldBe('document.body.getAttribute("id")', '"i"'); 112 shouldBe('attrNode.textContent', '"i"'); 113 shouldBe('attrNode.childNodes.length', '1'); 114 115 debug("\n12. Chnaging Attr.textContent."); 116 attrNode.textContent = "hi"; 117 shouldBe('document.getElementById("i")', 'null'); 118 shouldBe('document.getElementById("hi")', 'document.body'); 119 shouldBe('document.body.id', '"hi"'); 120 shouldBe('document.body.getAttribute("id")', '"hi"'); 121 shouldBe('attrNode.textContent', '"hi"'); 122 shouldBe('attrNode.childNodes.length', '1'); 123 124 debug("\n13. Text.splitText()."); 125 attrNode.firstChild.splitText(1); 126 shouldBe('document.getElementById("hi")', 'document.body'); 127 shouldBe('document.body.id', '"hi"'); 128 shouldBe('document.body.getAttribute("id")', '"hi"'); 129 shouldBe('document.body.getAttributeNode("id").textContent', '"hi"'); 130 shouldBe('document.body.getAttributeNode("id").childNodes.length', '2'); 131 132 debug("\n14. Node.normalize(), joining text nodes."); 133 attrNode.normalize(); 134 shouldBe('document.getElementById("hi")', 'document.body'); 135 shouldBe('document.body.id', '"hi"'); 136 shouldBe('document.body.getAttribute("id")', '"hi"'); 137 shouldBe('document.body.getAttributeNode("id").textContent', '"hi"'); 138 shouldBe('document.body.getAttributeNode("id").childNodes.length', '1'); 139 140 debug("\n15. Changing Attr.nodeValue."); 141 attrNode.nodeValue = "foo"; 142 attrNode.firstChild.replaceWholeText("j"); 143 shouldBe('document.getElementById("hi")', 'null'); 144 shouldBe('document.getElementById("j")', 'document.body'); 145 shouldBe('document.body.id', '"j"'); 146 shouldBe('document.body.getAttribute("id")', '"j"'); 147 shouldBe('attrNode.textContent', '"j"'); 148 shouldBe('attrNode.childNodes.length', '1'); 149 150 debug("\n16. Changing Text.data."); 151 attrNode.firstChild.data = "k"; 152 shouldBe('document.getElementById("j")', 'null'); 153 shouldBe('document.getElementById("k")', 'document.body'); 154 shouldBe('document.body.id', '"k"'); 155 shouldBe('document.body.getAttribute("id")', '"k"'); 156 shouldBe('attrNode.textContent', '"k"'); 157 shouldBe('attrNode.childNodes.length', '1'); 158 159 debug("\n17. Changing text child with appendData()."); 160 attrNode.firstChild.appendData("l"); 161 shouldBe('document.getElementById("k")', 'null'); 162 shouldBe('document.getElementById("kl")', 'document.body'); 163 shouldBe('document.body.id', '"kl"'); 164 shouldBe('document.body.getAttribute("id")', '"kl"'); 165 shouldBe('attrNode.textContent', '"kl"'); 166 shouldBe('attrNode.childNodes.length', '1'); 167 168 debug("\n18. Changing text child with insertData()."); 169 attrNode.firstChild.insertData(1, "1"); 170 shouldBe('document.getElementById("kl")', 'null'); 171 shouldBe('document.getElementById("k1l")', 'document.body'); 172 shouldBe('document.body.id', '"k1l"'); 173 shouldBe('document.body.getAttribute("id")', '"k1l"'); 174 shouldBe('attrNode.textContent', '"k1l"'); 175 shouldBe('attrNode.childNodes.length', '1'); 176 177 debug("\n19. Changing text child with deleteData()."); 178 attrNode.firstChild.deleteData(0, 2); 179 shouldBe('document.getElementById("k1l")', 'null'); 180 shouldBe('document.getElementById("l")', 'document.body'); 181 shouldBe('document.body.id', '"l"'); 182 shouldBe('document.body.getAttribute("id")', '"l"'); 183 shouldBe('attrNode.textContent', '"l"'); 184 shouldBe('attrNode.childNodes.length', '1'); 185 186 debug("\n20. Changing text child with replaceData()."); 187 attrNode.firstChild.replaceData(0, 1, "mn"); 188 shouldBe('document.getElementById("l")', 'null'); 189 shouldBe('document.getElementById("mn")', 'document.body'); 190 shouldBe('document.body.id', '"mn"'); 191 shouldBe('document.body.getAttribute("id")', '"mn"'); 192 shouldBe('attrNode.textContent', '"mn"'); 193 shouldBe('attrNode.childNodes.length', '1'); 194 195 debug("\n21. Remove an Attr node."); 196 document.body.removeAttributeNode(attrNode); 197 shouldBe('document.body.id', '""'); 198 shouldBe('document.getElementById("mn")', 'null'); 199 shouldBe('document.body.getAttribute("id")', 'null'); 200 shouldBe('document.body.getAttributeNode("id")', 'null'); 201 202 debug("\n22. Add an Attr node."); 203 var attrNode = document.createAttribute("id"); 204 attrNode.value = "o"; 205 document.body.setAttributeNode(attrNode); 206 shouldBe('document.getElementById("o")', 'document.body'); 207 shouldBe('document.body.id', '"o"'); 208 shouldBe('document.body.getAttribute("id")', '"o"'); 209 210 debug("\n23. Add an Attr node over an existing one."); 211 var attrNode = document.createAttribute("id"); 212 attrNode.value = "p"; 213 document.body.setAttributeNode(attrNode); 214 shouldBe('document.getElementById("o")', 'null'); 215 shouldBe('document.getElementById("p")', 'document.body'); 216 shouldBe('document.body.id', '"p"'); 217 shouldBe('document.body.getAttribute("id")', '"p"'); 218 219 var successfullyParsed = true; 220 </script> 221 <script src="../../js/resources/js-test-post.js"></script> 222 </body> 223 </html> 224