UNPKG

6.68 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 extra whitespace is replaced by a single space
10cleaner.clean('Foo Bar', function (html) {
11 assert.equal(html, 'Foo Bar');
12});
13cleaner.clean('Foo\nBar', function (html) {
14 assert.equal(html, 'Foo Bar');
15});
16
17// test that output is trimmed
18cleaner.clean(' foo\n', function (html) {
19 assert.equal(html, 'foo');
20});
21
22// test that line breaks are not added around comments when break-around-comments is false
23cleaner.clean('foo<!-- bar -->qux', {'break-around-comments': false}, function (html) {
24 assert.equal(html, 'foo<!-- bar -->qux');
25});
26// test that line breaks are added around comments when break-around-comments is true
27cleaner.clean('foo<!-- bar -->qux', {'break-around-comments': true}, function (html) {
28 assert.equal(html, 'foo\n<!-- bar -->\nqux');
29});
30
31// test that line breaks are not added around tags when not included in break-around-tags
32cleaner.clean('foo<div></div>bar', {'break-around-tags': []}, function (html) {
33 assert.equal(html, 'foo<div></div>bar');
34});
35// test that line breaks are added around tags when included in break-around-tags
36cleaner.clean('foo<div></div>bar', {'break-around-tags': ['div']}, function (html) {
37 assert.equal(html, 'foo\n<div></div>\nbar');
38});
39
40// test that attributes are not removed when not included in remove-attributes
41cleaner.clean('<span color="red">foo</span>', {'remove-attributes': []}, function (html) {
42 assert.equal(html, '<span color="red">foo</span>');
43});
44// test that attributes are removed when included in remove-attributes
45cleaner.clean('<span color="red">foo</span>', {'remove-attributes': ['color']}, function (html) {
46 assert.equal(html, '<span>foo</span>');
47});
48
49// test that comments are not removed when remove-comments is false
50cleaner.clean('<!-- foo -->', {'remove-comments': false}, function (html) {
51 assert.equal(html, '<!-- foo -->');
52});
53// test that comments are removed when remove-comments is true
54cleaner.clean('<!-- foo -->', {'remove-comments': true}, function (html) {
55 assert.equal(html, '');
56});
57
58// test that empty tags are not removed when not included in remove-empty-tags
59cleaner.clean('<p></p>', {'remove-empty-tags': []}, function (html) {
60 assert.equal(html, '<p></p>');
61});
62// test that empty tags are removed when included in remove-empty-tags
63cleaner.clean('<p></p>', {'remove-empty-tags': ['p']}, function (html) {
64 assert.equal(html, '');
65});
66
67// test that tags are not removed when not included in remove-tags
68cleaner.clean('<font face="arial">foo</font>', {'remove-tags': []}, function (html) {
69 assert.equal(html, '<font face="arial">foo</font>');
70});
71// test that tags are removed when included in remove-tags
72cleaner.clean('<font face="arial">foo</font>', {'remove-tags': ['font']}, function (html) {
73 assert.equal(html, 'foo');
74});
75
76// test that non-breaking space is not replaced by a single space when replace-nbsp is false
77cleaner.clean('Foo&nbsp;Bar', {'replace-nbsp': false}, function (html) {
78 assert.equal(html, 'Foo&nbsp;Bar');
79});
80// test that non-breaking space is replaced by a single space when replace-nbsp is true
81cleaner.clean('Foo&nbsp;Bar', {'replace-nbsp': true}, function (html) {
82 assert.equal(html, 'Foo Bar');
83});
84
85// ----------
86// indent tests
87// ----------
88
89// test indent when parent is not included in break-around-tags and...
90// child is text - indent is not added
91cleaner.clean('foo<span>bar</span>qux', {'indent': ' '}, function (html) {
92 assert.equal(html, 'foo<span>bar</span>qux');
93});
94// child is comment and break-around-comments is false - indent is not added
95cleaner.clean('foo<span><!-- bar --></span>qux', {'break-around-comments': false, 'indent': ' '}, function (html) {
96 assert.equal(html, 'foo<span><!-- bar --></span>qux');
97});
98// child is comment and break-around-comments is true - indent is added
99cleaner.clean('foo<span><!-- bar --></span>qux', {'break-around-comments': true, 'indent': ' '}, function (html) {
100 assert.equal(html, 'foo\n<span>\n <!-- bar -->\n</span>\nqux');
101});
102// child tag is not included in break-around-tags - indent is not added
103cleaner.clean('foo<span><span>bar</span></span>qux', {'indent': ' '}, function (html) {
104 assert.equal(html, 'foo<span><span>bar</span></span>qux');
105});
106// child tag is included in break-around-tags - indent is added
107cleaner.clean('foo<span><div>bar</div></span>qux', {'break-around-tags': ['div'], 'indent': ' '}, function (html) {
108 assert.equal(html, 'foo\n<span>\n <div>bar</div>\n</span>\nqux');
109});
110// child tag is not included in break-around-tags but contains a tag that is
111cleaner.clean('foo<span><span><div>bar</div></span></span>qux', {'break-around-tags': ['div'], 'indent': ' '}, function (html) {
112 assert.equal(html, 'foo\n<span>\n <span>\n <div>bar</div>\n </span>\n</span>\nqux');
113});
114
115// test indent when parent is included in break-around-tags and...
116// child is text - indent is not added
117cleaner.clean('foo<div>bar</div>qux', {'break-around-tags': ['div'], 'indent': ' '}, function (html) {
118 assert.equal(html, 'foo\n<div>bar</div>\nqux');
119});
120// child is comment and break-around-comments is false - indent is not added
121cleaner.clean('foo<div><!-- bar --></div>qux', {'break-around-comments': false, 'break-around-tags': ['div'], 'indent': ' '}, function (html) {
122 assert.equal(html, 'foo\n<div><!-- bar --></div>\nqux');
123});
124// child is comment and break-around-comments is true - indent is added
125cleaner.clean('foo<div><!-- bar --></div>qux', {'break-around-comments': true, 'break-around-tags': ['div'], 'indent': ' '}, function (html) {
126 assert.equal(html, 'foo\n<div>\n <!-- bar -->\n</div>\nqux');
127});
128// child tag is not included in break-around-tags - indent is not added
129cleaner.clean('foo<div><span>bar</span></div>qux', {'break-around-tags': ['div'], 'indent': ' '}, function (html) {
130 assert.equal(html, 'foo\n<div><span>bar</span></div>\nqux');
131});
132// child tag is included in break-around-tags - indent is added
133cleaner.clean('foo<div><div>bar</div></div>qux', {'break-around-tags': ['div'], 'indent': ' '}, function (html) {
134 assert.equal(html, 'foo\n<div>\n <div>bar</div>\n</div>\nqux');
135});
136// child tag is not included in break-around-tags but contains a tag that is
137cleaner.clean('foo<div><span><div>bar</div></span></div>qux', {'break-around-tags': ['div'], 'indent': ' '}, function (html) {
138 assert.equal(html, 'foo\n<div>\n <span>\n <div>bar</div>\n </span>\n</div>\nqux');
139});
140
141console.log('all tests passed');