-
Notifications
You must be signed in to change notification settings - Fork 1
/
run-tests.js
756 lines (680 loc) · 20.3 KB
/
run-tests.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
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
/* eslint-disable prefer-arrow-callback, func-names */
/* eslint no-param-reassign: ["error", { "props": true, "ignorePropertyModificationsFor": ["node", "container"] }] */
import assert from 'assert';
import { sanitizeNode, sanitizeChildNodes, sanitizeHtml } from '../src/index.js';
function runTests(doc, container) {
const nodePropertyMap = new WeakMap();
function run(html, opts = {}) {
return sanitizeHtml(doc, html, opts, false, nodePropertyMap);
}
describe('initialization', () => {
it('throws when no DOM API passed in', () => {
assert.throws(
(function () {
return function () {
const node = doc.createElement('div');
sanitizeChildNodes({}, node);
};
}()), /Need DOM Document interface/,
);
});
it('throws when node of wrong interface passed in', () => {
assert.throws(
(function () {
return function () {
doc.createElement('div');
sanitizeChildNodes(doc, {});
};
}()), /Need DOM Node interface/,
);
});
});
specify('sanitizeNode works', () => {
const par = doc.createElement('P');
const span = doc.createElement('SPAN');
par.appendChild(span);
span.innerHTML = 'abc';
sanitizeNode(doc, par, {
allow_tags_direct: {
'.*': '^P',
},
});
assert.equal(par.outerHTML, '<p>abc</p>');
});
it('can work with documents (with the HTML level)', () => {
const html = '<html><head><script>console.log("hi");</script></head><body>hello</body></html>';
assert.equal(
sanitizeHtml(doc, html, {
allow_tags_direct: {
'.*': '.*',
},
}, true),
html,
);
});
specify('the context node for non-document HTML is BODY', () => {
assert.equal(
run('<b>abc</b>', {
allow_tags_direct: {
body: 'b',
},
}),
'<b>abc</b>',
);
});
describe('join_siblings', () => {
it('joins same-tag siblings of specified tags, keeping children intact', () => {
assert.equal(
run('<b>abc</b> <b>def <u>ghi</u></b> <i>jkl</i> <i>mno</i>', {
join_siblings: ['B'],
allow_tags_direct: {
'.*': '.*',
},
}),
'<b>abc def <u>ghi</u></b> <i>jkl</i> <i>mno</i>',
);
});
it('joins same-tag siblings when separated by whitespace', () => {
assert.equal(
run('<b>abc</b> <b>def</b> <b>ghi</b>', {
join_siblings: ['B', 'I'],
allow_tags_direct: {
'.*': '.*',
},
}),
'<b>abc def ghi</b>',
);
});
it('joins same-tag siblings when not separated by whitespace', () => {
assert.equal(
run('<b>abc</b><b>def</b><b>ghi</b>', {
join_siblings: ['B', 'I'],
allow_tags_direct: {
'.*': '.*',
},
}),
'<b>abcdefghi</b>',
);
});
it('does not join same-tag siblings when separated by non-whitespace text', () => {
assert.equal(
run('<b>abc</b> x <b>def</b>', {
join_siblings: ['B', 'I'],
allow_tags_direct: {
'.*': '.*',
},
}),
'<b>abc</b> x <b>def</b>',
);
});
});
it('flattens all tags by default', () => {
assert.equal(
run('<div><p>abc <b class="klass">def</b></p></div>'),
'abc def',
);
});
describe('allow_tags', () => {
it('keeps all tags using wildcard tag specifiers', () => {
assert.equal(
run('<div><p>abc <b>def</b></p></div>', {
allow_tags_direct: {
'.*': '.*',
},
}),
'<div><p>abc <b>def</b></p></div>',
);
});
it('respects text nodes at top level', () => {
assert.equal(
run('abc <p>def</p>', {
allow_tags_direct: {
'.*': '.*',
},
}),
'abc <p>def</p>',
);
});
describe('allow_tags_direct', () => {
it('keeps only direct children of BODY', () => {
assert.equal(
run('<i><b>abc</b></i> <p>def <b>ghi</b> <i><b>jkl</b></i></p>', {
allow_tags_direct: {
BODY: '.*',
},
}),
'<i>abc</i> <p>def ghi jkl</p>',
);
});
it('keeps I and direct B children of I', () => {
assert.equal(
run('<i><b>abc</b></i><p>def <b>ghi</b> <i><b>jkl</b></i></p>', {
allow_tags_direct: {
'.*': 'I',
I: 'B',
},
}),
'<i><b>abc</b></i>def ghi <i><b>jkl</b></i>',
);
});
it('keeps only P, H1 and H2, specified by regexp', () => {
assert.equal(
run('<h1>abc</h1><p><span>def</span>ghi</p><h2>jkl</h2><p>mno</p><h3>pqr</h3>', {
allow_tags_direct: {
'.*': ['H[1-2]', '^P'],
},
}),
'<h1>abc</h1><p>defghi</p><h2>jkl</h2><p>mno</p>pqr',
);
});
});
describe('allow_tags_deep', () => {
it('keeps P and its B and U descendants', () => {
assert.equal(
run('<i><u>abc</u></i> <p>def <i><b>ghi</b> <u>jkl</u></i></p>', {
allow_tags_deep: {
'.*': 'P',
P: ['B', 'U'],
},
}),
'abc <p>def <b>ghi</b> <u>jkl</u></p>',
);
});
it('keeps deep U and I, specified by regexp', () => {
assert.equal(
run('<i>abc</i> <b>def</b>', {
allow_tags_deep: {
'.*': '(I|B)',
},
}),
'<i>abc</i> <b>def</b>',
);
});
});
});
describe('flatten_tags', () => {
it('gives flatten_tags_* precedence over allow_tags', () => {
assert.equal(
run('<b>abc</b> <p>def <b>ghi</b> <i><b>jkl</b></i></p>', {
flatten_tags_deep: {
'.*': '.*',
},
allow_tags_direct: {
'.*': '.*',
},
}),
'abc def ghi jkl',
);
});
it('flattens direct B children of P', () => {
assert.equal(
run('<b>abc</b><p>def <b>ghi</b> <i><b>jkl</b></i> <b>mno</b></p>', {
flatten_tags_direct: {
P: ['B'],
},
allow_tags_direct: {
'.*': '.*',
},
}),
'<b>abc</b><p>def ghi <i><b>jkl</b></i> mno</p>',
);
});
it('flattens redundant B', () => {
assert.equal(
run('<b>abc <i>def <b>ghi</b></i></b>', {
flatten_tags_deep: {
'^B$': ['B'],
},
allow_tags_direct: {
'.*': '.*',
},
}),
'<b>abc <i>def ghi</i></b>',
);
});
});
describe('remove_tags', () => {
it('removes everything except U and keeps its text', () => {
assert.equal(
run('<i>abc</i><u>def</u><b>ghi</b>', {
remove_tags_deep: {
'.*': '[^u]',
},
}),
'def',
);
});
it('removes direct B children of P', () => {
assert.equal(
run('<b>abc</b> <p>def <b>ghi</b> <i><b>jkl</b></i> <b>mno</b></p>', {
remove_tags_direct: {
P: ['B'],
},
allow_tags_direct: {
'.*': '.*',
},
}),
'<b>abc</b> <p>def <i><b>jkl</b></i> </p>',
);
});
it('removes deeply nested B children of P, and remove remaining empty I tag', () => {
assert.equal(
run('<b>abc</b> <p>def <b>ghi</b> <i><b>jkl</b></i> <b>mno</b></p>', {
remove_empty: true,
remove_tags_deep: {
P: ['B'],
},
allow_tags_direct: {
'.*': '.*',
},
}),
'<b>abc</b> <p>def </p>',
);
});
});
describe('remove_empty', () => {
it('removes empty nodes', () => {
assert.equal(
run('<b></b>', {
remove_empty: true,
allow_tags_direct: {
'.*': '.*',
},
}),
'',
);
});
it('does not remove a node just containing another empty node', () => {
// Here we need to create the DOM manually because jsdom doesn't seem to
// parse the string '<b><i></i></b>' properly. It says that B has no
// child nodes.
container.innerHTML = '';
const boldElement = doc.createElement('B');
const italicElement = doc.createElement('I');
const txtnd = doc.createTextNode('');
italicElement.appendChild(txtnd);
boldElement.appendChild(italicElement);
container.appendChild(boldElement);
sanitizeChildNodes(doc, container, {
remove_empty: true,
allow_tags_direct: {
'.*': '.*',
},
}, nodePropertyMap);
assert.equal(container.innerHTML, '<b><i></i></b>');
});
});
describe('filters', () => {
it('replaces `o` characters with IMG tags, and replaces `l` characters with `L`, using two consecutive filters', () => {
assert.equal(
run('Hello World', {
allow_tags_direct: { '.*': '.*' },
allow_attributes_by_tag: { '.*': ['.*'] },
allow_url_schemes_by_tag: {
IMG: ['https'],
},
filters_by_tag: {
TEXT: [
function replaceOWithImg(node) {
const content = node.textContent;
const elems = [];
let match;
let lastIndex = 0;
let matchedAny;
const regex = /o/g;
while ((match = regex.exec(content))) {
const txtBefore = content.substring(lastIndex, match.index);
if (txtBefore) {
const tn1 = doc.createTextNode(txtBefore);
nodePropertyMap.set(tn1, { skip_filters: false });
elems.push(tn1);
}
// const emojichar = match[0];
const img = doc.createElement('IMG');
img.alt = 'o';
img.src = 'https://link/to/o.png';
elems.push(img);
lastIndex = regex.lastIndex;
matchedAny = true;
}
if (matchedAny) {
const rest = content.substring(lastIndex);
if (rest) {
// console.log('emoji rest', rest);
const tn2 = doc.createTextNode(rest);
nodePropertyMap.set(tn2, { skip_filters: false });
elems.push(tn2);
}
return elems;
}
return node;
},
function (node) {
node.textContent = node.textContent.replace(/l/g, 'L');
return node;
},
],
},
}),
'HeLL<img alt="o" src="https://link/to/o.png"> W<img alt="o" src="https://link/to/o.png">rLd',
);
});
it('passes a list of parents to the filter', () => {
run('<p>abc <i><b>def</b></i></p>', {
allow_tags_direct: {
'.*': '.*',
},
filters_by_tag: {
B: [function (node, { parents }) {
assert.equal(parents[0].nodeName, 'I');
assert.equal(parents[1].nodeName, 'P');
}],
},
});
});
it('passes the current sibling index to the filter', () => {
run('<p><b>abc</b><i>def</i><u>jkl</u></p>', {
allow_tags_direct: {
'.*': '.*',
},
filters_by_tag: {
B: [function (node, { siblingIndex }) {
assert.equal(siblingIndex, 0);
}],
I: [function (node, { siblingIndex }) {
assert.equal(siblingIndex, 1);
}],
U: [function (node, { siblingIndex }) {
assert.equal(siblingIndex, 2);
}],
},
});
});
it('removes B tag', () => {
assert.equal(
run('<p>abc <i><b>def</b></i></p>', {
allow_tags_direct: {
'.*': '.*',
},
filters_by_tag: {
B: [function () {
return null; // returning null will remove the node
}],
},
}),
'<p>abc <i></i></p>',
);
});
it('modifies B tag to have a different innerText', () => {
assert.equal(
run('<p>abc <i><b>def</b></i></p>', {
allow_tags_direct: {
'.*': '.*',
},
filters_by_tag: {
B: [
function changesInnerHtml(node, { parentNodenames }) {
node.innerHTML = parentNodenames.join(', ');
return node;
},
],
},
}),
'<p>abc <i><b>I, P, BODY</b></i></p>',
);
});
it('changes B tag to EM tag', () => {
assert.equal(
run('<p>abc <i><b>def</b></i></p>', {
allow_tags_direct: {
'.*': '.*',
},
filters_by_tag: {
B: [
function changesToEm() {
const em = doc.createElement('em');
em.innerHTML = 'ghi';
return em;
},
],
},
}),
'<p>abc <i><em>ghi</em></i></p>',
);
});
it('changes B tag to EM tag, then modifies EM tag', () => {
assert.equal(
run('<p>abc <i><b>def</b></i></p>', {
allow_tags_direct: {
'.*': '.*',
},
filters_by_tag: {
B: [
function changesToEm() {
const em = doc.createElement('em');
em.innerHTML = 'changed';
return em;
},
],
EM: [
function changesInnerHtml(node, { parentNodenames }) {
node.innerHTML = parentNodenames.join('-');
return node;
},
],
},
}),
'<p>abc <i><em>I-P-BODY</em></i></p>',
);
});
it("throws when a filter returns the same node type but doesn't set the property 'skip_filters'", () => {
assert.throws(
(function () {
return function () {
run('<p>Paragraph</p>', {
allow_tags_direct: {
'.*': '.*',
},
filters_by_tag: {
P: [
function wouldCreateInfiniteLoop() {
const par = doc.createElement('P');
par.innerHTML = 'txt';
return par;
},
],
},
});
};
}()),
/Prevented possible infinite loop/,
);
});
it('prevents an infinite loop by setting skip_filters', () => {
assert.equal(
run('<p>Paragraph</p>', {
allow_tags_direct: {
'.*': '.*',
},
filters_by_tag: {
P: [
function replaceWithAnotherP() {
const par = doc.createElement('P');
par.innerHTML = 'txt';
nodePropertyMap.set(par, { skip_filters: true });
return par;
},
],
},
}),
'<p>txt</p>',
);
});
it('skips later filters', () => {
assert.equal(
run('<p><span class="a">foo</span><span>abc</span></p>', {
allow_tags_direct: {
'.*': '.*',
},
filters_by_tag: {
SPAN: [
function maybeSkipLaterFilters(node) {
if (node.className.includes('a')) nodePropertyMap.set(node, { skip_filters: true });
return node;
},
function replaceFooWithBar(node) {
node.innerHTML = node.innerHTML.replace('foo', 'bar');
return node;
},
],
},
}),
'<p><span>foo</span><span>abc</span></p>',
);
});
it('skips filtering nodes with skip_filters property, set before processing', () => {
container.innerHTML = '<p><span>foo</span><span>foo</span></p>';
const firstspan = container.getElementsByTagName('span')[0];
nodePropertyMap.set(firstspan, { skip_filters: true });
sanitizeChildNodes(doc, container, {
allow_tags_direct: {
'.*': '.*',
},
filters_by_tag: {
SPAN: [
function replaceFooWithBar(node) {
node.innerHTML = node.innerHTML.replace('foo', 'bar');
return node;
},
],
},
}, nodePropertyMap);
assert.equal(
container.innerHTML,
'<p><span>foo</span><span>bar</span></p>',
);
});
it('removes skip_filters property', () => {
container.innerHTML = '<p><span>foo</span><span>foo</span></p>';
const firstspan = container.getElementsByTagName('span')[0];
nodePropertyMap.set(firstspan, { skip_filters: true });
sanitizeChildNodes(doc, container, {
allow_tags_direct: {
'.*': '.*',
},
filters_by_tag: {
SPAN: [
function replaceFooWithBar(node) {
node.innerHTML = node.innerHTML.replace('foo', 'bar');
return node;
},
],
},
}, nodePropertyMap);
sanitizeChildNodes(doc, container, {
allow_tags_direct: {
'.*': '.*',
},
filters_by_tag: {
SPAN: [
function replaceFooWithBar(node) {
node.innerHTML = node.innerHTML.replace('foo', 'bar');
return node;
},
],
},
}, nodePropertyMap);
assert.equal(
container.innerHTML,
'<p><span>bar</span><span>bar</span></p>',
);
});
});
describe('skip property', () => {
it('skips sanitization when `skip` property present, then removes `skip` property', () => {
container.innerHTML = '<p><span>foo</span><span>foo</span></p>';
const firstspan = container.getElementsByTagName('span')[0];
nodePropertyMap.set(firstspan, { skip: true });
sanitizeChildNodes(doc, container, {
allow_tags_direct: {
'.*': ['^P'],
},
}, nodePropertyMap);
assert.equal(
container.innerHTML,
'<p><span>foo</span>foo</p>',
);
sanitizeChildNodes(doc, container, {
allow_tags_direct: {
'.*': ['^P'],
},
}, nodePropertyMap);
assert.equal(
container.innerHTML,
'<p>foofoo</p>',
);
});
});
describe('allow_attributes_by_tag', () => {
it('keeps all attributes', () => {
assert.equal(
run('<div><span abc="def">ghi</span></div>', {
allow_tags_direct: { '.*': '.*' },
allow_attributes_by_tag: { '.*': '.*' },
}),
'<div><span abc="def">ghi</span></div>',
);
});
it('keeps only allowed attributes, using regex', () => {
assert.equal(
run('<div><span hello="abc" hi="def" ghi="jkl">mno</span></div>', {
allow_tags_direct: { '.*': '.*' },
allow_attributes_by_tag: { '^s.*': '^h.*' },
}),
'<div><span hello="abc" hi="def">mno</span></div>',
);
});
it('removes all attributes', () => {
assert.equal(
run('<div aaa="1" bbb="2"></div>', {
allow_tags_direct: { '.*': '.*' },
}),
'<div></div>',
);
});
});
describe('allow_classes_by_tag', () => {
it('keeps all classes', () => {
assert.equal(
run('<div class="one">txt</div>', {
allow_tags_direct: { '.*': '.*' },
allow_classes_by_tag: { '.*': '.*' },
}),
'<div class="one">txt</div>',
);
});
it('keeps only allowed classes, using regex', () => {
assert.equal(
run('<div class="one two three thirty"><span class="two twenty thirty">txt</span></div>', {
allow_tags_direct: { '.*': '.*' },
allow_attributes_by_tag: { '.*': '.*' },
allow_classes_by_tag: {
DIV: ['th.*'],
SPAN: ['.*ty'],
},
}),
'<div class="three thirty"><span class="twenty thirty">txt</span></div>',
);
});
it('removes class attribute when no classes remaining', () => {
assert.equal(
run('<div class="abc">txt</div>', {
allow_tags_direct: { '.*': '.*' },
allow_attributes_by_tag: { '.*': '.*' },
}),
'<div>txt</div>',
);
});
});
}
export default runTests;