UNPKG

2.71 kBJavaScriptView Raw
1var assert = require('assert'),
2 cleaner = require('./index.js');
3
4// test that text is unchanged
5cleaner.clean('Foo Bar', function (html) {
6 assert.equal(html, 'Foo Bar');
7});
8
9// test that non-breaking space is replaced
10cleaner.clean('Foo Bar', function (html) {
11 assert.equal(html, 'Foo Bar');
12});
13cleaner.clean('Foo Bar', {'replace-nbsp': true}, function (html) {
14 assert.equal(html, 'Foo Bar');
15});
16
17// test that extra whitespace is removed
18cleaner.clean('Foo Bar', function (html) {
19 assert.equal(html, 'Foo Bar');
20});
21cleaner.clean('Foo\nBar', function (html) {
22 assert.equal(html, 'Foo Bar');
23});
24
25// test that comments are removed
26cleaner.clean('<!-- foo -->', function (html) {
27 assert.equal(html, '<!-- foo -->');
28});
29cleaner.clean('<!-- foo -->', {'remove-comments': true}, function (html) {
30 assert.equal(html, '');
31});
32
33// test that lines breaks are added before and after comments
34cleaner.clean('foo<!-- bar -->qux', function (html) {
35 assert.equal(html, 'foo\n<!-- bar -->\nqux');
36});
37cleaner.clean('foo<!-- bar -->qux', {'break-around-comments': false}, function (html) {
38 assert.equal(html, 'foo<!-- bar -->qux');
39});
40
41// test that empty paragraph tags are removed
42cleaner.clean('<p>\n</p>', function (html) {
43 assert.equal(html, '<p>\n</p>');
44});
45cleaner.clean('<p>\n</p>', {'remove-empty-paras': true}, function (html) {
46 assert.equal(html, '');
47});
48
49// test that deprecated tags are removed
50cleaner.clean('<font face="arial">foo</font>', function (html) {
51 assert.equal(html, 'foo');
52});
53
54// test that legacy attributes are removed
55cleaner.clean('<span color="red">foo</span>', function (html) {
56 assert.equal(html, '<span>foo</span>');
57});
58
59// test that line break is added after br tag
60cleaner.clean('foo<br>bar', function (html) {
61 assert.equal(html, 'foo<br>\nbar');
62});
63cleaner.clean('foo<br>bar', {'break-after-br': false}, function (html) {
64 assert.equal(html, 'foo<br>bar');
65});
66
67// test that line breaks are added before and after block element tags
68cleaner.clean('foo<div></div>bar', function (html) {
69 assert.equal(html, 'foo\n<div>\n</div>\nbar');
70});
71
72// test that nested tags are indented after block element tags
73cleaner.clean('<div>foo</div>', function (html) {
74 assert.equal(html, '<div>\n foo\n</div>');
75});
76cleaner.clean('<div><div>foo</div></div>', function (html) {
77 assert.equal(html, '<div>\n <div>\n foo\n </div>\n</div>');
78});
79cleaner.clean('<div>foo</div>', {'indent': ' '}, function (html) {
80 assert.equal(html, '<div>\n foo\n</div>');
81});
82
83// test that output is trimmed
84cleaner.clean(' foo\n', function (html) {
85 assert.equal(html, 'foo');
86});
87
88console.log('all tests passed');