forked from ericnorris/striptags
-
Notifications
You must be signed in to change notification settings - Fork 0
/
striptags-test.js
169 lines (124 loc) · 5.34 KB
/
striptags-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
'use strict';
/* global describe, it */
let assert = require('assert');
let fs = require('fs');
let vm = require('vm');
let striptags = require('../');
describe('striptags', function() {
describe('#module', function() {
let path = require.resolve('../');
let src = fs.readFileSync(path);
let script = new vm.Script(src);
it('should define a Node module', function() {
let module = { exports: {} };
script.runInNewContext({module});
assert.equal(module.exports.toString(), striptags.toString());
});
it('should define an AMD module', function() {
let module = null;
let define = function(module_factory) {
module = module_factory();
};
define.amd = true;
script.runInNewContext({define});
assert.equal(module.toString(), striptags.toString());
});
it('should define a browser global', function() {
let global = {};
script.runInNewContext(global);
assert.notEqual(global.striptags, null);
});
});
describe('with no optional parameters', function() {
it('should not strip invalid tags', function() {
let text = 'lorem ipsum < a> < div>';
assert.equal(striptags(text), text);
});
it('should remove simple HTML tags', function() {
let html = '<a href="">lorem <strong>ipsum</strong></a>',
text = 'lorem ipsum';
assert.equal(striptags(html), text);
});
it('should remove comments', function() {
let html = '<!-- lorem -- ipsum -- --> dolor sit amet',
text = ' dolor sit amet';
assert.equal(striptags(html), text);
});
it('should strip tags within comments', function() {
let html = '<!-- <strong>lorem ipsum</strong> --> dolor sit',
text = ' dolor sit';
assert.equal(striptags(html), text);
});
it('should not fail with nested quotes', function() {
let html = '<article attr="foo \'bar\'">lorem</article> ipsum',
text = 'lorem ipsum';
assert.equal(striptags(html), text);
});
});
describe('#allowed_tags', function() {
it('should parse a string', function() {
let html = '<strong>lorem ipsum</strong>',
allowed_tags = '<strong>';
assert.equal(striptags(html, allowed_tags), html);
});
it('should take an array', function() {
let html = '<strong>lorem <em>ipsum</em></strong>',
allowed_tags = ['strong', 'em'];
assert.equal(striptags(html, allowed_tags), html);
});
});
describe('with allowable_tags parameter', function() {
it('should leave attributes when allowing HTML', function() {
let html = '<a href="https://example.com">lorem ipsum</a>',
allowed_tags = '<a>';
assert.equal(striptags(html, allowed_tags), html);
});
it('should strip extra < within tags', function() {
let html = '<div<>>lorem ipsum</div>',
text = '<div>lorem ipsum</div>',
allowed_tags = '<div>';
assert.equal(striptags(html, allowed_tags), text);
});
it('should strip <> within quotes', function() {
let html = '<a href="<script>">lorem ipsum</a>',
text = '<a href="script">lorem ipsum</a>',
allowed_tags = '<a>';
assert.equal(striptags(html, allowed_tags), text);
});
it('should remove blacklisted tags', function() {
let html = '<p>a <i>text</i> with <a href="#">link</a></p>',
text = 'a <i>text</i> with <a href="#">link</a>',
tags = ['!p'];
assert.equal(striptags(html, tags), text);
});
});
describe('with tag_replacement parameter', function() {
it('should replace tags with that parameter', function() {
var html = 'Line One<br>Line Two',
allowed_tags = [],
tag_replacement = '\n',
text = 'Line One\nLine Two';
assert.equal(striptags(html, allowed_tags, tag_replacement), text);
});
});
describe('#streaming_mode', function() {
it('should strip streamed HTML', function() {
let striptags_stream = striptags.init_streaming_mode();
let part_one = striptags_stream('lorem ipsum <stro');
let part_two = striptags_stream('ng>dolor sit <');
let part_three = striptags_stream(' amet');
assert.equal(part_one, 'lorem ipsum ');
assert.equal(part_two, 'dolor sit ');
assert.equal(part_three, '< amet');
});
it('should work with allowable_tags', function() {
let striptags_stream = striptags.init_streaming_mode(['strong']);
let part_one = striptags_stream('lorem ipsum <stro');
let part_two = striptags_stream('ng>dolor sit <');
let part_three = striptags_stream(' amet');
assert.equal(part_one, 'lorem ipsum ');
assert.equal(part_two, '<strong>dolor sit ');
assert.equal(part_three, '< amet');
});
});
});