-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtest.js
486 lines (403 loc) · 15.5 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
const assert = require('node:assert/strict');
const childProcess = require('node:child_process');
const fs = require('node:fs');
const os = require('node:os');
const path = require('node:path');
const util = require('node:util');
const cleaner = require('./index.js');
const results = [];
const tests = [];
function logFail(message) {
return console.error(`\x1b[31m${message}\x1b[0m`);
}
function logPass(message) {
return console.log(`\x1b[32m${message}\x1b[0m`);
}
function registerTest(description, callback) {
tests.push({description, callback});
}
function runTest(description, callback) {
try {
callback();
} catch (error) {
if (error instanceof assert.AssertionError) {
const message = `✗ ${description}\n` +
` Expected: ${util.inspect(error.expected)}\n` +
` Actual: ${util.inspect(error.actual)}`;
logFail(message);
results.push({message, result: 'fail'});
return;
}
const message = `✗ ${description}: ${error}`;
logFail(message);
results.push({message, result: 'fail'});
return;
}
const message = `✓ ${description}`;
logPass(message);
results.push({message, result: 'pass'});
}
function runTests(filter) {
const filteredTests = tests.filter(test => !filter || filter(test));
if (filteredTests.length === 0) {
logFail('No tests satisfy filter');
process.exit(1);
}
for (const test of filteredTests) {
runTest(test.description, test.callback);
}
}
function summarizeResults() {
const numPassed = results.filter(r => r.result == 'pass').length;
const numFailed = results.filter(r => r.result == 'fail').length;
if (numPassed > 0) {
logPass(`Passed: ${numPassed}`);
}
if (numFailed > 0) {
logFail(`Failed: ${numFailed}`);
}
}
function test(description, callback) {
registerTest(description, callback);
}
test('text is unchanged', () => {
cleaner.clean('Foo Bar', html => {
assert.equal(html, 'Foo Bar');
});
});
test('extra whitespace is replaced by a single space', () => {
cleaner.clean('Foo \n Bar', html => {
assert.equal(html, 'Foo Bar');
});
});
test('extra whitespace inside comment is replaced by a single space', () => {
cleaner.clean('<!--\nFoo Bar\n-->', html => {
assert.equal(html, '<!-- Foo Bar -->');
});
});
test('output is trimmed', () => {
cleaner.clean(' foo\n', html => {
assert.equal(html, 'foo');
});
});
test('directive is unchanged', () => {
cleaner.clean('<!DOCTYPE html>', html => {
assert.equal(html, '<!DOCTYPE html>')
});
});
test('empty value is added when allow-attributes-without-values is false', () => {
cleaner.clean('<input name="foo" disabled>', {'allow-attributes-without-values': false}, html => {
assert.equal(html, '<input name="foo" disabled="">');
});
});
test('empty value not added when allow-attributes-without-values is true', () => {
cleaner.clean('<input name="foo" disabled>', {'allow-attributes-without-values': true}, html => {
assert.equal(html, '<input name="foo" disabled>');
});
});
test('line breaks are not added around comments when break-around-comments is false', () => {
cleaner.clean('foo<!-- bar -->qux', {'break-around-comments': false}, html => {
assert.equal(html, 'foo<!-- bar -->qux');
});
});
test('line breaks are added around comments when break-around-comments is true', () => {
cleaner.clean('foo<!-- bar -->qux', {'break-around-comments': true}, html => {
assert.equal(html, 'foo\n<!-- bar -->\nqux');
});
});
test('line breaks are not added around tags when not included in break-around-tags', () => {
cleaner.clean('foo<div></div>bar', {'break-around-tags': []}, html => {
assert.equal(html, 'foo<div></div>bar');
});
});
test('line breaks are added around tags when included in break-around-tags', () => {
cleaner.clean('foo<div></div>bar', {'break-around-tags': ['div']}, html => {
assert.equal(html, 'foo\n<div></div>\nbar');
});
});
test('non-breaking space is not replaced by a single space when decode-entities is false', () => {
cleaner.clean('Foo Bar', {'decode-entities': false}, html => {
assert.equal(html, 'Foo Bar');
});
});
test('non-breaking space is replaced by a single space when decode-entities is true', () => {
cleaner.clean('Foo Bar', {'decode-entities': true}, html => {
assert.equal(html, 'Foo Bar');
});
});
test('tag is lowercased when lower-case-tags is true', () => {
cleaner.clean('<A href="http://foo">bar</A>', {'lower-case-tags': true}, html => {
assert.equal(html, '<a href="http://foo">bar</a>');
});
});
test('tag is not lowercased when lower-case-tags is false', () => {
cleaner.clean('<A href="http://foo">bar</A>', {'lower-case-tags': false}, html => {
assert.equal(html, '<A href="http://foo">bar</A>');
});
});
test('attribute name is lowercased when lower-case-attribute-names is true', () => {
cleaner.clean('<a HREF="http://foo">bar</a>', {'lower-case-attribute-names': true}, html => {
assert.equal(html, '<a href="http://foo">bar</a>');
});
});
test('attribute name is not lowercased when lower-case-attribute-names is false', () => {
cleaner.clean('<a HREF="http://foo">bar</a>', {'lower-case-attribute-names': false}, html => {
assert.equal(html, '<a HREF="http://foo">bar</a>');
});
});
test('tag is not preserved when not included in preserve-tags', () => {
const input = `<script>
function sayHello() {
console.log('Hello, World!');
}
sayHello();
</script>`;
cleaner.clean(input, {'preserve-tags': []}, output => {
assert.notEqual(output, input);
});
});
test('tag is preserved when included in preserve-tags', () => {
const input = `<script>
function sayHello() {
console.log('Hello, World!');
}
sayHello();
</script>`;
cleaner.clean(input, {'preserve-tags': ['script']}, output => {
assert.equal(output, input);
});
});
test('attribute is not removed when not included in remove-attributes', () => {
cleaner.clean('<span color="red">foo</span>', {'remove-attributes': []}, html => {
assert.equal(html, '<span color="red">foo</span>');
});
});
test('attribute is removed when included in remove-attributes', () => {
cleaner.clean('<span color="red">foo</span>', {'remove-attributes': ['color']}, html => {
assert.equal(html, '<span>foo</span>');
});
});
test('attribute is removed when it matches at least one pattern included in remove-attributes', () => {
cleaner.clean('<span _test-color="red">foo</span>', {'remove-attributes': [/_test-[a-z0-9-]+/i]}, html => {
assert.equal(html, '<span>foo</span>');
});
});
test('comment is not removed when remove-comments is false', () => {
cleaner.clean('<!-- foo -->', {'remove-comments': false}, html => {
assert.equal(html, '<!-- foo -->');
});
});
test('comment is removed when remove-comments is true', () => {
cleaner.clean('<!-- foo -->', {'remove-comments': true}, html => {
assert.equal(html, '');
});
});
test('empty tag is not removed when not included in remove-empty-tags', () => {
cleaner.clean('<p></p>', {'remove-empty-tags': []}, html => {
assert.equal(html, '<p></p>');
});
});
test('empty tag is removed when included in remove-empty-tags', () => {
cleaner.clean('<p></p>', {'remove-empty-tags': ['p']}, html => {
assert.equal(html, '');
});
});
test('non-empty tag is not removed when included in remove-empty-tags', () => {
cleaner.clean('<p><img src="foo.jpg"></p>', {'remove-empty-tags': ['p']}, html => {
assert.equal(html, '<p><img src="foo.jpg"></p>');
});
});
test('empty tag is removed when it matches at least one pattern included in remove-empty-tags', () => {
cleaner.clean('<app-pam-pam-pam></app-pam-pam-pam>', {'remove-empty-tags': [/^app-.*/i]}, html => {
assert.equal(html, '');
});
});
test('tag is not removed when not included in remove-tags', () => {
cleaner.clean('<font face="arial">foo</font>', {'remove-tags': []}, html => {
assert.equal(html, '<font face="arial">foo</font>');
});
});
test('tag is removed and child is preserved when included in remove-tags', () => {
cleaner.clean('<font face="arial">foo</font>', {'remove-tags': ['font']}, html => {
assert.equal(html, 'foo');
});
});
test('tag is removed and child is preserved when it matches at least one pattern included in remove-tags', () => {
cleaner.clean('<app-test>foo</app-test>', {'remove-tags': [/app-.+/]}, html => {
assert.equal(html, 'foo');
});
});
// indent tests
test('indent is not added when child is text', () => {
cleaner.clean('foo<span>bar</span>qux', {'indent': ' '}, html => {
assert.equal(html, 'foo<span>bar</span>qux');
});
});
test('indent is not added when child is comment and break-around-comments is false', () => {
cleaner.clean('foo<span><!-- bar --></span>qux', {'break-around-comments': false, 'indent': ' '}, html => {
assert.equal(html, 'foo<span><!-- bar --></span>qux');
});
});
test('indent is added when child is comment and break-around-comments is true', () => {
cleaner.clean('foo<span><!-- bar --></span>qux', {'break-around-comments': true, 'indent': ' '}, html => {
assert.equal(html, 'foo\n<span>\n <!-- bar -->\n</span>\nqux');
});
});
test('indent is not added when child tag is not included in break-around-tags', () => {
cleaner.clean('foo<span><div>bar</div></span>qux', {'break-around-tags': [], 'indent': ' '}, html => {
assert.equal(html, 'foo<span><div>bar</div></span>qux');
});
});
test('indent is added when child tag is included in break-around-tags', () => {
cleaner.clean('foo<span><div>bar</div></span>qux', {'break-around-tags': ['div'], 'indent': ' '}, html => {
assert.equal(html, 'foo\n<span>\n <div>bar</div>\n</span>\nqux');
});
});
test('indent is added when child tag is not included in break-around-tags but descendant is', () => {
cleaner.clean('foo<span><span><div>bar</div></span></span>qux', {'break-around-tags': ['div'], 'indent': ' '}, html => {
assert.equal(html, 'foo\n<span>\n <span>\n <div>bar</div>\n </span>\n</span>\nqux');
});
});
test('indent is not added inside comment', () => {
cleaner.clean('<!-- foo<span><div>bar</div></span>qux -->', {'break-around-tags': ['div'], 'indent': ' '}, html => {
assert.equal(html, '<!-- foo<span><div>bar</div></span>qux -->');
});
});
test('indent is not added after comment', () => {
cleaner.clean('<!--[if IE 7]><div><![endif]--><div>foo</div>', {'break-around-tags': ['div'], 'indent': ' '}, html => {
assert.equal(html, '<!--[if IE 7]><div><![endif]-->\n<div>foo</div>');
});
});
// wrap tests
test('long line is wrapped with hanging indent', () => {
cleaner.clean('<div>I prefer the concrete, the graspable, the proveable.</div>', {'wrap': 40}, html => {
assert.equal(html, '<div>I prefer the concrete, the\n graspable, the proveable.</div>');
});
});
test('long line without whitespace is not wrapped', () => {
cleaner.clean('<div>Iprefertheconcrete,thegraspable,theproveable.</div>', {'wrap': 40}, html => {
assert.equal(html, '<div>Iprefertheconcrete,thegraspable,theproveable.</div>');
});
});
test('long line inside nested tag is wrapped with hanging indent', () => {
cleaner.clean('<div><div>I prefer the concrete, the graspable, the proveable.</div></div>', {'wrap': 40}, html => {
assert.equal(html, '<div>\n <div>I prefer the concrete, the\n graspable, the proveable.</div>\n</div>');
});
});
test('long line without whitespace inside nested tag is not wrapped', () => {
cleaner.clean('<div><div>Iprefertheconcrete,thegraspable,theproveable.</div></div>', {'wrap': 40}, html => {
assert.equal(html, '<div>\n <div>Iprefertheconcrete,thegraspable,theproveable.</div>\n</div>');
});
});
test('long comment is wrapped and indented', () => {
cleaner.clean('<!-- I prefer the concrete, the graspable, the proveable. -->', {'wrap': 40}, html => {
assert.equal(html, '<!-- I prefer the concrete, the\n graspable, the proveable. -->');
});
});
// command line tests
test('command line read from stdin and write to stdout', () => {
const input = fs.readFileSync('test.html', 'utf8');
const expected = `<table>
<tr>
<td>
<b>Currently we have these articles available:</b>
<blockquote>
<p>
<a href="foo.html">The History of Foo</a>
<br>
An <span>informative</span> piece of information.
</p>
<p>
<a href="bar.html">A Horse Walked Into a Bar</a>
<br>
The bartender said "Why the long face?"
</p>
</blockquote>
</td>
</tr>
</table>\n`;
const actual = childProcess.execFileSync('node', ['cmd.js'], {encoding: 'utf8', input: input});
assert.equal(actual, expected);
});
test('command line read from file and write to stdout', () => {
const expected = `<table>
<tr>
<td>
<b>Currently we have these articles available:</b>
<blockquote>
<p>
<a href="foo.html">The History of Foo</a>
<br>
An <span>informative</span> piece of information.
</p>
<p>
<a href="bar.html">A Horse Walked Into a Bar</a>
<br>
The bartender said "Why the long face?"
</p>
</blockquote>
</td>
</tr>
</table>\n`;
const actual = childProcess.execFileSync('node', ['cmd.js', 'test.html'], {encoding: 'utf8'});
assert.equal(actual, expected);
});
test('command line read from file and write to stdout with options', () => {
const expected = `<b>Currently we have these articles available:</b>
<p>
<a href="foo.html">The History of Foo</a>
<br>
An <span>informative</span> piece of information.
</p>
<p>
<a href="bar.html">A Horse Walked Into a Bar</a>
<br>
The bartender said "Why the long face?"
</p>\n`;
const actual = childProcess.execFileSync(
'node',
['cmd.js', 'test.html', '--add-remove-tags', 'table,tr,td,blockquote'],
{encoding: 'utf8'}
);
assert.equal(actual, expected);
});
test('command line read from file and write to file in place', () => {
const expected = `<table>
<tr>
<td>
<b>Currently we have these articles available:</b>
<blockquote>
<p>
<a href="foo.html">The History of Foo</a>
<br>
An <span>informative</span> piece of information.
</p>
<p>
<a href="bar.html">A Horse Walked Into a Bar</a>
<br>
The bartender said "Why the long face?"
</p>
</blockquote>
</td>
</tr>
</table>\n`;
const tempDirPrefix = path.join(os.tmpdir(), 'clean-html-');
const tempDir = fs.mkdtempSync(tempDirPrefix);
try {
const tempFile = path.join(tempDir, 'test.html');
fs.copyFileSync('test.html', tempFile, fs.constants.COPYFILE_EXCL);
childProcess.execFileSync('node', ['cmd.js', tempFile, '--in-place']);
const actual = fs.readFileSync(tempFile, 'utf8');
assert.equal(actual, expected);
} finally {
fs.rmSync(tempDir, {recursive: true});
}
});
const args = process.argv.slice(2);
if (args.length > 0) {
const argPatterns = args.map(arg => new RegExp(arg));
runTests((test) => argPatterns.some(pattern => pattern.test(test.description)));
} else {
runTests();
}
summarizeResults();