-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
626 lines (503 loc) · 18.8 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
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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
var _ = require('lodash'),
lib = require('./index'),
domify = require('domify');
// defaults for chai
chai.config.showDiff = true;
chai.config.truncateThreshold = 0;
describe('text-model', function () {
var sandbox;
/**
* Converts a document or document fragment to a compressed html string (no extra whitespace)
* @param {Element} el
* @returns {string}
*/
function documentToString(el) {
var parent = document.createElement('DIV');
parent.appendChild(el.cloneNode(true));
return parent.innerHTML;
}
beforeEach(function () {
sandbox = sinon.sandbox.create();
});
afterEach(function () {
sandbox.restore();
});
describe('fromElement', function () {
var fn = lib[this.title];
it('doesn\t decode sanitized html', function () {
var el = domify('Hello <iframe>'),
result = {
text: 'Hello <iframe>',
blocks: {}
};
expect(fn(el)).to.deep.equal(result);
});
it('finds text', function () {
var el = domify('Hello <strong>there <em>person</em></strong>!'),
result = {
text: 'Hello there person!',
blocks: {strong: [6, 18], emphasis: [12, 18]}
};
expect(fn(el)).to.deep.equal(result);
});
it('finds full text', function () {
var el = domify('<em>Hello <strong>there person</strong>!</em>'),
result = {
text: 'Hello there person!',
blocks: {strong: [6, 18], emphasis: [0, 19]}
};
expect(fn(el)).to.deep.equal(result);
});
it('finds text even with whitespace', function () {
var el = domify('<strong>Hello </strong>there <strong>person</strong> <strong>there!</strong>'),
result = {
text: 'Hello there person there!',
blocks: {strong: [0, 6, 12, 18, 19, 25]}
};
expect(fn(el)).to.deep.equal(result);
});
it('converts bold to strong and italics and underlines to emphasis', function () {
var el = domify('<i>Hello</i> <b>there</b> <u>person!</u>'),
result = {
text: 'Hello there person!',
blocks: {emphasis: [0, 5, 12, 19], strong: [6, 11]}
};
expect(fn(el)).to.deep.equal(result);
});
it('ignores scripts', function () {
var el = domify('Hello <script>jfkdslajfkdsal</script>there <strong>person</strong>!'),
result = {
text: 'Hello there person!',
blocks: {strong: [12, 18]}
};
expect(fn(el)).to.deep.equal(result);
});
it('merges nested continuous blocks (i.e., bold in bold)', function () {
var el = domify('Hello <strong>there <strong>person</strong></strong>!'),
result = {
text: 'Hello there person!',
blocks: {strong: [6, 18]}
};
expect(fn(el)).to.deep.equal(result);
});
it('merges congruent continuous blocks (i.e., bold in bold)', function () {
var el = domify('Hello <strong>there </strong><strong>person</strong>!'),
result = {
text: 'Hello there person!',
blocks: {strong: [6, 18]}
};
expect(fn(el)).to.deep.equal(result);
});
it('merges nested congruent continuous blocks (i.e., bold in italics in bold)', function () {
var el = domify('Hello <strong>the<em>re </em></strong><em><strong>person</strong></em>!'),
result = {
text: 'Hello there person!',
blocks: {strong: [6, 18], emphasis: [9, 18]}
};
expect(fn(el)).to.deep.equal(result);
});
it('finds propertied blocks (i.e., links)', function () {
var el = domify('Hello <a href="place" alt="hey">there</a> <u>person</u>!'),
result = {
text: 'Hello there person!',
blocks: {
link: [{start: 6, end: 11, href: 'place', alt: 'hey'}],
emphasis: [12, 18]
}
};
expect(fn(el)).to.deep.equal(result);
});
it('finds singled blocks (i.e., line breaks)', function () {
var el = domify('Hello<br>there<br />person!'),
result = {
text: 'Hellothereperson!',
blocks: { 'soft return': [5, 10] }
};
expect(fn(el)).to.deep.equal(result);
});
it('allows multiple singled blocks (i.e., <br><br>)', function () {
var el = domify('Hello<br><br />there person!'),
result = {
text: 'Hellothere person!',
blocks: { 'soft return': [5, 5] }
};
expect(fn(el)).to.deep.equal(result);
});
it('does not merge propertied blocks (i.e., links in links)', function () {
var el = domify('Hello <a href="outer place">there <a href="place" alt="hey">person</a> over there</a>!'),
result = {
text: 'Hello there person over there!',
blocks: {
link: [
{start: 6, end: 12, href: 'outer place'},
{start: 12, end: 18, href: 'place', alt: 'hey'}
]
}
};
expect(fn(el)).to.deep.equal(result);
});
it('removes empty continuous blocks (does not add block name either)', function () {
var el = domify('Hello <strong></strong>there person!'),
result = {
text: 'Hello there person!',
blocks: {}
};
expect(fn(el)).to.deep.equal(result);
});
it('removes empty propertied blocks (does not add block name either)', function () {
var el = domify('Hello <a href="place"></a>there person!'),
result = {
text: 'Hello there person!',
blocks: {}
};
expect(fn(el)).to.deep.equal(result);
});
it('removes empty propertied blocks caused by nesting', function () {
var el = domify('Hello <a href="outer place"><a href="place" alt="hey">there</a> person</a>!'),
result = {
text: 'Hello there person!',
blocks: {
link: [
{start: 6, end: 11, href: 'place', alt: 'hey'}
]
}
};
expect(fn(el)).to.deep.equal(result);
});
});
describe('toElement', function () {
var fn = lib[this.title];
it('doesn\t decode sanitized html', function () {
var model = {
text: '<Hello there person!>',
blocks: {}
},
result = '<Hello there person>!';
expect(documentToString(fn(model))).to.not.equal(result);
});
it('converts continuous blocks', function () {
var model = {
text: 'Hello there person!',
blocks: {strong: [0, 6, 12, 18]}
},
result = '<strong>Hello </strong>there <strong>person</strong>!';
expect(documentToString(fn(model))).to.equal(result);
});
it('nests when different continuous blocks are on the same space', function () {
var model = {
text: 'Hello there person!',
blocks: {strong: [6, 11], emphasis: [6, 11]}
},
result = 'Hello <strong><em>there</em></strong> person!';
expect(documentToString(fn(model))).to.equal(result);
});
it('nests when continuous blocks are already in order', function () {
var model = {
text: 'Hello there person!',
blocks: {strong: [6, 18], emphasis: [12, 18]}
},
result = 'Hello <strong>there <em>person</em></strong>!';
expect(documentToString(fn(model))).to.equal(result);
});
it('nests when continuous blocks are not already in order', function () {
var model = {
text: 'Hello there person!',
blocks: {strong: [12, 18], emphasis: [6, 18]}
},
result = 'Hello <em>there <strong>person</strong></em>!';
expect(documentToString(fn(model))).to.equal(result);
});
it('overlaps when continuous blocks regardless of order', function () {
var model = {
text: 'Hello there person!',
blocks: {strong: [0, 12], emphasis: [6, 18]}
},
result = '<strong>Hello <em>there </em></strong><em>person</em>!';
expect(documentToString(fn(model))).to.equal(result);
});
it('converts propertied blocks', function () {
var model = {
text: 'Hello there person!',
blocks: {link: [{start: 6, end: 18, alt: 'Good day!'}]}
},
result = 'Hello <a alt="Good day!">there person</a>!';
expect(documentToString(fn(model))).to.equal(result);
});
it('nests when continuous blocks applied within propertied blocks', function () {
var model = {
text: 'Hello there person!',
blocks: {strong: [6, 12], link: [{start: 0, end: 18}]}
},
result = '<a>Hello <strong>there </strong>person</a>!';
expect(documentToString(fn(model))).to.equal(result);
});
it('nests when continuous blocks applied outside propertied blocks', function () {
var model = {
text: 'Hello there person!',
blocks: {strong: [0, 18], link: [{start: 6, end: 12}]}
},
result = '<strong>Hello </strong><a><strong>there </strong></a><strong>person</strong>!';
expect(documentToString(fn(model))).to.equal(result);
});
it('overlaps when continuous blocks applied to propertied blocks', function () {
var model = {
text: 'Hello there person!',
blocks: {strong: [0, 12], link: [{start: 6, end: 18}]}
},
result = '<strong>Hello </strong><a><strong>there </strong>person</a>!';
expect(documentToString(fn(model))).to.equal(result);
});
it('converts singled blocks', function () {
var model = {
text: 'Hellothereperson!',
blocks: {'soft return': [5, 10]}
},
result = 'Hello<br>there<br>person!';
expect(documentToString(fn(model))).to.equal(result);
});
it('overlaps when continuous blocks applied to singled blocks', function () {
var model = {
text: 'Hellothere person!',
blocks: {strong: [0, 11], 'soft return': [5]}
},
result = '<strong>Hello<br>there </strong>person!';
expect(documentToString(fn(model))).to.equal(result);
});
it('overlaps when propertied blocks applied to singled blocks', function () {
var model = {
text: 'Hellothere person!',
blocks: {link: [{start: 2, end: 10}], 'soft return': [5]}
},
result = 'He<a>llo<br>there</a> person!';
expect(documentToString(fn(model))).to.equal(result);
});
});
describe('split', function () {
var fn = lib[this.title];
it('splits text', function () {
var num = 8,
el = domify('Hello there person!'),
result,
expectedResult = ['Hello th', 'ere person!'];
result = fn(lib.fromElement(el), num);
result = _.map(result, function (model) {
return documentToString(lib.toElement(model));
});
expect(result).to.deep.equal(expectedResult);
});
it('splits continuous blocks to each side', function () {
var num = 8,
el = domify('<strong>Hello </strong>there <strong>person</strong>!'),
result,
expectedResult = ['<strong>Hello </strong>th', 'ere <strong>person</strong>!'];
result = fn(lib.fromElement(el), num);
result = _.map(result, function (modelResult) {
return documentToString(lib.toElement(modelResult));
});
expect(result).to.deep.equal(expectedResult);
});
it('splits continuous blocks at middle', function () {
var num = 8,
el = domify('Hello <strong>there </strong>person!'),
result,
expectedResult = ['Hello <strong>th</strong>', '<strong>ere </strong>person!'];
result = fn(lib.fromElement(el), num);
result = _.map(result, function (modelResult) {
return documentToString(lib.toElement(modelResult));
});
expect(result).to.deep.equal(expectedResult);
});
it('splits continuous blocks to each side and middle', function () {
var num = 8,
el = domify('<strong>Hello</strong> <strong>there</strong> <strong>person</strong>!'),
result,
expectedResult = ['<strong>Hello</strong> <strong>th</strong>', '<strong>ere</strong> <strong>person</strong>!'];
result = fn(lib.fromElement(el), num);
result = _.map(result, function (modelResult) {
return documentToString(lib.toElement(modelResult));
});
expect(result).to.deep.equal(expectedResult);
});
it('splits propertied blocks to each side', function () {
var num = 8,
el = domify('<a>Hello </a>there <a>person</a>!'),
result,
expectedResult = ['<a>Hello </a>th', 'ere <a>person</a>!'];
result = fn(lib.fromElement(el), num);
result = _.map(result, function (modelResult) {
return documentToString(lib.toElement(modelResult));
});
expect(result).to.deep.equal(expectedResult);
});
it('removes propertied blocks at middle', function () {
var num = 8,
el = domify('Hello <a>there </a>person!'),
result,
expectedResult = ['Hello <a>th</a>', '<a>ere </a>person!'];
result = fn(lib.fromElement(el), num);
result = _.map(result, function (modelResult) {
return documentToString(lib.toElement(modelResult));
});
expect(result).to.deep.equal(expectedResult);
});
it('splits propertied blocks to each side and removes middle', function () {
var num = 8,
el = domify('<a>Hello</a> <a>there</a> <a>person</a>!'),
result,
expectedResult = ['<a>Hello</a> <a>th</a>', '<a>ere</a> <a>person</a>!'];
result = fn(lib.fromElement(el), num);
result = _.map(result, function (modelResult) {
return documentToString(lib.toElement(modelResult));
});
expect(result).to.deep.equal(expectedResult);
});
it('splits singled blocks to each side', function () {
var num = 7,
el = domify('Hello<br>there<br>person!'),
result,
expectedResult = ['Hello<br>th', 'ere<br>person!'];
result = fn(lib.fromElement(el), num);
result = _.map(result, function (modelResult) {
return documentToString(lib.toElement(modelResult));
});
expect(result).to.deep.equal(expectedResult);
});
it('removes singled blocks at middle', function () {
var num = 8,
el = domify('Hello th<br>ere person!'),
result,
expectedResult = ['Hello th', 'ere person!'];
result = fn(lib.fromElement(el), num);
result = _.map(result, function (modelResult) {
return documentToString(lib.toElement(modelResult));
});
expect(result).to.deep.equal(expectedResult);
});
it('removes multiple singled blocks at middle', function () {
var num = 8,
el = domify('Hello th<br><br />ere person!'),
result,
expectedResult = ['Hello th', 'ere person!'];
result = fn(lib.fromElement(el), num);
result = _.map(result, function (modelResult) {
return documentToString(lib.toElement(modelResult));
});
expect(result).to.deep.equal(expectedResult);
});
});
describe('concat', function () {
var fn = lib[this.title];
it('concats continuous', function () {
var result,
before = lib.fromElement(domify('Hello <strong>th</strong>')),
after = lib.fromElement(domify('<strong>ere</strong> person!')),
expectedResult = 'Hello <strong>there</strong> person!';
result = fn(before, after);
expect(documentToString(lib.toElement(result))).to.deep.equal(expectedResult);
});
it('concats nested continuous', function () {
var result,
before = lib.fromElement(domify('Hello <strong><em>th</em></strong>')),
after = lib.fromElement(domify('<strong><em>ere</em></strong> person!')),
expectedResult = 'Hello <strong><em>there</em></strong> person!';
result = fn(before, after);
expect(documentToString(lib.toElement(result))).to.deep.equal(expectedResult);
});
it('concats nested continuous (complex)', function () {
var result,
before = lib.fromElement(domify('<em>Hello <strong>t</strong>h</em>')),
after = lib.fromElement(domify('<strong><em>ere</em></strong> person!')),
expectedResult = '<em>Hello <strong>t</strong>h<strong>ere</strong></em> person!';
result = fn(before, after);
expect(documentToString(lib.toElement(result))).to.deep.equal(expectedResult);
});
it('preserves singled blocks', function () {
var result,
before = lib.fromElement(domify('Hello<br>th')),
after = lib.fromElement(domify('ere<br>person!')),
expectedResult = 'Hello<br>there<br>person!';
result = fn(before, after);
expect(documentToString(lib.toElement(result))).to.deep.equal(expectedResult);
});
});
describe('updateSameAs', function () {
var fn = lib[this.title];
it('uses default mappings for tag conversions', function () {
var el = domify('<b>a</b><u>b</u><i>c</i><h1>d</h1><h3>e</h3><h4>f</h4><h5>g</h5><h6>h</h6><strike>i</strike>'),
result = {
text: 'abcdefghi',
blocks: {
strong: [0, 1],
emphasis: [1, 3],
h2: [3, 8],
del: [8, 9]
}
};
expect(lib.fromElement(el)).to.deep.equal(result);
});
it('merges in user conversions', function () {
var el = domify('<b>a</b><u>b</u><i>c</i><h1>d</h1><h2>e</h2><h3>f</h3><h4>g</h4><h5>h</h5><h6>i</h6><strike>j</strike>'),
result = {
text: 'abcdefghij',
blocks: {
strong: [0, 1, 3, 9],
emphasis: [1, 3],
del: [9, 10]
}
};
lib.resetSameAs();
fn({
H1: 'STRONG',
H2: 'STRONG',
H3: 'STRONG',
H4: 'STRONG',
H5: 'STRONG',
H6: 'STRONG',
});
expect(lib.fromElement(el)).to.deep.equal(result);
});
it('allows removing default conversions', function () {
var el = domify('<b>a</b><u>b</u><i>c</i><h1>d</h1><h3>e</h3><h4>f</h4><h5>g</h5><h6>h</h6><strike>i</strike>'),
result = {
text: 'abcdefghi',
blocks: {
bold: [0, 1],
underline: [1, 2],
italic: [2, 3],
h2: [3, 8],
del: [8, 9]
}
};
lib.resetSameAs();
fn({
B: null,
U: null,
I: null
});
expect(lib.fromElement(el)).to.deep.equal(result);
});
});
describe('resetSameAs', function () {
var fn = lib[this.title];
it('resets the default conversions', function () {
var el = domify('<b>a</b><u>b</u><i>c</i><h1>d</h1><h3>e</h3><h4>f</h4><h5>g</h5><h6>h</h6><strike>i</strike>'),
result = {
text: 'abcdefghi',
blocks: {
strong: [0, 1],
emphasis: [1, 3],
h2: [3, 8],
del: [8, 9]
}
};
// set conversions to some weird state
lib.updateSameAs({
B: null,
U: null,
I: null
});
fn(); // reset them
expect(lib.fromElement(el)).to.deep.equal(result); // default conversions
});
});
});