-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathsanitization-tests.js
169 lines (149 loc) · 4.8 KB
/
sanitization-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
var should = require('should');
var helpers = require('./helpers');
describe('Sanitization', function() {
it('should convert tags to lowercase', function() {
var sanitized = helpers.parser.sanitize('<FOO></FOO>');
sanitized.should.equal('<foo></foo>');
});
it('should convert attribute names to lowercase', function() {
var sanitized = helpers.parser.sanitize('<foo BAR="BAZ">');
sanitized.should.equal('<foo bar="BAZ">');
});
it('should remove doctypes', function() {
var sanitized = helpers.parser.sanitize('<!doctype html><foo><!doctype asdf></foo>', {
docTypes: true
});
sanitized.should.equal('<foo></foo>');
});
it('should remove doctypes with callback', function() {
var sanitized = helpers.parser.sanitize('<!doctype html><foo><!doctype asdf></foo>', {
docTypes: function(value) {
return value !== 'html';
}
});
sanitized.should.equal('<!doctype html><foo></foo>');
});
it('should remove comments', function() {
var sanitized = helpers.parser.sanitize('<!-- foo --><foo><!-- foo --></foo><!-- foo -->', {
comments: true
});
sanitized.should.equal('<foo></foo>');
});
it('should remove comments with callback', function() {
var sanitized = helpers.parser.sanitize('<!-- foo --><foo><!-- bar --></foo><!-- foo -->', {
comments: function(value) {
return /foo/.test(value);
}
});
sanitized.should.equal('<foo><!-- bar --></foo>');
});
it('should remove specified attributes', function() {
var sanitized = helpers.parser.sanitize('<foo bar="baz" bat="qux"></foo>', {
attributes: [ 'bar' ]
});
sanitized.should.equal('<foo bat="qux"></foo>');
});
it('should remove attributes with callback', function() {
var sanitized = helpers.parser.sanitize('<foo bar="baz" bat="qux"></foo>', {
attributes: function(name, value) {
return name === 'bar';
}
});
sanitized.should.equal('<foo bat="qux"></foo>');
});
it('should remove specified elements', function() {
var html = '<foo><bar><baz><bat foo=bar>asdf</bat></baz></bar><bat><!-- comment --></bat></foo>';
var sanitized = helpers.parser.sanitize(html, {
elements: [ 'bat' ]
});
sanitized.should.equal('<foo><bar><baz></baz></bar></foo>');
});
it('should remove elements with callback', function() {
var html = '<foo><bar><baz><bat foo=bar>asdf</bat></baz></bar><bat><!-- comment --></bat></foo>';
var sanitized = helpers.parser.sanitize(html, {
elements: function(name) {
return name === 'bat';
}
});
sanitized.should.equal('<foo><bar><baz></baz></bar></foo>');
});
it('should remove self-closing elements', function() {
var html = '<foo><br />asdf</foo>';
var sanitized = helpers.parser.sanitize(html, {
elements: [ 'br' ]
});
sanitized.should.equal('<foo>asdf</foo>');
});
it('should remove element with attributes', function() {
var html = '<foo><bar baz="bat"></bar></foo>';
var sanitized = helpers.parser.sanitize(html, {
elements: [ 'bar' ]
});
sanitized.should.equal('<foo></foo>');
});
it('should remove script tag with whitespace', function() {
var html = '<p>foo<script ></script ></p>';
var sanitized = helpers.parser.sanitize(html, {
elements: [ 'script' ]
});
sanitized.should.equal('<p>foo</p>');
});
it('should remove script tag with attributes', function() {
var html = '<p>foo<script type="text/javascript">alert("foo");</script></p>';
var sanitized = helpers.parser.sanitize(html, {
elements: [ 'script' ]
});
sanitized.should.equal('<p>foo</p>');
});
it('should handle attributes with no value', function() {
var html = '<p novalue1 novalue2>foo</p>';
var sanitized = helpers.parser.sanitize(html, {
attributes: [ 'novalue1' ]
});
sanitized.should.equal('<p novalue2>foo</p>');
});
describe('self-closing tag', function() {
var selfClosingTags = {
meta: 1,
br: 1,
link: 1,
area: 1,
base: 1,
col: 1,
command: 1,
embed: 1,
hr: 1,
img: 1,
input: 1,
param: 1,
source: 1
};
for (var tag in selfClosingTags) {
it('"' + tag + '" without a closing "/>"', (function(tag) {
return function() {
var html = '<' + tag + '><p>foo</p>';
var sanitized = helpers.parser.sanitize(html, {
elements: [ tag ]
});
sanitized.should.equal('<p>foo</p>');
}
}(tag)));
}
});
it('should remove tags within tags', function() {
var html = '<foo><bar>lolz</bar><bat></bat></foo><baz>oh hai there</baz>';
var sanitized = helpers.parser.sanitize(html, {
elements: [ 'foo', 'bar' ]
});
sanitized.should.equal('<baz>oh hai there</baz>');
});
it('should sanitize based on attribute value', function() {
var html = '<foo id="abc"></foo><foo id="def"></foo>';
var sanitized = helpers.parser.sanitize(html, {
attributes: function(name, value, quote) {
return value === 'abc';
}
});
sanitized.should.equal('<foo></foo><foo id="def"></foo>');
});
});