-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.js
242 lines (206 loc) · 7.23 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
const assert = require('assert');
const { Document } = require('basichtml');
global.document = new Document();
const { Weaver, escapeHTML, unescapeHTML } = require('./index');
const weaver = new Weaver();
suite('encode', () => {
test('bold char', () => {
const encodedText = weaver.encode('<b>a</b>');
assert.deepEqual(encodedText, {
text: 'a',
meta: [ ['b', 0, 1, null] ]
});
});
test('alert', () => {
const encodedText = weaver.encode(escapeHTML('<script>alert(1)</script>'));
assert.deepEqual(encodedText, {
text: '<script>alert(1)</script>',
meta: []
});
});
test('basic tags', () => {
const encodedText = weaver.encode('my <b>bold</b> text');
assert.deepEqual(encodedText, {
text: 'my bold text',
meta: [
['b', 3, 7, null]
]
});
});
test('trim the input', () => {
const encodedText = weaver.encode(' my padded text \n');
assert.deepEqual(encodedText,{
text: 'my padded text',
meta: [],
});
});
test('links', () => {
const encodedText = weaver.encode('<a href="yahoo.com" class="linkified">YAHOO</a>');
assert.deepEqual(encodedText, {
text: 'YAHOO',
meta: [
['a', 0, 5, [ ['href', 'yahoo.com'], ['class', 'linkified'] ] ]
]
});
});
test('newlines', () => {
const encodedText = weaver.encode('<a href="yahoo.com" class="linkified">YAHOO\nYAHOO</a>\nhey!');
assert.deepEqual(encodedText, {
text: 'YAHOO\nYAHOO\nhey!',
meta: [
['a', 0, 11, [ ['href', 'yahoo.com'], ['class', 'linkified'] ] ]
]
});
});
test('stacked tags', () => {
const encodedText = weaver.encode('she <i>told</i> him that he was the <b><i>worst!</i></b>');
assert.deepEqual(encodedText, {
text: 'she told him that he was the worst!',
meta: [
['i', 4, 8, null],
['b', 29, 35, null],
['i', 29, 35, null],
]
});
});
});
suite('decode', () => {
test('errors with bad inputs', () => {
const noMeta = () => weaver.decode({ text: 'test' });
assert.throws(noMeta, Error);
const noText = () => weaver.decode({ meta: [] });
assert.throws(noText, Error);
});
test('alert', () => {
const decodedText = weaver.decode({
text: '<script>alert(1)</script>',
meta: []
});
assert.equal(decodedText, escapeHTML('<script>alert(1)</script>'));
});
test('basic tags', () => {
const decodedText = weaver.decode({
text: 'my bold text',
meta: [
['b', 3, 7]
]
});
assert.equal(decodedText, 'my <b>bold</b> text');
});
test('newlines', () => {
const decodedText = weaver.decode({
text: 'she told him that\n he\n was the worst!',
meta: [
['i', 4, 8],
['b', 31, 37],
['i', 31, 37],
]
});
assert.equal(decodedText, 'she <i>told</i> him that<br/> he<br/> was the <b><i>worst!</i></b>');
});
test('stacked tags', () => {
const decodedText = weaver.decode({
text: 'she told him that he was the worst!',
meta: [
['i', 4, 8],
['b', 29, 35],
['i', 29, 35],
]
});
assert.equal(decodedText, 'she <i>told</i> him that he was the <b><i>worst!</i></b>');
});
});
suite('linkify', () => {
test('basic link', () => {
const html = weaver.decode({
text: 'yahoo.com',
meta: []
});
assert.equal(html, '<a href="http://yahoo.com" class="linkified">yahoo.com</a>');
});
test('link on top of style', () => {
const html = weaver.decode({
text: 'visit yahoo.com and gmx.net today!',
meta: [ ['b', 0, 5], ['i', 6, 15] ]
});
assert.equal(html, '<b>visit</b> <i><a href="http://yahoo.com" class="linkified">yahoo.com</a></i> and <a href="http://gmx.net" class="linkified">gmx.net</a> today!');
});
test('ignore links with partially overlapping tags', () => {
const overlappingAfter = weaver.decode(weaver.encode('ya<i>hoooo.com is great</i>'));
assert.equal(overlappingAfter, 'ya<i>hoooo.com is great</i>');
const overlappingBefore = weaver.decode(weaver.encode('<i>ya</i>hoooo.com is great'));
assert.equal(overlappingBefore, '<i>ya</i>hoooo.com is great');
const notOverlapping = weaver.decode(weaver.encode('<b><i>yahoooo.com</i></b> is great'));
assert.equal(notOverlapping, '<b><i><a href="http://yahoooo.com" class="linkified">yahoooo.com</a></i></b> is great');
});
test('linkify inside style', () => {
assert.equal(
weaver.decode(weaver.encode('<b>yahoo.com</b>')),
'<b><a href="http://yahoo.com" class="linkified">yahoo.com</a></b>'
);
});
test('round trip without adding extra links', () => {
const html = weaver.decode(weaver.encode(weaver.decode({
text: 'yahoo.com',
meta: []
})));
assert.equal(html, '<a href="http://yahoo.com" class="linkified">yahoo.com</a>');
});
test("round trip without adding extra links when href doesn't match content", () => {
const html = weaver.decode(weaver.encode('<a href="notyahoo.com" class="linkified">yahoo.com</a>'));
assert.equal(html, '<a href="notyahoo.com" class="linkified">yahoo.com</a>');
});
test("don't linkify when we are inside an a tag", () => {
const input = '<a href="notyahoo.com" class="linkified">A bunch of <i>stuff</i> yada<b>yahoo.com</b></a>';
const html = weaver.decode(weaver.encode(input));
assert.equal(html, input);
});
test('double round trip without adding extra links', () => {
const html = weaver.decode(weaver.encode(weaver.decode(weaver.encode(weaver.decode({
text: 'yahoo.com',
meta: []
})))));
assert.equal(html, '<a href="http://yahoo.com" class="linkified">yahoo.com</a>');
});
});
suite('encode-decode', () => {
test('round trip', () => {
const inputs = [
`<h1>Rich Text Format</h1><b>Richard Brodie</b>, <b>Charles Simonyi</b>, and <b>David Luebbert</b>, members of the <i>Microsoft Word</i> development team, developed the original RTF in the middle to late 1980s. Its syntax was influenced by the TeX typesetting language.`,
`<h1>Hey, you! <b><i>Get out of there!</i></b></h1>`
];
for (const input of inputs) {
assert.equal(input, weaver.decode(weaver.encode(input)));
}
});
test('links', () => {
const inputs = [
`<a href="http://google.com" class="linkified">link to google</a>`
];
for (const input of inputs) {
assert.equal(input, weaver.decode(weaver.encode(input)));
}
});
test('works with empty string', () => {
const html = weaver.decode(weaver.encode(''));
assert.equal(html, '');
});
test('newlines', () => {
const inputs = [
`<a href="http://google.com" class="linkified">link to google<br/>another</a>`
];
for (const input of inputs) {
assert.equal(input, weaver.decode(weaver.encode(input)));
}
});
test('no doubling up', () => {
const input = 'visit <a href="http://yahoo.com">yahoo.com</a> today!';
const decodedHtml = weaver.decode(weaver.encode(input));
assert.equal(decodedHtml, input);
});
test('no doubling doubling up', () => {
const input = 'visit <a href="http://yahoo.com">yahoo.com</a> today!';
const decodedHtml = weaver.decode(weaver.encode(weaver.decode(weaver.encode(input))));
assert.equal(decodedHtml, input);
});
});