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
|
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=191864
-->
<head>
<title>Test for Bug 191864</title>
<script type="application/javascript" src="/MochiKit/packed.js"></script>
<script type="application/javascript" 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=191864">Mozilla Bug 191864</a>
<p id="display">
<span id="col1" style="float:left; height:800px; width:180px;"></span>
<span id="col2" style="float:left; height:800px; width:180px;"></span>
</p>
<div id="content" style="display: none">
</div>
<pre id="test">
<script type="application/javascript;version=1.7">
var tests = [
[ {}, [0,4], "012345678" ],
[ {}, [0,0], "012345678" ],
[ {}, [0,9], "012345678" ],
[ {startOffset:4}, [0,4], "012345678" ],
[ {startOffset:5}, [0,4], "012345678" ],
[ {startOffset:5,endOffset:6}, [0,4], "012345678" ],
[ {endOffset:5}, [0,4], "012345678" ],
[ {endOffset:4}, [0,4], "012345678" ],
[ {endOffset:3}, [0,4], "012345678" ],
[ {startOffset:1,endOffset:3}, [0,4], "012345678" ],
[ {startOffset:7,endOffset:7}, [0,4], "012345678" ],
[ {startOffset:4,endOffset:4}, [0,4], "012345678" ],
[ {endNode:1}, [0,4], "012345678", "" ],
[ {endNode:1}, [0,4], "01234567", "8" ],
[ {endNode:1}, [1,4], "0", "12345678" ],
[ {endNode:2}, [1,4], "0", "12345", "678" ],
]
function runtest(r,p,t) {
// create content
document.original_nodes = [];
for (let i = 2; i < t.length; ++i) {
c = document.createTextNode(t[i]);
p.appendChild(c);
document.original_nodes.push(c);
}
// setup the range
let sel = t[0]
let startNode = p.firstChild;
let startOffset = sel.startOffset === undefined ? 0 : sel.startOffset;
let endNode = sel.endNode === undefined ? startNode : p.childNodes[sel.endNode];
let endOffset = sel.endOffset === undefined ? endNode.length : sel.endOffset;
r.setStart(startNode, startOffset);
r.setEnd(endNode, endOffset);
// splitText
let split = t[1]
p.childNodes[split[0]].splitText(split[1])
}
function test_split(r,p,t) {
runtest(r,p,t);
}
function test_split_merge(r,p,t) {
runtest(r,p,t);
p.normalize();
}
</script>
<script type="application/javascript;version=1.7">
/** Test for Bug 191864 **/
var results = [
/* test_split */
[ {}, [ [0, "0123"], "45678" ]],
[ {}, [ [0, ""], "012345678" ]],
[ {endNode:0, endOffset:9}, [ [0, "012345678"], "" ]],
[ {startOffset:4}, [ [0, "0123"], "45678" ]],
[ {startNode:1, startOffset:1}, [ [0, "0123"], "45678" ]],
[ {startNode:1, startOffset:1, endOffset:2}, [ [0, "0123"], "45678" ]],
[ {endOffset:1}, [ [0, "0123"], "45678" ]],
[ {endNode:0}, [ [0, "0123"], "45678" ]],
[ {endNode:0, endOffset:3}, [ [0, "0123"], "45678" ]],
[ {startOffset:1, endNode:0, endOffset:3}, [ [0, "0123"], "45678" ]],
[ {startNode:1, startOffset:3, endOffset:3}, [ [0, "0123"], "45678" ]],
[ {startOffset:4, endNode:0}, [ [0, "0123"], "45678" ]],
[ {endNode:2, endOffset:0}, [ [0, "0123"], "45678", [1, ""] ]],
[ {endNode:2}, [ [0, "0123"], "4567", [1, "8"] ]],
[ {endNode:2}, [ [0, "0"], [1, "1234"], "5678" ]],
[ {endNode:3}, [ [0, "0"], [1, "1234"], "5", [2, "678"] ]],
/* test_split_merge */
[ {}, [ [0, "012345678" ] ]],
[ {startParent:true}, [ "012345678" ]], /* splitText() creates an empty first child which is removed by normalize() */
[ {}, [ [0, "012345678" ] ]],
[ {startOffset:4}, [ [0, "012345678" ] ]],
[ {startOffset:5}, [ [0, "012345678" ] ]],
[ {startOffset:5,endOffset:6}, [ [0, "012345678" ] ]],
[ {endOffset:5}, [ [0, "012345678" ] ]],
[ {endOffset:4}, [ [0, "012345678" ] ]],
[ {endOffset:3}, [ [0, "012345678" ] ]],
[ {startOffset:1,endOffset:3}, [ [0, "012345678" ] ]],
[ {startOffset:7,endOffset:7}, [ [0, "012345678" ] ]],
[ {startOffset:4,endOffset:4}, [ [0, "012345678" ] ]],
[ {endParent:true}, [ [0, "012345678" ] ]],
[ {}, [ [0, "012345678" ] ]],
[ {}, [ [0, "012345678" ] ]],
[ {}, [ [0, "012345678" ] ]],
]
function verifyResults(r,p,i) {
let nodes = results[i][1];
is(p.childNodes.length, nodes.length, "same number of DOM nodes" + " (test " + i + ")");
for (let j = 0; j < nodes.length; ++j) {
let a = nodes[j];
let b = p.childNodes[j];
if (a instanceof Array) {
is(b, document.original_nodes[a[0]], "same node" + " (test " + i + " child " + j + ")");
is(b.textContent, a[1], "contents2" + " (test " + i + " child " + j + ")");
} else {
is(b.nodeType, Node.TEXT_NODE, "text node" + " (test " + i + " child " + j + ")");
is(b.textContent, a, "contents1" + " (test " + i + " child " + j + ")");
}
}
let sel = results[i][0];
if (sel.todo) {
alert(r.startContainer + '\n' + r.startOffset + '\n' + r.endContainer + '\n' + r.endOffset + '\n')
return;
}
let startNode = sel.startNode === undefined ? p.firstChild : p.childNodes[sel.startNode];
let startOffset = sel.startOffset === undefined ? 0 : sel.startOffset;
if (sel.startParent) { startNode = p; startOffset = 0; }
let endNode = sel.endNode === undefined ? p.childNodes[p.childNodes.length>1 ? 1 : 0] : p.childNodes[sel.endNode];
let endOffset = sel.endOffset === undefined ? endNode.length : sel.endOffset;
if (sel.endParent) { endNode = p; endOffset = 1; }
is(r.startContainer, startNode, "range start node" + " (test " + i + ")");
is(r.startOffset, startOffset, "range start offset" + " (test " + i + ")");
is(r.endContainer, endNode, "range end node" + " (test " + i + ")");
is(r.endOffset, endOffset, "range end offset" + " (test " + i + ")");
}
function runTest() {
let col1 = document.getElementById('col1');
let col2 = document.getElementById('col2');
for (let i=0; i < tests.length; ++i) {
let t = tests[i];
let p = document.createElement('p')
col1.appendChild(p);
let r = document.createRange();
test_split(r,p,t);
verifyResults(r,p,i);
}
for (let i=0; i < tests.length; ++i) {
let t = tests[i];
let p = document.createElement('p')
col2.appendChild(p);
let r = document.createRange();
test_split_merge(r,p,t);
verifyResults(r,p,i+tests.length);
}
SimpleTest.finish();
}
window.onload = function() { setTimeout(runTest, 0); };
SimpleTest.waitForExplicitFinish();
</script>
</pre>
</body>
</html>
|