summaryrefslogtreecommitdiffstats
path: root/dom/base/test/test_textnode_split_in_selection.html
blob: 2bd5c492015b9de61c9416db974cf1b4a70fa3a8 (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
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=803924
-->
<head>
  <meta charset="utf-8">
  <title>Test for Bug 803924</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=803924">Mozilla Bug 803924</a>
<p id="display"></p>
<div id="content" style="display: none">
  
</div>
<pre id="test">
<script type="application/javascript">

/** Test for Bug 803924 **/

var sel = document.getSelection();
var flush = true;
var dry = true;
var run = "";
var empty_range;
var empty_first_text_range;
var empty_last_text_range;
var full_range;

function check(range, expected, test)
{
  is(""+range, expected, test);
  is(""+empty_range, "", "empty range test after: "+test);
  is(""+empty_first_text_range, "", "empty first text range test after: "+test);
  if (empty_last_text_range) is(""+empty_last_text_range, "", "empty last text range test after: "+test);
  is(""+full_range, full_range.startContainer.textContent, "full range test after: "+test);
}

function newDiv()
{
  var div = document.createElement('div');
  for (var i = 0; i < arguments.length; ++i) {
    div.appendChild(document.createTextNode(arguments[i]));
  }
  document.body.appendChild(div)
  empty_range = document.createRange();
  empty_range.setStart(div,0);
  empty_range.setEnd(div,0);
  var firstTextNode = div.childNodes[0];
  var lastTextNode = div.childNodes[div.childNodes.length - 1];
  empty_first_text_range = document.createRange();
  empty_first_text_range.setStart(firstTextNode,0);
  empty_first_text_range.setEnd(firstTextNode,0);
  empty_last_text_range = null;
  if (firstTextNode != lastTextNode) {
    empty_last_text_range = document.createRange();
    empty_last_text_range.setStart(lastTextNode,0);
    empty_last_text_range.setEnd(lastTextNode,0);
  }
  full_range = document.createRange();
  full_range.setStart(div,0);
  full_range.setEnd(div,div.childNodes.length);
  return div;
}

function selEnd(div,child,index,split,s)
{
  var start = div.childNodes[child];
  var r = document.createRange();
  sel.addRange(r);
  r.setStart(start, index);
  r.setEnd(div, div.childNodes.length);
  if (!dry) start.splitText(split);
  check(r,s,run+" selEnd "+child+","+index+","+split);
}

function selStart(div,child,index,split,s)
{
  if (flush) document.body.getClientRects();
  var start = div.childNodes[child];
  var r = document.createRange();
  sel.addRange(r);
  r.setStart(div, 0);
  r.setEnd(start, index);
  if (!dry) start.splitText(split);
  check(r,s,run+" selStart "+child+","+index+","+split);
}

function selMiddleStart(div,child,index,split,s)
{
  if (flush) document.body.getClientRects();
  var start = div.childNodes[child];
  var r = document.createRange();
  sel.addRange(r);
  r.setStart(div, 1);
  r.setEnd(start, index);
  if (!dry) start.splitText(split);
  check(r,s,run+" selMiddleStart "+child+","+index+","+split);
}

function selMiddleEnd(div,child,index,split,s)
{
  if (flush) document.body.getClientRects();
  var start = div.childNodes[child];
  var r = document.createRange();
  sel.addRange(r);
  r.setStart(start, index);
  r.setEnd(div, 2);
  if (!dry) start.splitText(split);
  check(r,s,run+" selMiddleEnd "+child+","+index+","+split);
}

function splitBefore(div,child,index,split,s)
{
  if (flush) document.body.getClientRects();
  var start = div.childNodes[child];
  var r = document.createRange();
  sel.addRange(r);
  r.setStart(div, 1);
  r.setEnd(start, index);
  if (!dry) div.childNodes[0].splitText(split);
  check(r,s,run+" splitBefore "+child+","+index+","+split);
}

function runTests(s)
{
  run = s+":";
  selEnd(newDiv('111'), 0,0,0,'111');
  selEnd(newDiv('111'), 0,0,1,'111');
  selEnd(newDiv('111'), 0,0,3,'111');
  selEnd(newDiv(''), 0,0,0,'');
  selEnd(newDiv('111'), 0,1,0,'11');
  selEnd(newDiv('111'), 0,2,1,'1');
  selEnd(newDiv('111'), 0,1,3,'11');
  selEnd(newDiv('111','222'), 0,1,0,'11222');
  selEnd(newDiv('111','222'), 0,2,1,'1222');
  selEnd(newDiv('111','222'), 0,1,3,'11222');
  selEnd(newDiv('111','222'), 1,1,0,'22');
  selEnd(newDiv('111','222'), 1,2,1,'2');
  selEnd(newDiv('','222'), 1,1,1,'22');
  selEnd(newDiv('','222'), 0,0,0,'222');
  selEnd(newDiv('111',''), 0,1,0,'11');
  selEnd(newDiv('111','222'), 1,1,3,'22');
  selEnd(newDiv('111','222','333'), 1,1,0,'22333');
  selEnd(newDiv('111','222','333'), 1,2,1,'2333');
  selEnd(newDiv('111','222','333'), 1,1,3,'22333');
  selEnd(newDiv('111','222',''), 1,1,3,'22');
  selEnd(newDiv('111','','333'), 0,1,3,'11333');

  selStart(newDiv('111'), 0,0,0,'');
  selStart(newDiv('111'), 0,0,1,'');
  selStart(newDiv('111'), 0,0,3,'');
  selStart(newDiv('111'), 0,1,0,'1');
  selStart(newDiv('111'), 0,2,1,'11');
  selStart(newDiv('111'), 0,1,3,'1');
  selStart(newDiv(''), 0,0,0,'');
  selStart(newDiv('111','222'), 0,1,0,'1');
  selStart(newDiv('111','222'), 0,2,1,'11');
  selStart(newDiv('111','222'), 0,1,3,'1');
  selStart(newDiv('111','222'), 1,1,0,'1112');
  selStart(newDiv('111','222'), 1,2,1,'11122');
  selStart(newDiv('111','222'), 1,1,3,'1112');
  selStart(newDiv('','222'), 1,1,2,'2');
  selStart(newDiv('','222'), 0,0,0,'');
  selStart(newDiv('111',''), 1,0,0,'111');
  selStart(newDiv('111','222','333'), 1,1,0,'1112');
  selStart(newDiv('111','222','333'), 1,2,1,'11122');
  selStart(newDiv('111','222','333'), 1,1,3,'1112');
  selStart(newDiv('111','','333'), 1,0,0,'111');
  selStart(newDiv('111','222',''), 1,1,3,'1112');

  selMiddleStart(newDiv('111','222','333'), 1,1,0,'2');
  selMiddleStart(newDiv('111','222','333'), 1,2,1,'22');
  selMiddleStart(newDiv('111','222','333'), 1,1,3,'2');
  selMiddleStart(newDiv('111','222','333'), 2,1,0,'2223');
  selMiddleStart(newDiv('111','222','333'), 2,2,1,'22233');
  selMiddleStart(newDiv('111','222','333'), 2,1,3,'2223');
  selMiddleStart(newDiv('111','','333'), 2,1,2,'3');
  selMiddleStart(newDiv('111','','333'), 1,0,0,'');

  selMiddleEnd(newDiv('111','222','333'), 0,1,0,'11222');
  selMiddleEnd(newDiv('111','222','333'), 0,2,1,'1222');
  selMiddleEnd(newDiv('111','222','333'), 0,1,3,'11222');
  selMiddleEnd(newDiv('111','222','333'), 1,1,0,'22');
  selMiddleEnd(newDiv('111','222','333'), 1,2,1,'2');
  selMiddleEnd(newDiv('111','222','333'), 1,1,3,'22');
  selMiddleEnd(newDiv('111','','333'), 0,1,2,'11');
  selMiddleEnd(newDiv('111','','333'), 0,1,3,'11');
  selMiddleEnd(newDiv('111','','333'), 1,0,0,'');

  splitBefore(newDiv('111','222','333'), 1,1,0,'2');
  splitBefore(newDiv('111','222','333'), 1,2,1,'22');
  splitBefore(newDiv('111','222','333'), 1,1,3,'2');
  splitBefore(newDiv('111','222','333'), 2,1,0,'2223');
  splitBefore(newDiv('111','222','333'), 2,2,1,'22233');
  splitBefore(newDiv('111','222','333'), 2,1,3,'2223');
  splitBefore(newDiv('','222','333'), 1,1,0,'2');
  splitBefore(newDiv('','','333'), 1,0,0,'');
  splitBefore(newDiv('','222',''), 2,0,0,'222');
  splitBefore(newDiv('111','','333'), 2,1,2,'3');
}

function boom()
{
  runTests("dry run");  // this is to verify the result strings without splitText()
  dry = false;
  flush = false;
  runTests("no flush");
  flush = true;
  runTests("flush");
}

boom();


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