2 "This test checks mutation of the DOM and how it affects Ranges."
29 if (node == paragraph)
44 function description(range)
46 return name(range.startContainer) + "," + range.startOffset + " -> " + name(range.endContainer) + "," + range.endOffset + ": " + range.toString();
49 function makeRange(sc, so, ec, eo)
51 var newRange = document.createRange();
52 newRange.setStart(sc, so);
53 newRange.setEnd(ec, eo);
59 paragraph = document.createElement("p");
60 text = document.createTextNode("Abcd efgh XY blah ijkl");
61 paragraph.appendChild(text);
62 range = makeRange(text, 11, text, 19);
65 function createTestElement(name)
67 var element = document.createElement("em");
68 var text = document.createTextNode(name);
69 element.appendChild(text);
75 paragraph = document.createElement("p");
76 a = createTestElement("a");
77 b = createTestElement("b");
78 c = createTestElement("c");
79 d = createTestElement("d");
80 e = createTestElement("e");
82 paragraph.appendChild(a);
83 paragraph.appendChild(b);
84 paragraph.appendChild(c);
85 paragraph.appendChild(d);
86 range = makeRange(paragraph, 1, paragraph, 3);
91 paragraph = document.createElement("p");
92 a = document.createTextNode("ax");
93 b = document.createTextNode("by");
95 paragraph.appendChild(a);
96 paragraph.appendChild(b);
98 range = makeRange(a, 1, b, 1);
103 paragraph = document.createElement("p");
104 a = document.createTextNode("abcd");
106 paragraph.appendChild(a);
108 range = makeRange(a, 1, a, 3);
113 section = document.createElement("div");
114 paragraph = document.createElement("p");
115 a = document.createTextNode("abcde");
117 section.appendChild(paragraph);
118 paragraph.appendChild(a);
120 range = makeRange(a, 2, section, 1);
123 // First, tests from the DOM Level 2 specification.
125 // DOM Level 2 Traversal Range, 2.12.1. Insertions, example 1.
127 text.insertData(10, "inserted text");
128 shouldBe('description(range)', '"text,24 -> text,32: Y blah i"');
130 // DOM Level 2 Traversal Range, 2.12.1. Insertions, example 2.
131 // Firefox does not match the DOM Level 2 specification on this one.
133 text.insertData(11, "inserted text");
134 shouldBe('description(range)', '"text,11 -> text,32: inserted textY blah i"');
136 // DOM Level 2 Traversal Range, 2.12.1. Insertions, example 3.
138 text.insertData(12, "inserted text");
139 shouldBe('description(range)', '"text,11 -> text,32: Yinserted text blah i"');
141 // DOM Level 2 Traversal Range, 2.12.1. Insertions, example 4.
143 text.insertData(17, "inserted text");
144 shouldBe('description(range)', '"text,11 -> text,32: Y blahinserted text i"');
146 // Similar test at the range end.
148 text.insertData(18, "inserted text");
149 shouldBe('description(range)', '"text,11 -> text,32: Y blah inserted texti"');
151 // Similar test at the range end.
153 text.insertData(19, "inserted text");
154 shouldBe('description(range)', '"text,11 -> text,19: Y blah i"');
156 // DOM Level 2 Traversal Range, 2.12.2. Deletions, example 1.
157 paragraph = document.createElement("p");
158 text = document.createTextNode("Abcd efgh The Range ijkl");
159 paragraph.appendChild(text);
160 range = makeRange(text, 11, text, 21);
161 text.deleteData(5, 8);
162 shouldBe('description(range)', '"text,5 -> text,13: Range i"');
164 // DOM Level 2 Traversal Range, 2.12.2. Deletions, example 2.
165 paragraph = document.createElement("p");
166 text = document.createTextNode("Abcd efgh The Range ijkl");
167 paragraph.appendChild(text);
168 range = makeRange(text, 11, text, 21);
169 text.deleteData(5, 17);
170 shouldBe('description(range)', '"text,5 -> text,5: "');
172 // DOM Level 2 Traversal Range, 2.12.2. Deletions, example 3.
173 // Firefox does not match the DOM Level 2 specification on this one.
174 // Or maybe I wrote the test wrong?
175 paragraph = document.createElement("p");
176 text1 = document.createTextNode("ABCD efgh The ");
177 paragraph.appendChild(text1);
178 em = document.createElement("em");
179 paragraph.appendChild(em);
180 text2 = document.createTextNode("Range");
181 em.appendChild(text2);
182 text3 = document.createTextNode(" ijkl");
183 paragraph.appendChild(text3);
184 range = makeRange(text1, 11, text2, 5);
185 makeRange(text1, 5, text2, 1).deleteContents();
186 shouldBe('description(range)', '"text1,5 -> text2,4: ange"');
188 // DOM Level 2 Traversal Range, 2.12.2. Deletions, example 4.
189 paragraph = document.createElement("p");
190 text = document.createTextNode("Abcd efgh The Range ijkl");
191 paragraph.appendChild(text);
192 range = makeRange(text, 11, text, 21);
193 text.deleteData(5, 6);
194 shouldBe('description(range)', '"text,5 -> text,15: he Range i"');
196 // DOM Level 2 Traversal Range, 2.12.2. Deletions, example 5.
197 paragraph = document.createElement("p");
198 text1 = document.createTextNode("Abcd ");
199 paragraph.appendChild(text1);
200 em = document.createElement("em");
201 paragraph.appendChild(em);
202 text2 = document.createTextNode("efgh The Range ij");
203 em.appendChild(text2);
204 text3 = document.createTextNode("kl");
205 paragraph.appendChild(text3);
206 range = makeRange(text2, 6, text2, 16);
207 makeRange(paragraph, 1, paragraph, 2).deleteContents();
208 shouldBe('paragraph.childNodes.length', '2');
209 shouldBe('text1.length', '5');
210 shouldBe('description(range)', '"paragraph,1 -> paragraph,1: "');
211 paragraph.normalize();
212 shouldBe('paragraph.childNodes.length', '1');
213 shouldBe('text1.length', '7');
214 shouldBe('description(range)', '"text1,5 -> text1,5: "');
216 // Inserting a node in the start container, before the range start offset.
218 paragraph.insertBefore(e, a);
219 shouldBe('description(range)', '"paragraph,2 -> paragraph,4: bc"');
221 // Inserting a node in the start container, at the range start offset.
223 paragraph.insertBefore(e, b);
224 shouldBe('description(range)', '"paragraph,1 -> paragraph,4: ebc"');
226 // Inserting a node in the start container, between start and end.
228 paragraph.insertBefore(e, c);
229 shouldBe('description(range)', '"paragraph,1 -> paragraph,4: bec"');
231 // Inserting a node in the end container, at the range end offset.
233 paragraph.insertBefore(e, d);
234 shouldBe('description(range)', '"paragraph,1 -> paragraph,3: bc"');
236 // Inserting a node in the end container, after the range end offset.
238 paragraph.appendChild(e);
239 shouldBe('description(range)', '"paragraph,1 -> paragraph,3: bc"');
241 // Removing the start container of a range.
243 range = makeRange(b, 0, d, 1);
244 paragraph.removeChild(b);
245 shouldBe('description(range)', '"paragraph,1 -> d,1: cd"');
247 range = makeRange(b, 1, d, 0);
248 paragraph.removeChild(b);
249 shouldBe('description(range)', '"paragraph,1 -> d,0: c"');
251 // Removing the end container of a range.
253 range = makeRange(b, 0, d, 1);
254 paragraph.removeChild(d);
255 shouldBe('description(range)', '"b,0 -> paragraph,3: bc"');
257 range = makeRange(b, 1, d, 0);
258 paragraph.removeChild(d);
259 shouldBe('description(range)', '"b,1 -> paragraph,3: c"');
261 // Calling normalize with a range that has an endpoint in a text node that gets merged into another.
262 // Firefox does not do what the DOM Level 2 specification seems to call for on this one.
264 paragraph.normalize();
265 shouldBe('description(range)', '"a,1 -> a,3: xb"');
267 // Calling splitText when a range has an endpoint on in the piece that gets made into a new text node.
268 // Firefox does not do what the DOM Level 2 specification seems to call for on this one.
271 shouldBe('description(range)', '"a,1 -> b,2: bc"');
274 shouldBe('description(range)', '"a,1 -> b,1: bc"');
277 shouldBe('description(range)', '"a,1 -> a,3: bc"');
279 // Range test in Acid3.
281 shouldBe('description(range)', '"a,2 -> section,1: cde"');
282 section.removeChild(paragraph);
283 shouldBe('description(range)', '"section,0 -> section,0: "');
285 // Children in a range being removed, but by DOM mutation (not CharacterData mutation).
286 // Test CharacterData.replaceData cases?
287 // Test Element.innerHTML cases (setting it)?
288 // Test Element.innerText cases (setting it)?
289 // Test Element.outerHTML cases (setting it)?
290 // Test Element.outerText cases (setting it)?
291 // Test Node.replaceChild cases?
292 // Test cases where Node.insertBefore/appendChild has a side effect of deleting the node from a range.
293 // Test Range.deleteContents cases?
294 // Test Range.extractContents cases?
295 // Test Range.surroundContents cases?
296 // Test Text.replaceWholeText cases?