summaryrefslogtreecommitdiffstats
path: root/dom/base/test/test_bug498240.html
blob: 4bb66659d17b1ea5850913e9ac5b957daf9fda21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=498240
-->
<head>
  <title>Test for Bug 498240</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
<style>
 .container { border: 1px solid blue; display:block; }
 b { color:blue; }
 i { color:magenta; }
</style>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=498240">Mozilla Bug 498240</a>
<p id="display"></p>
<div id="content" style="display: none">
  
</div>
<pre id="test">
<script type="application/javascript">

/** Test for Bug 498240 **/


function create(s) {
  var p = document.createElement('span');
  p.innerHTML = s;
  p.setAttribute("class","container");
  document.body.appendChild(p);
  return p;
}
function select(start, startOffset, end, endOffset) {
  var sel = getSelection();
  sel.removeAllRanges();
  var range = document.createRange();
  range.setStart(start, startOffset);
  range.setEnd(end, endOffset);
  sel.addRange(range);
}

function insertClone(node) {
  var sel = getSelection();
  var range = sel.getRangeAt(0);
  range.insertNode(node.cloneNode(true));
}
function insertCloneAtEnd(node) {
  var sel = getSelection();
  var range = sel.getRangeAt(0);
  range.endContainer.insertBefore(node.cloneNode(true),range.endContainer.childNodes[range.endOffset]);
}

function check(start, startOffset, end, endOffset, s) {
  var sel = getSelection();
  var range = sel.getRangeAt(0);
  is(range.startContainer, start, "wrong start node for range: '"+range.toString()+"'");
  is(range.startOffset, startOffset, "wrong start offset for range: '"+range.toString()+"'");
  is(range.endContainer, end, "wrong end node for range: '"+range.toString()+"'");
  is(range.endOffset, endOffset, "wrong end offset for range: '"+range.toString()+"'");
}

function testInsertNode(node) {
  var p;

  p = create('a<b>bc</b>');
  select(p.childNodes[0],0,p.childNodes[1],0);
  insertClone(node);
  check(p.childNodes[0],0,p.childNodes[3],0);

  p = create('d<b>ef</b>');
  select(p.childNodes[0],0,p.childNodes[1],1);
  insertClone(node);
  check(p.childNodes[0],0,p.childNodes[3],1);

  p = create('g<b>h</b>');
  select(p.childNodes[0],0,p.childNodes[0],0);
  insertClone(node);
  check(p.childNodes[0],0,p,2);

  p = create('i<b>j</b>');
  select(p.childNodes[0],1,p.childNodes[0],1);
  insertClone(node);
  check(p.childNodes[0],1,p,2);

  p = create('k<b>l</b>');
  select(p.childNodes[0],0,p.childNodes[1].childNodes[0],0);
  insertClone(node);
  check(p.childNodes[0],0,p.childNodes[3].childNodes[0],0);

  p = create('m<b>no</b>');
  select(p.childNodes[0],1,p.childNodes[1].childNodes[0],0);
  insertClone(node);
  check(p.childNodes[0],1,p.childNodes[3].childNodes[0],0);

  p = create('p<b>qr</b>');
  select(p.childNodes[0],1,p.childNodes[1].childNodes[0],1);
  insertClone(node);
  check(p.childNodes[0],1,p.childNodes[3].childNodes[0],1);

  p = create('s<b>tu</b>');
  select(p.childNodes[0],1,p.childNodes[1],0);
  insertClone(node);
  check(p.childNodes[0],1,p.childNodes[3],0);

  p = create('<i>A</i><b>BC</b>');
  select(p.childNodes[0],0,p.childNodes[1],0);
  insertClone(node);
  check(p.childNodes[0],0,p.childNodes[1],0);

  p = create('<i>D</i><b>EF</b>');
  select(p.childNodes[0],1,p.childNodes[1],1);
  insertClone(node);
  check(p.childNodes[0],1,p.childNodes[1],1);

  p = create('<i></i><b>GH</b>');
  select(p.childNodes[0],0,p.childNodes[1],0);
  insertClone(node);
  check(p.childNodes[0],0,p.childNodes[1],0);

  p = create('<i>I</i><b>J</b>');
  select(p,0,p.childNodes[1],0);
  insertClone(node);
  check(p,0,p.childNodes[2],0);

  p = create('<i>K</i><b>L</b>');
  select(p,0,p,2);
  insertClone(node);
  check(p,0,p,3);

  p = create('<i>M</i><b>N</b>');
  select(p,1,p,2);
  insertClone(node);
  check(p,1,p,3);

  p = create('<i>O</i><b>P</b>');
  select(p,1,p,1);
  insertClone(node);
  check(p,1,p,2);

  p = create('<i>Q</i><b>R</b>');
  select(p,2,p,2);
  insertClone(node);
  check(p,2,p,3);

  p = create('<i>S</i><b>T</b>');
  select(p,1,p,1);
  insertCloneAtEnd(node);
  check(p,1,p,1);

  p = create('<i>U</i><b>V</b>');
  select(p,2,p,2);
  insertCloneAtEnd(node);
  check(p,2,p,2);

  p = create('<i>X</i><b>Y</b>');
  select(p,0,p,1);
  insertCloneAtEnd(node);
  check(p,0,p,1);

  p = create('<i>X</i><b><s>Y</s></b>');
  select(p,0,p.childNodes[1],1);
  insertCloneAtEnd(node);
  check(p,0,p.childNodes[1],1);

  p = create('<i>Z</i><b></b>');
  select(p,0,p.childNodes[1],0);
  insertCloneAtEnd(node);
  check(p,0,p.childNodes[1],0);

  p = create('<i>ZA</i><b><s>ZB</s><u>ZC</u></b>');
  select(p,0,p.childNodes[1],1);
  insertCloneAtEnd(node);
  check(p,0,p.childNodes[1],1);
}
function testInvalidNodeType(node) {
  try { 
    testInsertNode(node); 
    ok(false,"Expected an InvalidNodeTypeError"); 
  } catch(e) {
    is(e.name, "InvalidNodeTypeError", "Wrong exception, expected InvalidNodeTypeError");
    ok(e instanceof DOMException, "Wrong type of exception: " + e);
    is(e.code, DOMException.INVALID_NODE_TYPE_ERR, "Wrong exception code, expected INVALID_NODE_TYPE_ERR");
  }
}

