-
Notifications
You must be signed in to change notification settings - Fork 6
/
dom_unittest.js
245 lines (224 loc) · 9.04 KB
/
dom_unittest.js
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
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
'use strict';
function dumpNodes(nodes) {
var sink = ''
var delimiter = '';
for (var node of nodes) {
sink += delimiter;
if (node === null)
sink += "(null)";
else if (editing.dom.isText(node))
sink += node.nodeValue;
else
sink += node.nodeName;
delimiter = ',';
}
return sink;
}
//
// HTMLIterable
//
testCaseWithSample('dom.HTMLIterable.1',
'|<select><option>1</option><option>2</option><option>3</option></select>',
function(sample) {
var selectElement = sample.document.querySelector('select');
var results = [];
for (var option of new editing.dom.HTMLIterable(selectElement.options))
results.push(option.value);
expectEq('1,2,3', function() { return results.join(','); });
});
//
// ancestorsOrSelf
//
testCaseWithSample('dom.ancestorsOrSelf.1',
'<div><foo>0<bar>1<baz>|2</baz>3</bar>4</foo>',
function(sample, selection) {
var results = [];
var sample = selection.anchorNode;
for (var node of editing.dom.ancestorsOrSelf(sample))
results.push(node.nodeName);
expectEq('#text,BAZ,BAR,FOO,DIV,BODY,HTML,#document',
function() { return results.join(','); });
});
//
// ancestorsWhile
//
testCaseWithSample('dom.ancestorsWhile.Nodes',
'<div><a>foo<b>bar<i>baz<u>q$u|x</u></i></b></a></div>',
function(sample, selection) {
var node = selection.anchorNode;
expectEq('U,I,B,A', function() {
return dumpNodes(
editing.dom.ancestorsWhile(node, function(node) {
return node.nodeName !== 'DIV';
}));
});
expectEq('U,I,B', function() {
return dumpNodes(
editing.dom.ancestorsWhile(node, function(node) {
return node.nodeName !== 'A';
}));
});
expectEq('U,I', function() {
return dumpNodes(
editing.dom.ancestorsWhile(node, function(node) {
return node.nodeName !== 'B';
}));
});
expectEq('U', function() {
return dumpNodes(
editing.dom.ancestorsWhile(node, function(node) {
return node.nodeName !== 'I';
}));
});
expectEq('', function() {
return dumpNodes(
editing.dom.ancestorsWhile(node, function(node) {
return node.nodeName !== 'U';
}));
});
}
);
//
// canContainRangeEndPoint
//
testCaseWithSample('dom.canContainRangeEndPoint.NodesText',
'<p contenteditable>^abcd|</p>',
function(sample) {
function canContainRangeEndPoint(tagName) {
var element = sample.createElement(tagName);
return editing.dom.canContainRangeEndPoint(element);
}
expectTrue(function() { return canContainRangeEndPoint('a'); });
expectTrue(function() { return canContainRangeEndPoint('b'); });
expectTrue(function() { return canContainRangeEndPoint('div'); });
expectFalse(function() { return canContainRangeEndPoint('button'); });
expectFalse(function() { return canContainRangeEndPoint('input'); });
expectFalse(function() { return canContainRangeEndPoint('progress'); });
});
//
// computeSelectedNodes
//
testCaseWithSample('dom.computeSelectedNodes.NodesText',
'<p contenteditable>^abcd|</p>',
function(sample, selection) {
var editor = editing.Editor.getOrCreate(sample.document);
var context = editor.createContext('noname', selection);
var normalizedSelection = context.normalizeSelection(selection);
var nodes = editing.dom.computeSelectedNodes(normalizedSelection);
expectEq('abcd', function() { return dumpNodes(nodes); });
});
testCaseWithSample('dom.computeSelectedNodes.NodesTextPartial',
'<p contenteditable>ab^c|d</p>',
function(sample, selection) {
var editor = editing.Editor.getOrCreate(sample.document);
var context = editor.createContext('noname', selection);
var normalizedSelection = context.normalizeSelection(selection);
var nodes = editing.dom.computeSelectedNodes(normalizedSelection);
expectEq('c', function() { return dumpNodes(nodes); });
});
testCaseWithSample('dom.computeSelectedNodes.NodesTree',
'<p contenteditable><e1><e2>e2Before<e3>^e3</e3>e2After</e2>e1After|</e1></p>',
function(sample, selection) {
var editor = editing.Editor.getOrCreate(sample.document);
var context = editor.createContext('noname', selection);
var normalizedSelection = context.normalizeSelection(selection);
var nodes = editing.dom.computeSelectedNodes(normalizedSelection);
expectEq('e3,e2After,e1After', function() { return dumpNodes(nodes); });
});
testCaseWithSample('dom.computeSelectedNodes.NodesTree2',
'<p contenteditable>^abcd<b>efg</b>|</p>',
function(sample, selection) {
var editor = editing.Editor.getOrCreate(sample.document);
var context = editor.createContext('noname', selection);
var normalizedSelection = context.normalizeSelection(selection);
var nodes = editing.dom.computeSelectedNodes(normalizedSelection);
expectEq('abcd,B,efg', function() { return dumpNodes(nodes); });
});
testCaseWithSample('dom.computeSelectedNodes.NodesTree3',
'<p contenteditable>ab^cd<b>efg</b>|</p>',
function(sample, selection) {
var editor = editing.Editor.getOrCreate(sample.document);
var context = editor.createContext('noname', selection);
var normalizedSelection = context.normalizeSelection(selection);
var nodes = editing.dom.computeSelectedNodes(normalizedSelection);
expectEq('cd,B,efg', function() { return dumpNodes(nodes); });
});
testCaseWithSample('dom.computeSelectedNodes.NodesTree4',
'<p contenteditable><e1><e2>e2Before<e3>^e3</e3>e2After</e2><e4>e4|</e4></e1></p>',
function(sample, selection) {
var editor = editing.Editor.getOrCreate(sample.document);
var context = editor.createContext('noname', selection);
var normalizedSelection = context.normalizeSelection(selection);
var nodes = editing.dom.computeSelectedNodes(normalizedSelection);
expectEq('e3,e2After,E4,e4', function() { return dumpNodes(nodes); });
});
testCaseWithSample('dom.computeSelectedNodes.Nodes.Tree.Empty',
'<div contenteditable><span>foo^</span><span>|bar</span></div>',
function(sample, selection) {
var editor = editing.Editor.getOrCreate(sample.document);
var context = editor.createContext('noname', selection);
var normalizedSelection = context.normalizeSelection(selection);
var nodes = editing.dom.computeSelectedNodes(normalizedSelection);
expectEq('', function() { return dumpNodes(nodes); });
});
testCaseWithSample('dom.computeSelectedNodes.NodesTreeUL',
'<div contenteditable>^<ul><li>one</li><li>two</li></ul>|</div>',
function(sample, selection) {
var editor = editing.Editor.getOrCreate(sample.document);
var context = editor.createContext('noname', selection);
var normalizedSelection = context.normalizeSelection(selection);
var nodes = editing.dom.computeSelectedNodes(normalizedSelection);
expectEq('UL,LI,one,LI,two', function() { return dumpNodes(nodes); });
});
//
// isEditable
//
// Note: Due to http://crbug.com/313082, to get true from |isContentEditable|,
// element should be associated to renderer.
testCaseWithSample('dom.isEditable',
'|<a>foo</a>', function(sample, selection) {
var elementA = sample.document.querySelector('a');
expectFalse(function () { return editing.dom.isEditable(elementA); });
var elementB = sample.createElement('b');
elementA.appendChild(elementB);
elementA.setAttribute('contentEditable', 'true');
expectTrue(function () { return elementA.isContentEditable; });
expectFalse(function () { return editing.dom.isEditable(elementA); });
expectTrue(function () { return editing.dom.isEditable(elementB); });
});
//
// isInteractive
//
testCaseWithSample('dom.isInteractive', '', function(sample, selection) {
var elementA = sample.createElement('a');
var elementB = sample.createElement('b');
expectTrue(function () { return editing.dom.isInteractive(elementA); });
expectFalse(function () { return editing.dom.isInteractive(elementB); });
});
//
// isPhrasing
//
testCaseWithSample('dom.isPhrasing', '', function(sample, selection) {
var elementA = sample.createElement('a');
var elementB = sample.createElement('b');
var elementDiv = sample.createElement('div');
var elementH1 = sample.createElement('h1');
expectTrue(function () { return editing.dom.isPhrasing(elementA); });
expectTrue(function () { return editing.dom.isPhrasing(elementB); });
expectFalse(function () { return editing.dom.isPhrasing(elementDiv); });
expectFalse(function () { return editing.dom.isPhrasing(elementH1); });
});
//
// isWhitespaceNode
//
testCaseWithSample('dom.isWhitespaceNode', '', function(sample, selection) {
var elementA = sample.createElement('a');
var textB = sample.createTextNode('b');
var textC = sample.createTextNode(' ');
expectFalse(function () { return editing.dom.isWhitespaceNode(elementA); });
expectFalse(function () { return editing.dom.isWhitespaceNode(textB); });
expectTrue(function () { return editing.dom.isWhitespaceNode(textC); });
});