|
1 <!DOCTYPE HTML> |
|
2 <html> |
|
3 <!-- |
|
4 https://bugzilla.mozilla.org/show_bug.cgi?id=498240 |
|
5 --> |
|
6 <head> |
|
7 <title>Test for Bug 498240</title> |
|
8 <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> |
|
9 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> |
|
10 <style> |
|
11 .container { border: 1px solid blue; display:block; } |
|
12 b { color:blue; } |
|
13 i { color:magenta; } |
|
14 </style> |
|
15 </head> |
|
16 <body> |
|
17 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=498240">Mozilla Bug 498240</a> |
|
18 <p id="display"></p> |
|
19 <div id="content" style="display: none"> |
|
20 |
|
21 </div> |
|
22 <pre id="test"> |
|
23 <script type="application/javascript"> |
|
24 |
|
25 /** Test for Bug 498240 **/ |
|
26 |
|
27 |
|
28 function create(s) { |
|
29 var p = document.createElement('span'); |
|
30 p.innerHTML = s; |
|
31 p.setAttribute("class","container"); |
|
32 document.body.appendChild(p); |
|
33 return p; |
|
34 } |
|
35 function select(start, startOffset, end, endOffset) { |
|
36 var sel = getSelection(); |
|
37 sel.removeAllRanges(); |
|
38 var range = document.createRange(); |
|
39 range.setStart(start, startOffset); |
|
40 range.setEnd(end, endOffset); |
|
41 sel.addRange(range); |
|
42 } |
|
43 |
|
44 function insertClone(node) { |
|
45 var sel = getSelection(); |
|
46 var range = sel.getRangeAt(0); |
|
47 range.insertNode(node.cloneNode(true)); |
|
48 } |
|
49 function insertCloneAtEnd(node) { |
|
50 var sel = getSelection(); |
|
51 var range = sel.getRangeAt(0); |
|
52 range.endContainer.insertBefore(node.cloneNode(true),range.endContainer.childNodes[range.endOffset]); |
|
53 } |
|
54 |
|
55 function check(start, startOffset, end, endOffset, s) { |
|
56 var sel = getSelection(); |
|
57 var range = sel.getRangeAt(0); |
|
58 is(range.startContainer, start, "wrong start node for range: '"+range.toString()+"'"); |
|
59 is(range.startOffset, startOffset, "wrong start offset for range: '"+range.toString()+"'"); |
|
60 is(range.endContainer, end, "wrong end node for range: '"+range.toString()+"'"); |
|
61 is(range.endOffset, endOffset, "wrong end offset for range: '"+range.toString()+"'"); |
|
62 } |
|
63 |
|
64 function testInsertNode(node) { |
|
65 var p; |
|
66 |
|
67 p = create('a<b>bc</b>'); |
|
68 select(p.childNodes[0],0,p.childNodes[1],0); |
|
69 insertClone(node); |
|
70 check(p.childNodes[0],0,p.childNodes[3],0); |
|
71 |
|
72 p = create('d<b>ef</b>'); |
|
73 select(p.childNodes[0],0,p.childNodes[1],1); |
|
74 insertClone(node); |
|
75 check(p.childNodes[0],0,p.childNodes[3],1); |
|
76 |
|
77 p = create('g<b>h</b>'); |
|
78 select(p.childNodes[0],0,p.childNodes[0],0); |
|
79 insertClone(node); |
|
80 check(p.childNodes[0],0,p,2); |
|
81 |
|
82 p = create('i<b>j</b>'); |
|
83 select(p.childNodes[0],1,p.childNodes[0],1); |
|
84 insertClone(node); |
|
85 check(p.childNodes[0],1,p,2); |
|
86 |
|
87 p = create('k<b>l</b>'); |
|
88 select(p.childNodes[0],0,p.childNodes[1].childNodes[0],0); |
|
89 insertClone(node); |
|
90 check(p.childNodes[0],0,p.childNodes[3].childNodes[0],0); |
|
91 |
|
92 p = create('m<b>no</b>'); |
|
93 select(p.childNodes[0],1,p.childNodes[1].childNodes[0],0); |
|
94 insertClone(node); |
|
95 check(p.childNodes[0],1,p.childNodes[3].childNodes[0],0); |
|
96 |
|
97 p = create('p<b>qr</b>'); |
|
98 select(p.childNodes[0],1,p.childNodes[1].childNodes[0],1); |
|
99 insertClone(node); |
|
100 check(p.childNodes[0],1,p.childNodes[3].childNodes[0],1); |
|
101 |
|
102 p = create('s<b>tu</b>'); |
|
103 select(p.childNodes[0],1,p.childNodes[1],0); |
|
104 insertClone(node); |
|
105 check(p.childNodes[0],1,p.childNodes[3],0); |
|
106 |
|
107 p = create('<i>A</i><b>BC</b>'); |
|
108 select(p.childNodes[0],0,p.childNodes[1],0); |
|
109 insertClone(node); |
|
110 check(p.childNodes[0],0,p.childNodes[1],0); |
|
111 |
|
112 p = create('<i>D</i><b>EF</b>'); |
|
113 select(p.childNodes[0],1,p.childNodes[1],1); |
|
114 insertClone(node); |
|
115 check(p.childNodes[0],1,p.childNodes[1],1); |
|
116 |
|
117 p = create('<i></i><b>GH</b>'); |
|
118 select(p.childNodes[0],0,p.childNodes[1],0); |
|
119 insertClone(node); |
|
120 check(p.childNodes[0],0,p.childNodes[1],0); |
|
121 |
|
122 p = create('<i>I</i><b>J</b>'); |
|
123 select(p,0,p.childNodes[1],0); |
|
124 insertClone(node); |
|
125 check(p,0,p.childNodes[2],0); |
|
126 |
|
127 p = create('<i>K</i><b>L</b>'); |
|
128 select(p,0,p,2); |
|
129 insertClone(node); |
|
130 check(p,0,p,3); |
|
131 |
|
132 p = create('<i>M</i><b>N</b>'); |
|
133 select(p,1,p,2); |
|
134 insertClone(node); |
|
135 check(p,1,p,3); |
|
136 |
|
137 p = create('<i>O</i><b>P</b>'); |
|
138 select(p,1,p,1); |
|
139 insertClone(node); |
|
140 check(p,1,p,2); |
|
141 |
|
142 p = create('<i>Q</i><b>R</b>'); |
|
143 select(p,2,p,2); |
|
144 insertClone(node); |
|
145 check(p,2,p,3); |
|
146 |
|
147 p = create('<i>S</i><b>T</b>'); |
|
148 select(p,1,p,1); |
|
149 insertCloneAtEnd(node); |
|
150 check(p,1,p,1); |
|
151 |
|
152 p = create('<i>U</i><b>V</b>'); |
|
153 select(p,2,p,2); |
|
154 insertCloneAtEnd(node); |
|
155 check(p,2,p,2); |
|
156 |
|
157 p = create('<i>X</i><b>Y</b>'); |
|
158 select(p,0,p,1); |
|
159 insertCloneAtEnd(node); |
|
160 check(p,0,p,1); |
|
161 |
|
162 p = create('<i>X</i><b><s>Y</s></b>'); |
|
163 select(p,0,p.childNodes[1],1); |
|
164 insertCloneAtEnd(node); |
|
165 check(p,0,p.childNodes[1],1); |
|
166 |
|
167 p = create('<i>Z</i><b></b>'); |
|
168 select(p,0,p.childNodes[1],0); |
|
169 insertCloneAtEnd(node); |
|
170 check(p,0,p.childNodes[1],0); |
|
171 |
|
172 p = create('<i>ZA</i><b><s>ZB</s><u>ZC</u></b>'); |
|
173 select(p,0,p.childNodes[1],1); |
|
174 insertCloneAtEnd(node); |
|
175 check(p,0,p.childNodes[1],1); |
|
176 } |
|
177 function testInvalidNodeType(node) { |
|
178 try { |
|
179 testInsertNode(node); |
|
180 ok(false,"Expected an InvalidNodeTypeError"); |
|
181 } catch(e) { |
|
182 is(e.name, "InvalidNodeTypeError", "Wrong exception, expected InvalidNodeTypeError"); |
|
183 ok(e instanceof DOMException, "Wrong type of exception: " + e); |
|
184 is(e.code, DOMException.INVALID_NODE_TYPE_ERR, "Wrong exception code, expected INVALID_NODE_TYPE_ERR"); |
|
185 } |
|
186 } |
|
187 |
|
188 function runTest() { |
|
189 testInsertNode(document.createTextNode('123')); |
|
190 |
|
191 var i = document.createElement('SPAN') |
|
192 i.innerHTML='456' |
|
193 testInsertNode(i); |
|
194 |
|
195 i = document.createDocumentFragment(); |
|
196 i.appendChild(document.createTextNode('789')); |
|
197 testInsertNode(i); |
|
198 |
|
199 /// DOM2 Traversal and Range Specification 2.13 "insertNode": |
|
200 /// RangeException INVALID_NODE_TYPE_ERR: Raised if newNode is an Attr, Entity, Notation, or Document node. |
|
201 // BUG: testInvalidNodeType(document.createAttribute('a')); |
|
202 todo(false, "Test insertion of Entity node into range"); |
|
203 // TODO: testInvalidNodeType(document.createEntity()); |
|
204 todo(false, "Test insertion of Notation node into range"); |
|
205 // TODO: testInvalidNodeType(document.createNotation()); |
|
206 // BUG: testInvalidNodeType(document.implementation.createDocument('http://www.w3.org/1999/xhtml', 'html', null)); |
|
207 |
|
208 // Intentionally fails because of bug 418755. |
|
209 todo(false, "test that Range::insertNode() throws WrongDocumentError when it should"); |
|
210 i = document.implementation.createDocument('http://www.w3.org/1999/xhtml', 'html', null).createElement('html'); |
|
211 try { |
|
212 testInsertNode(i); |
|
213 todo(false,"Expected a WrongDocumentError"); |
|
214 } catch(e) { |
|
215 is(e.name, "WrongDocumentError", "Wrong exception, expected WrongDocumentError"); |
|
216 ok(e instanceof DOMException, "Wrong type of exception: " + e); |
|
217 is(e.code, DOMException.WRONG_DOCUMENT_ERR, "Wrong exception code, expected WRONG_DOCUMENT_ERR"); |
|
218 } |
|
219 |
|
220 // Inserting an ancestor of the start container should throw HierarchyRequestError |
|
221 todo(false, "test that Range::insertNode() throws HierarchyRequestError when it should"); |
|
222 var p = create('<b>IJK</b>'); |
|
223 select(p.childNodes[0],0,p.childNodes[0],1); |
|
224 var sel = getSelection(); |
|
225 var range = sel.getRangeAt(0); |
|
226 try { |
|
227 range.insertNode(p); |
|
228 ok(false,"Expected a HierarchyRequestError"); |
|
229 } catch(e) { |
|
230 is(e.name, "HierarchyRequestError", "Wrong exception, expected HierarchyRequestError"); |
|
231 ok(e instanceof DOMException, "Wrong type of exception: " + e); |
|
232 is(e.code, DOMException.HIERARCHY_REQUEST_ERR, "Wrong exception code, expected HIERARCHY_REQUEST_ERR"); |
|
233 } |
|
234 |
|
235 // TODO: we should also have a test for: |
|
236 /// "HierarchyRequestError: Raised if the container of the start of the Range is of a type |
|
237 /// that does not allow children of the type of newNode" |
|
238 |
|
239 todo(false, "InvalidStateError test goes here..."); |
|
240 |
|
241 var sel = getSelection(); |
|
242 sel.removeAllRanges(); |
|
243 |
|
244 SimpleTest.finish(); |
|
245 } |
|
246 |
|
247 SimpleTest.waitForExplicitFinish(); |
|
248 addLoadEvent(runTest); |
|
249 |
|
250 |
|
251 </script> |
|
252 </pre> |
|
253 </body> |
|
254 </html> |