4 https://bugzilla.mozilla.org/show_bug.cgi?id=498240
7 <title>Test for Bug
498240</title>
8 <script src=
"/tests/SimpleTest/SimpleTest.js"></script>
9 <link rel=
"stylesheet" type=
"text/css" href=
"/tests/SimpleTest/test.css"/>
11 .container { border:
1px solid blue; display:block; }
17 <a target=
"_blank" href=
"https://bugzilla.mozilla.org/show_bug.cgi?id=498240">Mozilla Bug
498240</a>
19 <div id=
"content" style=
"display: none">
23 <script type=
"application/javascript">
25 /** Test for Bug
498240 **/
29 var p = document.createElement('span');
31 p.setAttribute(
"class",
"container");
32 document.body.appendChild(p);
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);
44 function insertClone(node) {
45 var sel = getSelection();
46 var range = sel.getRangeAt(
0);
47 range.insertNode(node.cloneNode(true));
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]);
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()+
"'");
64 function testInsertNode(node) {
67 p = create('a
<b>bc
</b>');
68 select(p.childNodes[
0],
0,p.childNodes[
1],
0);
70 check(p.childNodes[
0],
0,p.childNodes[
3],
0);
72 p = create('d
<b>ef
</b>');
73 select(p.childNodes[
0],
0,p.childNodes[
1],
1);
75 check(p.childNodes[
0],
0,p.childNodes[
3],
1);
77 p = create('g
<b>h
</b>');
78 select(p.childNodes[
0],
0,p.childNodes[
0],
0);
80 check(p.childNodes[
0],
0,p,
2);
82 p = create('i
<b>j
</b>');
83 select(p.childNodes[
0],
1,p.childNodes[
0],
1);
85 check(p.childNodes[
0],
1,p,
2);
87 p = create('k
<b>l
</b>');
88 select(p.childNodes[
0],
0,p.childNodes[
1].childNodes[
0],
0);
90 check(p.childNodes[
0],
0,p.childNodes[
3].childNodes[
0],
0);
92 p = create('m
<b>no
</b>');
93 select(p.childNodes[
0],
1,p.childNodes[
1].childNodes[
0],
0);
95 check(p.childNodes[
0],
1,p.childNodes[
3].childNodes[
0],
0);
97 p = create('p
<b>qr
</b>');
98 select(p.childNodes[
0],
1,p.childNodes[
1].childNodes[
0],
1);
100 check(p.childNodes[
0],
1,p.childNodes[
3].childNodes[
0],
1);
102 p = create('s
<b>tu
</b>');
103 select(p.childNodes[
0],
1,p.childNodes[
1],
0);
105 check(p.childNodes[
0],
1,p.childNodes[
3],
0);
107 p = create('
<i>A
</i><b>BC
</b>');
108 select(p.childNodes[
0],
0,p.childNodes[
1],
0);
110 check(p.childNodes[
0],
0,p.childNodes[
1],
0);
112 p = create('
<i>D
</i><b>EF
</b>');
113 select(p.childNodes[
0],
1,p.childNodes[
1],
1);
115 check(p.childNodes[
0],
1,p.childNodes[
1],
1);
117 p = create('
<i></i><b>GH
</b>');
118 select(p.childNodes[
0],
0,p.childNodes[
1],
0);
120 check(p.childNodes[
0],
0,p.childNodes[
1],
0);
122 p = create('
<i>I
</i><b>J
</b>');
123 select(p,
0,p.childNodes[
1],
0);
125 check(p,
0,p.childNodes[
2],
0);
127 p = create('
<i>K
</i><b>L
</b>');
132 p = create('
<i>M
</i><b>N
</b>');
137 p = create('
<i>O
</i><b>P
</b>');
142 p = create('
<i>Q
</i><b>R
</b>');
147 p = create('
<i>S
</i><b>T
</b>');
149 insertCloneAtEnd(node);
152 p = create('
<i>U
</i><b>V
</b>');
154 insertCloneAtEnd(node);
157 p = create('
<i>X
</i><b>Y
</b>');
159 insertCloneAtEnd(node);
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);
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);
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);
177 function testInvalidNodeType(node) {
179 testInsertNode(node);
180 ok(false,
"Expected an InvalidNodeTypeError");
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");
189 testInsertNode(document.createTextNode('
123'));
191 var i = document.createElement('SPAN')
195 i = document.createDocumentFragment();
196 i.appendChild(document.createTextNode('
789'));
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));
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');
213 todo(false,
"Expected a WrongDocumentError");
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");
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);
228 ok(false,
"Expected a HierarchyRequestError");
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");
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"
239 todo(false,
"InvalidStateError test goes here...");
241 var sel = getSelection();
242 sel.removeAllRanges();
247 SimpleTest.waitForExplicitFinish();
248 addLoadEvent(runTest);