-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
131 lines (108 loc) · 3.75 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
import test from 'tape';
import {save, restore} from './lib';
test('save() no current selection', function (t) {
const selection = window.getSelection();
selection.removeAllRanges();
const elm = document.body.appendChild(document.createElement('div'));
elm.contentEditable = 'true';
elm.setAttribute('contenteditable', 'true');
elm.innerHTML = '<p>Beep</p>';
save(elm);
t.equal(elm.innerHTML, '<p>Beep</p>', 'unchanged html');
t.end();
});
test('save() selection', function (t) {
const elm = document.body.appendChild(document.createElement('div'));
const elm2 = document.body.appendChild(document.createElement('div'));
elm.innerHTML = '<p>Beep</p>';
const text = elm.querySelector('p').childNodes[0];
const selection = window.getSelection();
selection.removeAllRanges();
const range = document.createRange();
range.setStart(text, 1);
range.setEnd(text, 3);
selection.addRange(range);
save(elm2);
t.equal(elm.innerHTML, '<p>Beep</p>',
'root doument must include selection');
save(elm);
t.equal(
elm.innerHTML,
'<p>B' +
'<mark class="selection-start"></mark>' +
'ee' +
'<mark class="selection-end"></mark>p</p>',
'added selection markers'
);
t.end();
});
test('save() selection with no content selected', function (t) {
const elm = document.body.appendChild(document.createElement('div'));
const elm2 = document.body.appendChild(document.createElement('div'));
elm.innerHTML = '<p>Beep</p>';
const text = elm.querySelector('p').childNodes[0];
const selection = window.getSelection();
selection.removeAllRanges();
const range = document.createRange();
range.setStart(text, 1);
range.setEnd(text, 1);
selection.addRange(range);
save(elm2);
t.equal(elm.innerHTML, '<p>Beep</p>',
'root doument must include selection');
save(elm);
t.equal(
elm.innerHTML,
'<p>B' +
'<mark class="selection-start"></mark>' +
'<mark class="selection-end"></mark>' +
'eep</p>',
'added selection markers'
);
t.end();
});
test('restore() no markers', function (t) {
const elm = document.body.appendChild(document.createElement('div'));
elm.innerHTML = '<p>Beep</p>';
window.getSelection().removeAllRanges();
restore(elm);
t.equal(window.getSelection().rangeCount, 0, 'No ranges');
t.end();
});
test('restore() markers', function (t) {
const elm = document.body.appendChild(document.createElement('div'));
elm.innerHTML = '<p>B' +
'<mark class="selection-start"></mark>' +
'ee' +
'<mark class="selection-end"></mark>p</p>';
window.getSelection().removeAllRanges();
restore(elm);
t.equal(window.getSelection().rangeCount, 1, 'One ranges');
elm.normalize();
const text = elm.querySelector('p').childNodes[0];
const range = window.getSelection().getRangeAt(0);
t.equal(range.startContainer, text, 'range.startContainer');
t.equal(range.startOffset, 1, 'range.startOffset');
t.equal(range.endContainer, text, 'range.endContainer');
t.equal(range.endOffset, 3, 'range.endOffset');
t.end();
});
test('save() new selection with previous markers', function (t) {
const elm = document.body.appendChild(document.createElement('div'));
const markStart = '<mark class="selection-start"></mark>';
const markEnd = '<mark class="selection-end"></mark>';
const initialHtml = `<p>Bee${markStart}p${markEnd}</p>`;
elm.innerHTML = initialHtml;
const text = elm.querySelector('p').childNodes[0];
const selection = window.getSelection();
selection.removeAllRanges();
const range = document.createRange();
range.setStart(text, 1);
range.setEnd(text, 2);
selection.addRange(range);
save(elm);
const actual = elm.innerHTML;
const expected = `<p>B${markStart}e${markEnd}ep</p>`;
t.equal(actual, expected, 'added selection markers');
t.end();
});