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