/
Range.test.ts
501 lines (362 loc) · 15.5 KB
/
Range.test.ts
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
import Window from '../../src/window/Window';
import IWindow from '../../src/window/IWindow';
import IDocument from '../../src/nodes/document/IDocument';
import Range from '../../src/range/Range';
import NodeTypeEnum from '../../src/nodes/node/NodeTypeEnum';
import IText from '../../src/nodes/text/IText';
import DOMRect from '../../src/nodes/element/DOMRect';
describe('Range', () => {
let window: IWindow;
let document: IDocument;
let range: Range;
beforeEach(() => {
window = new Window();
document = window.document;
range = document.createRange();
});
describe('get collapsed()', () => {
it('Returns true when start and end container are the same and has the same offset.', () => {
const container = document.createElement('div');
container.innerHTML = `Hello <u>world</u>!`;
range.setStart(container, 0);
range.setEnd(container, 0);
expect(range.collapsed).toBe(true);
});
it('Returns false when start and end container are the same, but does not have the same offset.', () => {
const container = document.createElement('div');
container.innerHTML = `Hello <u>world</u>!`;
range.setStart(container, 0);
range.setEnd(container, 1);
expect(range.collapsed).toBe(false);
});
it('Returns false when start and end container are not the same, but have the same offset.', () => {
const container = document.createElement('div');
container.innerHTML = `Hello <u>world</u>!`;
range.setStart(container, 0);
range.setEnd(container.children[0], 0);
expect(range.collapsed).toBe(false);
});
});
describe('get commonAncestorContainer()', () => {
it('Returns ancestor parent container when end container is set to a child of start container.', () => {
const container = document.createElement('div');
container.innerHTML = `Hello <u>world</u>!`;
range.setStart(container, 0);
range.setEnd(container.children[0], 0);
expect(range.commonAncestorContainer === container).toBe(true);
});
it('Returns ancestor parent container when start and end container are the same.', () => {
const container = document.createElement('div');
range.setStart(container, 0);
range.setEnd(container, 0);
expect(range.commonAncestorContainer === container).toBe(true);
});
it('Returns end container when start and end container does not have a common ancestor.', () => {
const container = document.createElement('div');
const container2 = document.createElement('div');
range.setStart(container, 0);
range.setEnd(container2, 0);
expect(range.commonAncestorContainer === container2).toBe(true);
});
it('Returns common parent container.', () => {
const container = document.createElement('div');
const span = document.createElement('span');
const span2 = document.createElement('span');
container.appendChild(span);
container.appendChild(span2);
range.setStart(span, 0);
range.setEnd(span2, 0);
expect(range.commonAncestorContainer === container).toBe(true);
});
it('Returns body when it is the common parent container.', () => {
const span = document.createElement('span');
const span2 = document.createElement('span');
document.body.appendChild(span);
document.body.appendChild(span2);
range.setStart(span, 0);
range.setEnd(span2, 0);
expect(range.commonAncestorContainer === document.body).toBe(true);
});
});
describe('collapse()', () => {
it('Collapses the Range to the end container by default.', () => {
const container = document.createElement('div');
container.innerHTML = `Hello <u>world</u>!`;
range.setStart(container, 2);
range.setEnd(container.children[0], 1);
range.collapse();
expect(range.startContainer === container.children[0]).toBe(true);
expect(range.endContainer === container.children[0]).toBe(true);
expect(range.startOffset).toBe(1);
expect(range.endOffset).toBe(1);
expect(range.collapsed).toBe(true);
});
it('Collapses the Range to the end container, even though the toStart parameter is set to true, when the end container is a child of the start container.', () => {
const container = document.createElement('div');
container.innerHTML = `Hello <u>world</u>!`;
range.setStart(container, 2);
range.setEnd(container.children[0], 1);
range.collapse(true);
expect(range.startContainer === container.children[0]).toBe(true);
expect(range.endContainer === container.children[0]).toBe(true);
expect(range.startOffset).toBe(1);
expect(range.endOffset).toBe(1);
expect(range.collapsed).toBe(true);
});
it('Collapses the Range to the start container if the toStart parameter is set to true.', () => {
const container = document.createElement('div');
const span = document.createElement('span');
const span2 = document.createElement('span');
span.textContent = 'hello';
span2.textContent = 'world';
container.appendChild(span);
container.appendChild(span2);
range.setStart(span.firstChild, 1);
range.setEnd(span2.firstChild, 2);
range.collapse(true);
expect(range.startContainer === span.firstChild).toBe(true);
expect(range.endContainer === span.firstChild).toBe(true);
expect(range.startOffset).toBe(1);
expect(range.endOffset).toBe(1);
expect(range.collapsed).toBe(true);
});
});
describe('compareBoundaryPoints()', () => {
it('Returns -1 when pointB is after pointA and "how" is set to "START_TO_END".', () => {
const sourceRange = document.createRange();
const container = document.createElement('div');
container.innerHTML = `
<div>This is the Range 1 Content</div>
<div>This is the Range 2 Content</div>
`;
range.selectNode(container.children[0]);
sourceRange.selectNode(container.children[1]);
expect(range.compareBoundaryPoints(Range.START_TO_END, sourceRange)).toBe(-1);
});
it('Returns 1 when pointA is after pointB and "how" is set to "START_TO_END".', () => {
const sourceRange = document.createRange();
const container = document.createElement('div');
container.innerHTML = `
<div>This is the Range 1 Content</div>
<div>This is the Range 2 Content</div>
`;
range.selectNode(container.children[1]);
sourceRange.selectNode(container.children[0]);
expect(range.compareBoundaryPoints(Range.START_TO_END, sourceRange)).toBe(1);
});
it('Returns -1 when pointA comes before pointB and the selection are text nodes and "how" is set to "START_TO_END".', () => {
const sourceRange = document.createRange();
const container = document.createElement('div');
container.innerHTML = `
<div>This is the Range 1 Content</div>
<div>This is the Range 2 Content</div>
`;
range.setStart(container.children[0].firstChild, 1);
sourceRange.setEnd(container.children[1].firstChild, 10);
expect(range.compareBoundaryPoints(Range.START_TO_END, sourceRange)).toBe(-1);
});
it('Returns 0 when pointA are the same as pointB and "how" is set to "START_TO_END".', () => {
const sourceRange = document.createRange();
const container = document.createElement('div');
container.innerHTML = `
<div>This is the Range 1 Content</div>
<div>This is the Range 2 Content</div>
`;
range.setStart(container.children[0].firstChild, 1);
range.setEnd(container.children[1].firstChild, 10);
sourceRange.setStart(container.children[0].firstChild, 1);
sourceRange.setEnd(container.children[1].firstChild, 10);
expect(range.compareBoundaryPoints(Range.START_TO_END, sourceRange)).toBe(1);
});
});
describe('comparePoint()', () => {
it('Returns 1 when referenceNode is after range.', () => {
const container = document.createElement('div');
container.innerHTML = `
<div>This is the Range 1 Content</div>
<div>This is the Range 2 Content</div>
`;
range.selectNode(container.children[0]);
expect(range.comparePoint(container.children[1], 0)).toBe(1);
});
it('Returns -1 when referenceNode is before range.', () => {
const container = document.createElement('div');
container.innerHTML = `
<div>This is the Range 1 Content</div>
<div>This is the Range 2 Content</div>
`;
range.selectNode(container.children[1]);
expect(range.comparePoint(container.children[0], 0)).toBe(-1);
});
});
describe('cloneContents()', () => {
it('Clones text in a paragraph.', () => {
const paragraph = document.createElement('p');
paragraph.innerHTML = 'My text';
const referenceNode = paragraph.firstChild;
range.selectNode(referenceNode);
const documentFragment = range.cloneContents();
expect(documentFragment.nodeType).toBe(NodeTypeEnum.documentFragmentNode);
expect(documentFragment.childNodes.length).toBe(1);
expect(documentFragment.firstChild.nodeType).toBe(NodeTypeEnum.textNode);
expect((<IText>documentFragment.firstChild).data).toBe('My text');
document.body.appendChild(documentFragment);
expect(documentFragment.childNodes.length).toBe(0);
});
it('Clones multiple elements inside a paragraph.', () => {
const paragraph = document.createElement('p');
paragraph.innerHTML = 'Example: <i>italic</i> and <b>bold</b>';
range.setStart(paragraph.firstChild, 2);
range.setEnd(paragraph.querySelector('b').firstChild, 3);
const documentFragment = range.cloneContents();
document.body.appendChild(documentFragment);
expect(document.body.innerHTML).toBe('ample: <i>italic</i> and <b>bol</b>');
});
});
describe('cloneRange()', () => {
it('Returns a new range with the same start and end as the original.', () => {
const paragraph = document.createElement('p');
paragraph.innerHTML = 'Example: <i>italic</i> and <b>bold</b>';
range.setStart(paragraph.firstChild, 2);
range.setEnd(paragraph.querySelector('b').firstChild, 3);
const clone = range.cloneRange();
expect(clone.startContainer === range.startContainer).toBe(true);
expect(clone.startOffset).toBe(range.startOffset);
expect(clone.endContainer === range.endContainer).toBe(true);
expect(clone.endOffset).toBe(range.endOffset);
});
});
describe('createContextualFragment()', () => {
it('Returns a document fragment with the tag string parsed as nodes.', () => {
const paragraph = document.createElement('p');
document.body.appendChild(paragraph);
range.selectNode(paragraph);
const fragment = range.createContextualFragment('<div>Test</div>');
document.body.appendChild(fragment);
expect(document.body.innerHTML).toBe('<p></p><div>Test</div>');
});
});
describe('deleteContents()', () => {
it('Deletes the selected content.', () => {
const paragraph = document.createElement('p');
paragraph.innerHTML = 'Example: <i>italic</i> and <b>bold</b>';
range.setStart(paragraph.firstChild, 2);
range.setEnd(paragraph.querySelector('b').firstChild, 3);
range.deleteContents();
expect(paragraph.innerHTML).toBe('Ex<b>d</b>');
});
});
describe('detach()', () => {
it('Do nothing.', () => {
expect(range.detach()).toBe(undefined);
});
});
describe('extractContents()', () => {
it('Extracts the selected content.', () => {
const paragraph = document.createElement('p');
paragraph.innerHTML = 'Example: <i>italic</i> and <b>bold</b>';
document.body.appendChild(paragraph);
range.setStart(paragraph.firstChild, 2);
range.setEnd(paragraph.querySelector('b').firstChild, 3);
const documentFragment = range.extractContents();
document.body.appendChild(documentFragment);
expect(document.body.innerHTML).toBe('<p>Ex<b>d</b></p>ample: <i>italic</i> and <b>bol</b>');
});
});
describe('getBoundingClientRect()', () => {
it('Returns an instance of DOMRect.', () => {
const domRect = range.getBoundingClientRect();
expect(domRect instanceof DOMRect).toBe(true);
});
});
describe('getClientRects()', () => {
it('Returns an empty IDOMRectList.', () => {
const clientRects = range.getClientRects();
expect(Array.isArray(clientRects)).toBe(true);
expect(typeof clientRects.item).toBe('function');
});
});
describe('isPointInRange()', () => {
it('Returns "true" if point is in Range.', () => {
document.body.innerHTML = '<p>Example: <i>italic</i> and <b>bold</b></p><span>Test</span>';
const paragraph = document.body.children[0];
range.setStart(paragraph.firstChild, 2);
range.setEnd(paragraph.querySelector('b').firstChild, 3);
expect(range.isPointInRange(paragraph.querySelector('i').firstChild, 2)).toBe(true);
expect(range.isPointInRange(document.body.children[1], 1)).toBe(false);
expect(range.isPointInRange(document.body.children[1].firstChild, 2)).toBe(false);
});
});
describe('insertNode()', () => {
it('Inserts a node into the Range.', () => {
document.body.innerHTML = '<p>Example: <i>italic</i> and <b>bold</b></p><span>Test</span>';
const paragraph = document.body.children[0];
range.setStart(paragraph.firstChild, 2);
range.setEnd(paragraph.querySelector('b').firstChild, 3);
const newNode = document.createElement('u');
newNode.innerHTML = 'New node';
range.insertNode(newNode);
expect(document.body.innerHTML).toBe(
'<p>Ex<u>New node</u>ample: <i>italic</i> and <b>bold</b></p><span>Test</span>'
);
});
});
describe('intersectsNode()', () => {
it('Returns "true" if a node intersects the Range.', () => {
document.body.innerHTML = '<p>Example: <i>italic</i> and <b>bold</b></p><span>Test</span>';
const paragraph = document.body.children[0];
range.setStart(paragraph.firstChild, 2);
range.setEnd(paragraph.querySelector('b').firstChild, 3);
expect(range.intersectsNode(paragraph.querySelector('i'))).toBe(true);
expect(range.intersectsNode(paragraph.querySelector('i').firstChild)).toBe(true);
expect(range.intersectsNode(document.body.children[1])).toBe(false);
expect(range.intersectsNode(document.body.children[1].firstChild)).toBe(false);
});
});
describe('selectNode()', () => {
it('Sets the start and end Node to the parent of the provided Node and offset to its corresponding index.', () => {
const paragraph = document.createElement('p');
paragraph.innerHTML = 'Example: <i>italic</i> and <b>bold</b>';
const italic = paragraph.querySelector('i');
range.selectNode(italic);
expect(range.startContainer === paragraph).toBe(true);
expect(range.endContainer === paragraph).toBe(true);
expect(range.startOffset).toBe(1);
expect(range.endOffset).toBe(2);
});
});
describe('selectNodeContents()', () => {
it('Selects the content of an element.', () => {
const paragraph = document.createElement('p');
paragraph.innerHTML = 'Example: <i>italic</i> and <b>bold</b>';
const italic = paragraph.querySelector('i');
range.selectNodeContents(italic);
expect(range.startContainer === italic).toBe(true);
expect(range.endContainer === italic).toBe(true);
expect(range.startOffset).toBe(0);
expect(range.endOffset).toBe(1);
});
it('Selects the content of a text node.', () => {
const paragraph = document.createElement('p');
paragraph.innerHTML = 'Example: <i>italic</i> and <b>bold</b>';
const text = paragraph.querySelector('i').firstChild;
range.selectNodeContents(text);
expect(range.startContainer === text).toBe(true);
expect(range.endContainer === text).toBe(true);
expect(range.startOffset).toBe(0);
expect(range.endOffset).toBe(6);
});
});
describe('setStart()', () => {
it('Selects the content of an element.', () => {
const paragraph = document.createElement('p');
paragraph.innerHTML = 'Example: <i>italic</i> and <b>bold</b>';
const italic = paragraph.querySelector('i');
range.selectNodeContents(italic);
expect(range.startContainer === italic).toBe(true);
expect(range.endContainer === italic).toBe(true);
expect(range.startOffset).toBe(0);
expect(range.endOffset).toBe(1);
});
});
});