/
squire.spec.js
430 lines (373 loc) · 18.9 KB
/
squire.spec.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
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
/*global expect, describe, afterEach, beforeEach, it */
expect = expect.clone()
.addType({
name: 'SquireRTE',
base: 'object',
identify: function (value) {
return value instanceof Squire;
},
inspect: function (value) {
return 'Squire RTE: ' + value.getHTML();
}
})
.addAssertion('[not] to contain HTML', function (expect, editor, expectedValue) {
this.errorMode = 'bubble';
var actualHTML = editor.getHTML().replace(/<br>/g, '');
// BR tags are inconsistent across browsers. Removing them allows cross-browser testing.
expect(actualHTML, '[not] to be', expectedValue);
});
describe('Squire RTE', function () {
var doc, editor;
beforeEach(function () {
var iframe = document.getElementById('testFrame');
doc = iframe.contentDocument;
editor = new Squire(doc);
});
function selectAll(editor) {
doc.getSelection().removeAllRanges()
var range = doc.createRange();
range.setStart(doc.body.childNodes.item(0), 0);
range.setEnd(doc.body.childNodes.item(0), doc.body.childNodes.item(0).childNodes.length);
editor.setSelection(range);
}
describe('hasFormat', function () {
var startHTML;
beforeEach( function () {
startHTML = '<div>one <b>two three</b> four <i>five</i></div>';
editor.setHTML(startHTML);
});
it('returns false when range not touching format', function () {
var range = doc.createRange();
range.setStart(doc.body.childNodes.item(0), 0);
range.setEnd(doc.body.childNodes.item(0), 1);
editor.setSelection(range);
expect(editor.hasFormat('b'), 'to be false');
});
it('returns false when range inside other format', function () {
var range = doc.createRange();
range.setStart(doc.querySelector('i').childNodes[0], 1);
range.setEnd(doc.querySelector('i').childNodes[0], 2);
editor.setSelection(range);
expect(editor.hasFormat('b'), 'to be false');
});
it('returns false when range covers anything outside format', function () {
var range = doc.createRange();
range.setStart(doc.querySelector('b').previousSibling, 2);
range.setEnd(doc.querySelector('b').childNodes[0], 8);
editor.setSelection(range);
expect(editor.hasFormat('b'), 'to be false');
});
it('returns true when range inside format', function () {
var range = doc.createRange();
range.setStart(doc.querySelector('b').childNodes[0], 2);
range.setEnd(doc.querySelector('b').childNodes[0], 8);
editor.setSelection(range);
expect(editor.hasFormat('b'), 'to be true');
});
it('returns true when range covers start of format', function () {
var range = doc.createRange();
range.setStartBefore(doc.querySelector('b'));
range.setEnd(doc.querySelector('b').childNodes[0], 8);
editor.setSelection(range);
expect(editor.hasFormat('b'), 'to be true');
});
it('returns true when range covers start of format, even in weird cases', function () {
var range = doc.createRange();
var prev = doc.querySelector('b').previousSibling;
range.setStart(prev, prev.length);
range.setEnd(doc.querySelector('b').childNodes[0], 8);
editor.setSelection(range);
expect(editor.hasFormat('b'), 'to be true');
});
it('returns true when range covers end of format', function () {
var range = doc.createRange();
range.setStart(doc.querySelector('b').childNodes[0], 2);
range.setEndAfter(doc.querySelector('b'));
editor.setSelection(range);
expect(editor.hasFormat('b'), 'to be true');
});
it('returns true when range covers end of format, even in weird cases', function () {
var range = doc.createRange();
range.setStart(doc.querySelector('b').childNodes[0], 2);
var next = doc.querySelector('b').nextSibling;
range.setEnd(next, 0);
editor.setSelection(range);
expect(editor.hasFormat('b'), 'to be true');
});
it('returns true when range covers all of format', function () {
var range = doc.createRange();
range.setStartBefore(doc.querySelector('b'));
range.setEndAfter(doc.querySelector('b'));
editor.setSelection(range);
expect(editor.hasFormat('b'), 'to be true');
});
});
describe('removeAllFormatting', function () {
// Trivial cases
it('removes inline styles', function () {
var startHTML = '<div><i>one</i> <b>two</b> <u>three</u> <sub>four</sub> <sup>five</sup></div>';
editor.setHTML(startHTML);
expect(editor, 'to contain HTML', startHTML);
selectAll(editor);
editor.removeAllFormatting();
expect(editor, 'to contain HTML', '<div>one two three four five</div>');
});
it('removes block styles', function () {
var startHTML = '<div><blockquote>one</blockquote><ul><li>two</li></ul>' +
'<ol><li>three</li></ol><table><tbody><tr><th>four</th><td>five</td></tr></tbody></table></div>';
editor.setHTML(startHTML);
expect(editor, 'to contain HTML', startHTML);
selectAll(editor);
editor.removeAllFormatting();
var expectedHTML = '<div>one</div><div>two</div><div>three</div><div>four</div><div>five</div>';
expect(editor, 'to contain HTML', expectedHTML);
});
// Potential bugs
it('removes styles that begin inside the range', function () {
var startHTML = '<div>one <i>two three four five</i></div>';
editor.setHTML(startHTML);
expect(editor, 'to contain HTML', startHTML);
var range = doc.createRange();
range.setStart(doc.body.childNodes.item(0), 0);
range.setEnd(doc.getElementsByTagName('i').item(0).childNodes.item(0), 4);
editor.removeAllFormatting(range);
expect(editor, 'to contain HTML', '<div>one two <i>three four five</i></div>');
});
it('removes styles that end inside the range', function () {
var startHTML = '<div><i>one two three four</i> five</div>';
editor.setHTML(startHTML);
expect(editor, 'to contain HTML', startHTML);
var range = doc.createRange();
range.setStart(doc.getElementsByTagName('i').item(0).childNodes.item(0), 13);
range.setEnd(doc.body.childNodes.item(0), doc.body.childNodes.item(0).childNodes.length);
editor.removeAllFormatting(range);
expect(editor, 'to contain HTML', '<div><i>one two three</i> four five</div>');
});
it('removes styles enclosed by the range', function () {
var startHTML = '<div>one <i>two three four</i> five</div>';
editor.setHTML(startHTML);
expect(editor, 'to contain HTML', startHTML);
var range = doc.createRange();
range.setStart(doc.body.childNodes.item(0), 0);
range.setEnd(doc.body.childNodes.item(0), doc.body.childNodes.item(0).childNodes.length);
editor.removeAllFormatting(range);
expect(editor, 'to contain HTML', '<div>one two three four five</div>');
});
it('removes styles enclosing the range', function () {
var startHTML = '<div><i>one two three four five</i></div>';
editor.setHTML(startHTML);
expect(editor, 'to contain HTML', startHTML);
var range = doc.createRange();
range.setStart(doc.getElementsByTagName('i').item(0).childNodes.item(0), 4);
range.setEnd(doc.getElementsByTagName('i').item(0).childNodes.item(0), 18);
editor.removeAllFormatting(range);
expect(editor, 'to contain HTML', '<div><i>one </i>two three four<i> five</i></div>');
});
it('removes nested styles and closes tags correctly', function () {
var startHTML = '<table><tbody><tr><td>one</td></tr><tr><td>two</td><td>three</td></tr><tr><td>four</td><td>five</td></tr></tbody></table>';
editor.setHTML(startHTML);
expect(editor, 'to contain HTML', startHTML);
var range = doc.createRange();
range.setStart(doc.getElementsByTagName('td').item(1), 0);
range.setEnd(doc.getElementsByTagName('td').item(2), doc.getElementsByTagName('td').item(2).childNodes.length);
editor.removeAllFormatting(range);
expect(editor, 'to contain HTML', '<table><tbody><tr><td>one</td></tr></tbody></table>' +
'<div>two</div>' +
'<div>three</div>' +
'<table><tbody><tr><td>four</td><td>five</td></tr></tbody></table>');
});
});
describe('getPath', function () {
var startHTML;
beforeEach( function () {
startHTML = '<div>one <b>two three</b> four <i>five</i></div>';
editor.setHTML(startHTML);
var range = doc.createRange();
range.setStart(doc.body.childNodes.item(0), 0);
range.setEnd(doc.body.childNodes.item(0), 1);
editor.setSelection(range);
});
it('returns the path to the selection', function () {
var range = doc.createRange();
range.setStart(doc.body.childNodes.item(0).childNodes.item(1), 0);
range.setEnd(doc.body.childNodes.item(0).childNodes.item(1), 0);
editor.setSelection(range);
//Manually tell it to update the path
editor._updatePath(range);
expect(editor.getPath(), 'to be', 'DIV>B');
});
it('includes id in the path', function () {
editor.insertHTML('<div id="spanId">Text</div>');
expect(editor.getPath(), 'to be', 'DIV#spanId');
});
it('includes class name in the path', function () {
editor.insertHTML('<div class="myClass">Text</div>');
expect(editor.getPath(), 'to be', 'DIV.myClass');
});
it('includes all class names in the path', function () {
editor.insertHTML('<div class="myClass myClass2 myClass3">Text</div>');
expect(editor.getPath(), 'to be', 'DIV.myClass.myClass2.myClass3');
});
it('includes direction in the path', function () {
editor.insertHTML('<div dir="rtl">Text</div>');
expect(editor.getPath(), 'to be', 'DIV[dir=rtl]');
});
it('includes highlight value in the path', function () {
editor.insertHTML('<div class="highlight" style="background-color: rgb(255, 0, 0)">Text</div>');
expect(editor.getPath(), 'to be', 'DIV.highlight[backgroundColor=rgb(255,0,0)]');
});
it('includes color value in the path', function () {
editor.insertHTML('<div class="colour" style="color: rgb(255, 0, 0)">Text</div>');
expect(editor.getPath(), 'to be', 'DIV.colour[color=rgb(255,0,0)]');
});
it('includes font family value in the path', function () {
editor.insertHTML('<div class="font" style="font-family: Arial, sans-serif">Text</div>');
expect(editor.getPath(), 'to be', 'DIV.font[fontFamily=Arial,sans-serif]');
});
it('includes font size value in the path', function () {
editor.insertHTML('<div class="size" style="font-size: 12pt">Text</div>');
expect(editor.getPath(), 'to be', 'DIV.size[fontSize=12pt]');
});
it('is (selection) when the selection is a range', function() {
var range = doc.createRange();
range.setStart(doc.body.childNodes.item(0).childNodes.item(0), 0);
range.setEnd(doc.body.childNodes.item(0).childNodes.item(3), 0);
editor.setSelection(range);
//Manually tell it to update the path
editor._updatePath(range);
expect(editor.getPath(), 'to be', '(selection)');
});
});
describe('multi-level lists', function () {
it('increases list indentation', function() {
var startHTML = '<ul><li><div>a</div></li><li><div>b</div></li><li><div>c</div></li></ul>';
editor.setHTML(startHTML);
expect(editor, 'to contain HTML', startHTML);
var range = doc.createRange();
var textNode = doc.getElementsByTagName('li').item(1).childNodes[0].childNodes[0]
range.setStart(textNode, 0);
range.setEnd(textNode, 0);
editor.setSelection(range);
editor.increaseListLevel()
expect(editor, 'to contain HTML', '<ul><li><div>a</div></li><ul><li><div>b</div></li></ul><li><div>c</div></li></ul>');
});
it('increases list indentation 2', function() {
var startHTML = '<ul><li><div>a</div></li><li><div>b</div></li><li><div>c</div></li></ul>';
editor.setHTML(startHTML);
expect(editor, 'to contain HTML', startHTML);
var range = doc.createRange();
var textNode = doc.getElementsByTagName('li').item(1).childNodes[0].childNodes[0]
range.setStart(textNode, 0);
range.setEnd(textNode, 0);
editor.setSelection(range);
editor.increaseListLevel()
editor.increaseListLevel()
expect(editor, 'to contain HTML', '<ul><li><div>a</div></li><ul><ul><li><div>b</div></li></ul></ul><li><div>c</div></li></ul>');
});
it('decreases list indentation', function() {
var startHTML = '<ul><li><div>a</div></li><ul><li><div>b</div></li></ul><li><div>c</div></li></ul>';
editor.setHTML(startHTML);
expect(editor, 'to contain HTML', startHTML);
var range = doc.createRange();
var textNode = doc.getElementsByTagName('li').item(1).childNodes[0].childNodes[0]
range.setStart(textNode, 0);
range.setEnd(textNode, 0);
editor.setSelection(range);
editor.decreaseListLevel()
expect(editor, 'to contain HTML', '<ul><li><div>a</div></li><li><div>b</div></li><li><div>c</div></li></ul>');
});
it('decreases list indentation 2', function() {
var startHTML = '<ul><li><div>a</div></li><ul><ul><li><div>b</div></li></ul></ul><li><div>c</div></li></ul>';
editor.setHTML(startHTML);
expect(editor, 'to contain HTML', startHTML);
var range = doc.createRange();
var textNode = doc.getElementsByTagName('li').item(1).childNodes[0].childNodes[0]
range.setStart(textNode, 0);
range.setEnd(textNode, 0);
editor.setSelection(range);
editor.decreaseListLevel()
editor.decreaseListLevel()
expect(editor, 'to contain HTML', '<ul><li><div>a</div></li><li><div>b</div></li><li><div>c</div></li></ul>');
});
it('removes lists', function() {
var startHTML = '<ul><li><div>foo</div></li><ul><li><div>bar</div></li></ul></ul>';
editor.setHTML(startHTML);
expect(editor, 'to contain HTML', startHTML);
var range = doc.createRange();
var textNode = doc.getElementsByTagName('li').item(1).childNodes[0].childNodes[0]
range.setStart(textNode, 0);
range.setEnd(textNode, 0);
editor.setSelection(range);
editor.removeList()
expect(editor, 'to contain HTML', '<ul><li><div>foo</div></li></ul><div>bar</div>');
})
});
describe('insertHTML', function() {
it('fix CF_HTML incomplete table', function() {
editor.insertHTML('<table><tbody><tr><!--StartFragment--><td>text</td><!--EndFragment--></tr></tbody></table>');
expect(editor.getHTML(), 'to contain', '<table><tbody><tr><td>text<br></td></tr></tbody></table>');
editor.setHTML('');
editor.insertHTML('<table><tbody><!--StartFragment--><tr><td>text1</td><td>text2</td></tr><!--EndFragment--></tbody></table>');
expect(editor.getHTML(), 'to contain', '<table><tbody><tr><td>text1<br></td><td>text2<br></td></tr></tbody></table>');
});
var LINK_MAP = {
"dewdw@fre.fr": "mailto:dewdw@fre.fr",
"dew@free.fr?dew=dew": "mailto:dew@free.fr?dew=dew",
"dew@free.fr?subject=dew": "mailto:dew@free.fr?subject=dew",
"test@example.com?subject=foo&body=bar": "mailto:test@example.com?subject=foo&body=bar",
"dew@fre.fr dewdwe @dew": "mailto:dew@fre.fr",
"http://free.fr": "http://free.fr",
"http://google.com": "http://google.com",
"https://google.com": "https://google.com",
"https://www.google.com": "https://www.google.com",
"https://www.google.com/": "https://www.google.com/",
"https://google.com/?": "https://google.com/",
"https://google.com?": "https://google.com",
"https://google.com?a": "https://google.com/?a",
"https://google.com?a=": "https://google.com/?a=",
"https://google.com?a=b": "https://google.com/?a=b",
"https://google.com?a=b?": "https://google.com/?a=b",
"https://google.com?a=b&": "https://google.com/?a=b&",
"https://google.com?a=b&c": "https://google.com/?a=b&c",
"https://google.com?a=b&c=": "https://google.com/?a=b&c=",
"https://google.com?a=b&c=d": "https://google.com/?a=b&c=",
"https://google.com?a=b&c=d?": "https://google.com/?a=b&c=d",
"https://google.com?a=b&c=d&": "https://google.com/?a=b&c=d&",
"https://google.com?a=b&c=d&e=": "https://google.com/?a=b&c=d&e=",
"https://google.com?a=b&c=d&e=f": "https://google.com/?a=b&c=d&e=f"
};
Object.keys(LINK_MAP).forEach((input) => {
it('should auto convert links to anchor: ' + input, function() {
editor.insertHTML(input);
var link = editor.getDocument().querySelector('a');
expect(link.href, 'to contain', LINK_MAP[input]);
editor.setHTML('');
});
});
it('should auto convert a part of the link to an anchor', function() {
editor.insertHTML(`
dew@fre.fr dewdwe @dew
`);
var link = editor.getDocument().querySelector('a');
expect(link.textContent, 'to be', 'dew@fre.fr');
expect(link.href, 'to be', 'mailto:dew@fre.fr');
editor.setHTML('');
});
it('should not auto convert non links to anchor', function() {
editor.insertHTML(`
dewdwe @dew
deww.de
monique.fre
google.com
`);
var link = editor.getDocument().querySelector('a');
expect(link, 'to be', null);
editor.setHTML('');
});
});
afterEach(function () {
editor = null;
var iframe = document.getElementById('testFrame');
iframe.src = 'blank.html';
});
});