function runTest() {
  testInsertNode(document.createTextNode('123'));

  var i = document.createElement('SPAN')
  i.innerHTML='456'
  testInsertNode(i);

  i = document.createDocumentFragment();
  i.appendChild(document.createTextNode('789'));
  testInsertNode(i);

  /// DOM2 Traversal and Range Specification 2.13 "insertNode":
  ///   RangeException INVALID_NODE_TYPE_ERR: Raised if newNode is an Attr, Entity, Notation, or Document node.
  // BUG: testInvalidNodeType(document.createAttribute('a'));
  todo(false, "Test insertion of Entity node into range");
  // TODO: testInvalidNodeType(document.createEntity());
  todo(false, "Test insertion of Notation node into range");
  // TODO: testInvalidNodeType(document.createNotation());
  // BUG: testInvalidNodeType(document.implementation.createDocument('http://www.w3.org/1999/xhtml', 'html', null));

  // Intentionally fails because of bug 418755.
  todo(false, "test that Range::insertNode() throws WrongDocumentError when it should");
  i = document.implementation.createDocument('http://www.w3.org/1999/xhtml', 'html', null).createElement('html');
  try { 
    testInsertNode(i);
    todo(false,"Expected a WrongDocumentError"); 
  } catch(e) {
    is(e.name, "WrongDocumentError", "Wrong exception, expected WrongDocumentError");
    ok(e instanceof DOMException, "Wrong type of exception: " + e);
    is(e.code, DOMException.WRONG_DOCUMENT_ERR, "Wrong exception code, expected WRONG_DOCUMENT_ERR");
  }

  // Inserting an ancestor of the start container should throw HierarchyRequestError
  todo(false, "test that Range::insertNode() throws HierarchyRequestError when it should");
  var p = create('<b>IJK</b>');
  select(p.childNodes[0],0,p.childNodes[0],1);
  var sel = getSelection();
  var range = sel.getRangeAt(0);
  try { 
    range.insertNode(p);
    ok(false,"Expected a HierarchyRequestError"); 
  } catch(e) {
    is(e.name, "HierarchyRequestError", "Wrong exception, expected HierarchyRequestError");
    ok(e instanceof DOMException, "Wrong type of exception: " + e);
    is(e.code, DOMException.HIERARCHY_REQUEST_ERR, "Wrong exception code, expected HIERARCHY_REQUEST_ERR");
  }

  // TODO: we should also have a test for:
  /// "HierarchyRequestError: Raised if the container of the start of the Range is of a type
  ///   that does not allow children of the type of newNode"

  todo(false, "InvalidStateError test goes here...");

  var sel = getSelection();
  sel.removeAllRanges();

  SimpleTest.finish();
}

SimpleTest.waitForExplicitFinish();
addLoadEvent(runTest);


</script>
</pre>
</body>
</html>