forked from landmime/XKCD-Substitutions-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
336 lines (259 loc) · 13.6 KB
/
test.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
// A one second timeout in case the asynchronous Settings functions have an
// error preventing their callbacks from ever being called.
QUnit.config.testTimeout = 1000;
// Trying to split a null textNode should return null.
QUnit.test('TextNode.split null textNode', function(assert) {
assert.notOk(TextNode.split(null, 1, 1, 'SPAN'));
});
// Trying to split a non text node should return null.
QUnit.test('TextNode.split non text node', function(assert) {
var nodeParent = document.createElement('div');
var nonTextNode = document.createElement('a');
nonTextNode.textContent = 'abc'
nodeParent.appendChild(nonTextNode);
assert.notOk(TextNode.split(nonTextNode, 1, 1, 'SPAN'));
});
// Trying to split an orphaned node should return null, since splitting it
// requires changing the parent's childNodes.
QUnit.test('TextNode.split node without parent', function(assert) {
var textNode = document.createTextNode('abc');
assert.notOk(TextNode.split(textNode, 1, 1, 'SPAN'));
});
// Trying to split a non-positive amount of text doesn't make sense, and should
// return null.
QUnit.test('TextNode.split length <= 0', function(assert) {
var textNodeParent = document.createElement('div');
var textNode = document.createTextNode('abc');
textNodeParent.appendChild(textNode);
assert.notOk(TextNode.split(textNode, 0, 0, 'SPAN'));
assert.notOk(TextNode.split(textNode, 0, -1, 'SPAN'));
});
// Trying to split at a location past then end of the text doesn't make sense,
// and should return null.
QUnit.test('TextNode.split startIndex >= text size', function(assert) {
var textNodeParent = document.createElement('div');
var textNode = document.createTextNode('abc');
textNodeParent.appendChild(textNode);
assert.notOk(TextNode.split(textNode, 3, 1, 'SPAN'));
assert.notOk(TextNode.split(textNode, 4, 1, 'SPAN'));
});
// The common case is splitting a text node somewhere in the middle, resulting
// in the text to the left of it in the first node, the target text in the
// middle, and the remainder in the right node.
QUnit.test('TextNode.split split in the middle', function(assert) {
var textNodeParent = document.createElement('div');
var textNode = document.createTextNode('abc');
textNodeParent.appendChild(textNode);
var result = TextNode.split(textNode, 1, 1, 'SPAN');
assert.ok(3 == result.length);
assert.equal(result[0].nodeType, NodeType.TEXT);
assert.equal(result[0].textContent, 'a');
assert.equal(result[1].nodeType, NodeType.ELEMENT);
assert.equal(result[1].nodeName, 'SPAN');
assert.equal(result[1].textContent, 'b');
assert.equal(result[2].nodeType, NodeType.TEXT);
assert.equal(result[2].textContent, 'c');
});
// For consistency with the common case, splitting a node at the very beginning
// will still result in three nodes, the first of which will be empty.
QUnit.test('TextNode.split split at the beginning', function(assert) {
var textNodeParent = document.createElement('div');
var textNode = document.createTextNode('abc');
textNodeParent.appendChild(textNode);
var result = TextNode.split(textNode, 0, 1, 'SPAN');
assert.ok(3 == result.length);
assert.equal(result[0].nodeType, NodeType.TEXT);
assert.equal(result[0].textContent, '');
assert.equal(result[1].nodeType, NodeType.ELEMENT);
assert.equal(result[1].nodeName, 'SPAN');
assert.equal(result[1].textContent, 'a');
assert.equal(result[2].nodeType, NodeType.TEXT);
assert.equal(result[2].textContent, 'bc');
});
// For consistency with the common case, splitting a node at the very beginning
// will still result in three nodes, the last of which will be empty.
QUnit.test('TextNode.split split at the end', function(assert) {
var textNodeParent = document.createElement('div');
var textNode = document.createTextNode('abc');
textNodeParent.appendChild(textNode);
var result = TextNode.split(textNode, 2, 1, 'SPAN');
assert.ok(3 == result.length);
assert.equal(result[0].nodeType, NodeType.TEXT);
assert.equal(result[0].textContent, 'ab');
assert.equal(result[1].nodeType, NodeType.ELEMENT);
assert.equal(result[1].nodeName, 'SPAN');
assert.equal(result[1].textContent, 'c');
assert.equal(result[2].nodeType, NodeType.TEXT);
assert.equal(result[2].textContent, '');
});
// For consistency with the common case, splitting a text node will still result
// in three nodes, the first and last being empty.
QUnit.test('TextNode.split split the entire text', function(assert) {
var textNodeParent = document.createElement('div');
var textNode = document.createTextNode('abc');
textNodeParent.appendChild(textNode);
var result = TextNode.split(textNode, 0, 3, 'SPAN');
assert.ok(3 == result.length);
assert.equal(result[0].nodeType, NodeType.TEXT);
assert.equal(result[0].textContent, '');
assert.equal(result[1].nodeType, NodeType.ELEMENT);
assert.equal(result[1].nodeName, 'SPAN');
assert.equal(result[1].textContent, 'abc');
assert.equal(result[2].nodeType, NodeType.TEXT);
assert.equal(result[2].textContent, '');
});
// The common case is replacing a single occurance once, somewhere in the middle
// of a text node.
QUnit.test('Replacer.run common case', function(assert) {
var textNodeParent = document.createElement('div');
var textNode = document.createTextNode('a b c');
textNodeParent.appendChild(textNode);
var replacements = [new Replacement('b', 'xxx')];
var replacer = new Replacer(replacements, SettingsValue.REPLACED_STYLE_NONE, 0);
assert.equal(1, replacer.run(textNodeParent));
assert.equal(textNodeParent.childNodes[0].nodeType, NodeType.TEXT);
assert.equal(textNodeParent.childNodes[0].textContent, 'a ');
assert.equal(textNodeParent.childNodes[1].nodeType, NodeType.ELEMENT);
assert.equal(textNodeParent.childNodes[1].nodeName, 'SPAN');
assert.equal(textNodeParent.childNodes[1].textContent, 'xxx');
assert.equal(textNodeParent.childNodes[2].nodeType, NodeType.TEXT);
assert.equal(textNodeParent.childNodes[2].textContent, ' c');
});
// Replacing multiple occurances of the same text in the same node should work
// by splitting the text node into (n * 2) + 1 new nodes, where n is the number
// of replacements.
QUnit.test('Replacer.run multiple instances of the same replacement',
function(assert) {
var textNodeParent = document.createElement('div');
var textNode = document.createTextNode('a b b c');
textNodeParent.appendChild(textNode);
var replacements = [new Replacement('b', 'xxx')];
var replacer = new Replacer(replacements, SettingsValue.REPLACED_STYLE_NONE, 0);
assert.equal(2, replacer.run(textNodeParent));
assert.equal(textNodeParent.childNodes[0].nodeType, NodeType.TEXT);
assert.equal(textNodeParent.childNodes[0].textContent, 'a ');
assert.equal(textNodeParent.childNodes[1].nodeType, NodeType.ELEMENT);
assert.equal(textNodeParent.childNodes[1].nodeName, 'SPAN');
assert.equal(textNodeParent.childNodes[1].textContent, 'xxx');
assert.equal(textNodeParent.childNodes[2].nodeType, NodeType.TEXT);
assert.equal(textNodeParent.childNodes[2].textContent, ' ');
assert.equal(textNodeParent.childNodes[3].nodeType, NodeType.ELEMENT);
assert.equal(textNodeParent.childNodes[3].nodeName, 'SPAN');
assert.equal(textNodeParent.childNodes[3].textContent, 'xxx');
assert.equal(textNodeParent.childNodes[4].nodeType, NodeType.TEXT);
assert.equal(textNodeParent.childNodes[4].textContent, ' c');
});
// Replacing multiple different replacements in the node should work by
// splitting the text node into (n * 2) + 1 new nodes, where n is the number
// of replacements.
QUnit.test('Replacer.run multiple different replacements', function(assert) {
var textNodeParent = document.createElement('div');
var textNode = document.createTextNode('a b c d');
textNodeParent.appendChild(textNode);
var replacements = [new Replacement('b', 'xxx'), new Replacement('c', 'yyy')];
var replacer = new Replacer(replacements, SettingsValue.REPLACED_STYLE_NONE, 0);
assert.equal(2, replacer.run(textNodeParent));
assert.equal(textNodeParent.childNodes[0].nodeType, NodeType.TEXT);
assert.equal(textNodeParent.childNodes[0].textContent, 'a ');
assert.equal(textNodeParent.childNodes[1].nodeType, NodeType.ELEMENT);
assert.equal(textNodeParent.childNodes[1].nodeName, 'SPAN');
assert.equal(textNodeParent.childNodes[1].textContent, 'xxx');
assert.equal(textNodeParent.childNodes[2].nodeType, NodeType.TEXT);
assert.equal(textNodeParent.childNodes[2].textContent, ' ');
assert.equal(textNodeParent.childNodes[3].nodeType, NodeType.ELEMENT);
assert.equal(textNodeParent.childNodes[3].nodeName, 'SPAN');
assert.equal(textNodeParent.childNodes[3].textContent, 'yyy');
assert.equal(textNodeParent.childNodes[4].nodeType, NodeType.TEXT);
assert.equal(textNodeParent.childNodes[4].textContent, ' d');
});
// Replacing with a zero length pattern should do nothing (and definitely not go
// into an infinite loop).
QUnit.test('Replacer.run zero length match', function(assert) {
var textNodeParent = document.createElement('div');
var text = 'a b c d';
var textNode = document.createTextNode(text);
textNodeParent.appendChild(textNode);
var replacements = [new Replacement('', 'xxx')];
var replacer = new Replacer(replacements, SettingsValue.REPLACED_STYLE_NONE, 0);
assert.equal(0, replacer.run(textNodeParent));
assert.equal(textNode.nodeType, NodeType.TEXT);
assert.equal(textNode.textContent, text);
});
// Replacing with a pattern with an empty value should work as expected, by
// creating an empty node in place of the matched text.
QUnit.test('Replacer.run zero length value', function(assert) {
var textNodeParent = document.createElement('div');
var textNode = document.createTextNode('a b c');
textNodeParent.appendChild(textNode);
var replacements = [new Replacement('b', '')];
var replacer = new Replacer(replacements, SettingsValue.REPLACED_STYLE_NONE, 0);
assert.equal(1, replacer.run(textNodeParent));
assert.equal(textNodeParent.childNodes[0].nodeType, NodeType.TEXT);
assert.equal(textNodeParent.childNodes[0].textContent, 'a ');
assert.equal(textNodeParent.childNodes[1].nodeType, NodeType.ELEMENT);
assert.equal(textNodeParent.childNodes[1].nodeName, 'SPAN');
assert.equal(textNodeParent.childNodes[1].textContent, '');
assert.equal(textNodeParent.childNodes[2].nodeType, NodeType.TEXT);
assert.equal(textNodeParent.childNodes[2].textContent, ' c');
});
// The Replacer has a limit on the number of replacements its object will make, mostly
// to avoid hanging a page if a bug is encountered.
QUnit.test('Replacer.run replacement limit', function(assert) {
var textNodeParent = document.createElement('div');
var textNode = document.createTextNode('a b b b');
textNodeParent.appendChild(textNode);
var replacements = [new Replacement('b', 'x')];
var replacer = new Replacer(replacements, SettingsValue.REPLACED_STYLE_NONE, 2);
assert.equal(2, replacer.run(textNodeParent));
assert.equal(textNodeParent.childNodes[0].nodeType, NodeType.TEXT);
assert.equal(textNodeParent.childNodes[0].textContent, 'a ');
assert.equal(textNodeParent.childNodes[1].nodeType, NodeType.ELEMENT);
assert.equal(textNodeParent.childNodes[1].nodeName, 'SPAN');
assert.equal(textNodeParent.childNodes[1].textContent, 'x');
assert.equal(textNodeParent.childNodes[2].nodeType, NodeType.TEXT);
assert.equal(textNodeParent.childNodes[2].textContent, ' ');
assert.equal(textNodeParent.childNodes[3].nodeType, NodeType.ELEMENT);
assert.equal(textNodeParent.childNodes[3].nodeName, 'SPAN');
assert.equal(textNodeParent.childNodes[3].textContent, 'x');
assert.equal(textNodeParent.childNodes[4].nodeType, NodeType.TEXT);
assert.equal(textNodeParent.childNodes[4].textContent, ' b');
});
// A Replacer should only replace text in a node once. So if a replacement's 'to'
// text matches another (or its own) 'from' pattern, then only the first replacement
// will be evaluated.
QUnit.test('Replacer.run only process nodes once', function(assert) {
var textNodeParent = document.createElement('div');
var textNode = document.createTextNode('a');
textNodeParent.appendChild(textNode);
var replacements = [new Replacement('a', 'b'),
new Replacement('b', 'a')];
var replacer = new Replacer(replacements, SettingsValue.REPLACED_STYLE_NONE, 100);
assert.equal(1, replacer.run(textNodeParent));
assert.equal(textNodeParent.childNodes[0].nodeType, NodeType.TEXT);
assert.equal(textNodeParent.childNodes[0].textContent, '');
assert.equal(textNodeParent.childNodes[1].nodeType, NodeType.ELEMENT);
assert.equal(textNodeParent.childNodes[1].nodeName, 'SPAN');
assert.equal(textNodeParent.childNodes[1].textContent, 'b');
assert.equal(textNodeParent.childNodes[2].nodeType, NodeType.TEXT);
assert.equal(textNodeParent.childNodes[2].textContent, '');
assert.equal(1, replacer.run(textNodeParent.childNodes[1]));
});
// Sanity check on the loadFromStorage function.
QUnit.test('Settings.loadFromStorage santiy check', function(assert) {
assert.expect(1);
var done = assert.async();
Settings.loadFromStorage(function(settings) {
assert.ok(settings instanceof Settings);
done();
});
});
// Sanity check on the saveToStorage method.
QUnit.test('Settings.saveToStorage sanity check', function(assert) {
assert.expect(1);
var done = assert.async();
var settings = new Settings();
settings.saveToStorage(function() {
assert.ok(true);
done();
});
});