forked from meteor/meteor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokenize_tests.js
344 lines (301 loc) · 12.4 KB
/
tokenize_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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
var Scanner = HTMLTools.Scanner;
var getComment = HTMLTools.Parse.getComment;
var getDoctype = HTMLTools.Parse.getDoctype;
var getHTMLToken = HTMLTools.Parse.getHTMLToken;
// "tokenize" is not really a great operation for real use, because
// it ignores the special content rules for tags like "style" and
// "script".
var tokenize = function (input) {
var scanner = new Scanner(input);
var tokens = [];
while (! scanner.isEOF()) {
var token = getHTMLToken(scanner);
if (token)
tokens.push(token);
}
return tokens;
};
Tinytest.add("html-tools - comments", function (test) {
var succeed = function (input, content) {
var scanner = new Scanner(input);
var result = getComment(scanner);
test.isTrue(result);
test.equal(scanner.pos, content.length + 7);
test.equal(result, {
t: 'Comment',
v: content
});
};
var ignore = function (input) {
var scanner = new Scanner(input);
var result = getComment(scanner);;
test.isFalse(result);
test.equal(scanner.pos, 0);
};
var fatal = function (input, messageContains) {
var scanner = new Scanner(input);
var error;
try {
getComment(scanner);
} catch (e) {
error = e;
}
test.isTrue(error);
if (error)
test.isTrue(messageContains && error.message.indexOf(messageContains) >= 0, error.message);
};
test.equal(getComment(new Scanner("<!-- hello -->")),
{ t: 'Comment', v: ' hello ' });
ignore("<!DOCTYPE>");
ignore("<!-a");
ignore("<--");
ignore("<!");
ignore("abc");
ignore("<a");
fatal('<!--', 'Unclosed');
fatal('<!---', 'Unclosed');
fatal('<!----', 'Unclosed');
fatal('<!-- -', 'Unclosed');
fatal('<!-- --', 'Unclosed');
fatal('<!-- -- abcd', 'Unclosed');
fatal('<!-- ->', 'Unclosed');
fatal('<!-- a--b -->', 'cannot contain');
fatal('<!--x--->', 'must end at first');
fatal('<!-- a\u0000b -->', 'cannot contain');
fatal('<!--\u0000 x-->', 'cannot contain');
succeed('<!---->', '');
succeed('<!---x-->', '-x');
succeed('<!--x-->', 'x');
succeed('<!-- hello - - world -->', ' hello - - world ');
});
Tinytest.add("html-tools - doctype", function (test) {
var succeed = function (input, expectedProps) {
var scanner = new Scanner(input);
var result = getDoctype(scanner);
test.isTrue(result);
test.equal(scanner.pos, result.v.length);
test.equal(input.slice(0, result.v.length), result.v);
var actualProps = _.extend({}, result);
delete actualProps.t;
delete actualProps.v;
test.equal(actualProps, expectedProps);
};
var fatal = function (input, messageContains) {
var scanner = new Scanner(input);
var error;
try {
getDoctype(scanner);
} catch (e) {
error = e;
}
test.isTrue(error);
if (messageContains)
test.isTrue(error.message.indexOf(messageContains) >= 0, error.message);
};
test.equal(getDoctype(new Scanner("<!DOCTYPE html>x")),
{ t: 'Doctype',
v: '<!DOCTYPE html>',
name: 'html' });
test.equal(getDoctype(new Scanner("<!DOCTYPE html SYSTEM 'about:legacy-compat'>x")),
{ t: 'Doctype',
v: "<!DOCTYPE html SYSTEM 'about:legacy-compat'>",
name: 'html',
systemId: 'about:legacy-compat' });
test.equal(getDoctype(new Scanner("<!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.0//EN'>x")),
{ t: 'Doctype',
v: "<!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.0//EN'>",
name: 'html',
publicId: '-//W3C//DTD HTML 4.0//EN' });
test.equal(getDoctype(new Scanner("<!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.0//EN' 'http://www.w3.org/TR/html4/strict.dtd'>x")),
{ t: 'Doctype',
v: "<!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.0//EN' 'http://www.w3.org/TR/html4/strict.dtd'>",
name: 'html',
publicId: '-//W3C//DTD HTML 4.0//EN',
systemId: 'http://www.w3.org/TR/html4/strict.dtd' });
succeed('<!DOCTYPE html>', {name: 'html'});
succeed('<!DOCTYPE htML>', {name: 'html'});
succeed('<!DOCTYPE HTML>', {name: 'html'});
succeed('<!doctype html>', {name: 'html'});
succeed('<!doctYPE html>', {name: 'html'});
succeed('<!DOCTYPE html \u000c>', {name: 'html'});
fatal('<!DOCTYPE', 'Expected space');
fatal('<!DOCTYPE ', 'Malformed DOCTYPE');
fatal('<!DOCTYPE ', 'Malformed DOCTYPE');
fatal('<!DOCTYPE>', 'Expected space');
fatal('<!DOCTYPE >', 'Malformed DOCTYPE');
fatal('<!DOCTYPE\u0000', 'Expected space');
fatal('<!DOCTYPE \u0000', 'Malformed DOCTYPE');
fatal('<!DOCTYPE html\u0000>', 'Malformed DOCTYPE');
fatal('<!DOCTYPE html', 'Malformed DOCTYPE');
succeed('<!DOCTYPE html SYSTEM "about:legacy-compat">', {name: 'html', systemId: 'about:legacy-compat'});
succeed('<!doctype HTML system "about:legacy-compat">', {name: 'html', systemId: 'about:legacy-compat'});
succeed("<!DOCTYPE html SYSTEM 'about:legacy-compat'>", {name: 'html', systemId: 'about:legacy-compat'});
succeed("<!dOcTyPe HtMl sYsTeM 'about:legacy-compat'>", {name: 'html', systemId: 'about:legacy-compat'});
succeed('<!DOCTYPE html\tSYSTEM\t"about:legacy-compat" \t>', {name: 'html', systemId: 'about:legacy-compat'});
fatal('<!DOCTYPE html SYSTE "about:legacy-compat">', 'Expected PUBLIC or SYSTEM');
fatal('<!DOCTYPE html SYSTE', 'Expected PUBLIC or SYSTEM');
fatal('<!DOCTYPE html SYSTEM"about:legacy-compat">', 'Expected space');
fatal('<!DOCTYPE html SYSTEM');
fatal('<!DOCTYPE html SYSTEM ');
fatal('<!DOCTYPE html SYSTEM>');
fatal('<!DOCTYPE html SYSTEM >');
fatal('<!DOCTYPE html SYSTEM ">">');
fatal('<!DOCTYPE html SYSTEM "\u0000about:legacy-compat">');
fatal('<!DOCTYPE html SYSTEM "about:legacy-compat\u0000">');
fatal('<!DOCTYPE html SYSTEM "');
fatal('<!DOCTYPE html SYSTEM "">');
fatal('<!DOCTYPE html SYSTEM \'');
fatal('<!DOCTYPE html SYSTEM\'a\'>');
fatal('<!DOCTYPE html SYSTEM about:legacy-compat>');
succeed('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0//EN">',
{ name: 'html',
publicId: '-//W3C//DTD HTML 4.0//EN'});
succeed('<!DOCTYPE html PUBLIC \'-//W3C//DTD HTML 4.0//EN\'>',
{ name: 'html',
publicId: '-//W3C//DTD HTML 4.0//EN'});
succeed('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">',
{ name: 'html',
publicId: '-//W3C//DTD HTML 4.0//EN',
systemId: 'http://www.w3.org/TR/REC-html40/strict.dtd'});
succeed('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0//EN" \'http://www.w3.org/TR/REC-html40/strict.dtd\'>',
{ name: 'html',
publicId: '-//W3C//DTD HTML 4.0//EN',
systemId: 'http://www.w3.org/TR/REC-html40/strict.dtd'});
succeed('<!DOCTYPE html public \'-//W3C//DTD HTML 4.0//EN\' \'http://www.w3.org/TR/REC-html40/strict.dtd\'>',
{ name: 'html',
publicId: '-//W3C//DTD HTML 4.0//EN',
systemId: 'http://www.w3.org/TR/REC-html40/strict.dtd'});
succeed('<!DOCTYPE html public \'-//W3C//DTD HTML 4.0//EN\'\t\'http://www.w3.org/TR/REC-html40/strict.dtd\' >',
{ name: 'html',
publicId: '-//W3C//DTD HTML 4.0//EN',
systemId: 'http://www.w3.org/TR/REC-html40/strict.dtd'});
fatal('<!DOCTYPE html public \'-//W3C//DTD HTML 4.0//EN\' \'http://www.w3.org/TR/REC-html40/strict.dtd\'');
fatal('<!DOCTYPE html public \'-//W3C//DTD HTML 4.0//EN\' \'http://www.w3.org/TR/REC-html40/strict.dtd\'');
fatal('<!DOCTYPE html public \'-//W3C//DTD HTML 4.0//EN\' \'http://www.w3.org/TR/REC-html40/strict.dtd');
fatal('<!DOCTYPE html public \'-//W3C//DTD HTML 4.0//EN\' \'');
fatal('<!DOCTYPE html public \'-//W3C//DTD HTML 4.0//EN\' ');
fatal('<!DOCTYPE html public \'- ');
fatal('<!DOCTYPE html public>');
fatal('<!DOCTYPE html public "-//W3C//DTD HTML 4.0//EN""http://www.w3.org/TR/REC-html40/strict.dtd">');
});
Tinytest.add("html-tools - tokenize", function (test) {
var fatal = function (input, messageContains) {
var error;
try {
tokenize(input);
} catch (e) {
error = e;
}
test.isTrue(error);
if (messageContains)
test.isTrue(error.message.indexOf(messageContains) >= 0, error.message);
};
test.equal(tokenize(''), []);
test.equal(tokenize('abc'), [{t: 'Chars', v: 'abc'}]);
test.equal(tokenize('&'), [{t: 'Chars', v: '&'}]);
test.equal(tokenize('&'), [{t: 'CharRef', v: '&', cp: [38]}]);
test.equal(tokenize('ok fine'),
[{t: 'Chars', v: 'ok'},
{t: 'CharRef', v: ' ', cp: [32]},
{t: 'Chars', v: 'fine'}]);
test.equal(tokenize('a<!--b-->c'),
[{t: 'Chars',
v: 'a'},
{t: 'Comment',
v: 'b'},
{t: 'Chars',
v: 'c'}]);
test.equal(tokenize('<a>'), [{t: 'Tag', n: 'a'}]);
fatal('<');
fatal('<x');
fatal('<x ');
fatal('<x a');
fatal('<x a ');
fatal('<x a =');
fatal('<x a = ');
fatal('<x a = b');
fatal('<x a = "b');
fatal('<x a = \'b');
fatal('<x a = b ');
fatal('<x a = b /');
test.equal(tokenize('<x a = b />'),
[{t: 'Tag', n: 'x',
attrs: { a: [{t: 'Chars', v: 'b'}] },
isSelfClosing: true}]);
test.equal(tokenize('<a>X</a>'),
[{t: 'Tag', n: 'a'},
{t: 'Chars', v: 'X'},
{t: 'Tag', n: 'a', isEnd: true}]);
fatal('<x a a>'); // duplicate attribute value
test.equal(tokenize('<a b >'),
[{t: 'Tag', n: 'a', attrs: { b: [] }}]);
fatal('< a>');
fatal('< /a>');
fatal('</ a>');
// Slash does not end an unquoted attribute, interestingly
test.equal(tokenize('<a b=/>'),
[{t: 'Tag', n: 'a', attrs: { b: [{t: 'Chars', v: '/'}] }}]);
test.equal(tokenize('<a b="c" d=e f=\'g\' h \t>'),
[{t: 'Tag', n: 'a',
attrs: { b: [{t: 'Chars', v: 'c'}],
d: [{t: 'Chars', v: 'e'}],
f: [{t: 'Chars', v: 'g'}],
h: [] }}]);
fatal('</a b="c" d=e f=\'g\' h \t\u0000>');
fatal('</a b="c" d=ef=\'g\' h \t>');
fatal('</a b="c"d=e f=\'g\' h \t>');
test.equal(tokenize('<a/>'), [{t: 'Tag', n: 'a', isSelfClosing: true}]);
fatal('<a/ >');
fatal('<a/b>');
fatal('<a b=c`>');
fatal('<a b=c<>');
test.equal(tokenize('<a# b0="c@" d1=e2 f#=\'g \' h \t>'),
[{t: 'Tag', n: 'a#',
attrs: { b0: [{t: 'Chars', v: 'c@'}],
d1: [{t: 'Chars', v: 'e2'}],
'f#': [{t: 'Chars', v: 'g '}],
h: [] }}]);
test.equal(tokenize('<div class=""></div>'),
[{t: 'Tag', n: 'div', attrs: { 'class': [] }},
{t: 'Tag', n: 'div', isEnd: true}]);
test.equal(tokenize('<div class="&">'),
[{t: 'Tag', n: 'div', attrs: { 'class': [{t: 'Chars', v: '&'}] }}]);
test.equal(tokenize('<div class=&>'),
[{t: 'Tag', n: 'div', attrs: { 'class': [{t: 'Chars', v: '&'}] }}]);
test.equal(tokenize('<div class=&>'),
[{t: 'Tag', n: 'div', attrs: { 'class': [{t: 'CharRef', v: '&', cp: [38]}] }}]);
test.equal(tokenize('<div class=aa&𝕫∾̳&bb>'),
[{t: 'Tag', n: 'div', attrs: { 'class': [
{t: 'Chars', v: 'aa&'},
{t: 'CharRef', v: '𝕫', cp: [120171]},
{t: 'CharRef', v: '∾̳', cp: [8766, 819]},
{t: 'Chars', v: '&bb'}
] }}]);
test.equal(tokenize('<div class="aa &𝕫∾̳& bb">'),
[{t: 'Tag', n: 'div', attrs: { 'class': [
{t: 'Chars', v: 'aa &'},
{t: 'CharRef', v: '𝕫', cp: [120171]},
{t: 'CharRef', v: '∾̳', cp: [8766, 819]},
{t: 'Chars', v: '& bb'}
] }}]);
test.equal(tokenize('<a b="\'`<>&">'),
[{t: 'Tag', n: 'a', attrs: { b: [{t: 'Chars', v: '\'`<>&'}] }}]);
test.equal(tokenize('<a b=\'"`<>&\'>'),
[{t: 'Tag', n: 'a', attrs: { b: [{t: 'Chars', v: '"`<>&'}] }}]);
fatal('>');
fatal('>c');
test.equal(tokenize('<a b=>c>'),
[{t: 'Tag', n: 'a', attrs: { b: [{t: 'Chars', v: '>c' }] }}]);
test.equal(tokenize('<a b=">c">'),
[{t: 'Tag', n: 'a', attrs: { b: [{t: 'Chars', v: '>c' }] }}]);
fatal('<a b=>>');
fatal('<a b=">">');
fatal('<a b=">=">');
fatal('<!');
fatal('<!x>');
fatal('<a{{b}}>');
fatal('<{{a}}>');
fatal('</a b=c>'); // end tag can't have attributes
fatal('</a/>'); // end tag can't be self-closing
fatal('</a />');
});