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 | 1x
1x
1x
1x
1x
1x
1x
1x
1x
34x
34x
34x
167x
34x
34x
41x
41x
18x
18x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
2x
2x
2x
1x
1x
1x
1x
1x
1x
1x
1x
1x
3x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
| import { Config } from '../config';
import { DOMTree } from '../dom';
import { EventCallback } from '../event';
import { InvalidTokenError } from '../lexer';
import { Parser } from './parser';
import HtmlValidate from '../htmlvalidate';
describe('parser', function(){
const chai = require('chai');
const expect = chai.expect;
let events: Array<any>;
let parser: Parser;
beforeEach(function(){
events = [];
parser = new Parser(Config.empty());
parser.on('*', function(event, data){
switch (event){
case 'tag:open':
events.push({
event,
target: data.target ? data.target.tagName : undefined,
});
break;
case 'tag:close':
events.push({
event,
target: data.target ? data.target.tagName : undefined,
previous: data.previous ? data.previous.tagName : undefined,
});
break;
case 'attr':
events.push({
event,
key: data.key,
value: data.value,
});
break;
}
});
});
describe('should parse elements', function(){
it('simple element', function(){
parser.parseHtml('<div></div>');
expect(events.shift()).to.deep.equal({event: 'tag:open', target: 'div'});
expect(events.shift()).to.deep.equal({event: 'tag:close', target: 'div', previous: 'div'});
expect(events.shift()).to.be.undefined;
});
it('with numbers', function(){
parser.parseHtml('<h1></h1>');
expect(events.shift()).to.deep.equal({event: 'tag:open', target: 'h1'});
expect(events.shift()).to.deep.equal({event: 'tag:close', target: 'h1', previous: 'h1'});
expect(events.shift()).to.be.undefined;
});
it('with dashes', function(){
parser.parseHtml('<foo-bar></foo-bar>');
expect(events.shift()).to.deep.equal({event: 'tag:open', target: 'foo-bar'});
expect(events.shift()).to.deep.equal({event: 'tag:close', target: 'foo-bar', previous: 'foo-bar'});
expect(events.shift()).to.be.undefined;
});
it('elements closed on wrong order', function(){
parser.parseHtml('<div><label></div></label>');
expect(events.shift()).to.deep.equal({event: 'tag:open', target: 'div'});
expect(events.shift()).to.deep.equal({event: 'tag:open', target: 'label'});
expect(events.shift()).to.deep.equal({event: 'tag:close', target: 'div', previous: 'label'});
expect(events.shift()).to.deep.equal({event: 'tag:close', target: 'label', previous: 'div'});
expect(events.shift()).to.be.undefined;
});
it('self-closing elements', function(){
parser.parseHtml('<input/>');
expect(events.shift()).to.deep.equal({event: 'tag:open', target: 'input'});
expect(events.shift()).to.deep.equal({event: 'tag:close', target: 'input', previous: 'input'});
expect(events.shift()).to.be.undefined;
});
it('void elements', function(){
parser.parseHtml('<input>');
expect(events.shift()).to.deep.equal({event: 'tag:open', target: 'input'});
expect(events.shift()).to.deep.equal({event: 'tag:close', target: 'input', previous: 'input'});
expect(events.shift()).to.be.undefined;
});
it('with text node', function(){
parser.parseHtml('<p>Lorem ipsum</p>');
});
it('with trailing text', function(){
parser.parseHtml('<p>Lorem ipsum</p>\n');
});
it('unclosed', function(){
parser.parseHtml('<p>');
});
it('unopened', function(){
parser.parseHtml('</p>');
});
it('multiple unopened', function(){
/* mostly for regression testing: root element should never be
* popped from node stack. */
parser.parseHtml('</p></p></p></p></p></p>');
});
it('with only text', function(){
parser.parseHtml('Lorem ipsum');
});
it('with newlines', function(){
parser.parseHtml('<div\nfoo="bar"></div>');
expect(events.shift()).to.deep.equal({event: 'tag:open', target: 'div'});
expect(events.shift()).to.deep.equal({event: 'attr', key: 'foo', value: 'bar'});
expect(events.shift()).to.deep.equal({event: 'tag:close', target: 'div', previous: 'div'});
expect(events.shift()).to.be.undefined;
});
it('with newline after attribute', function(){
parser.parseHtml('<div foo="bar"\nspam="ham"></div>');
expect(events.shift()).to.deep.equal({event: 'tag:open', target: 'div'});
expect(events.shift()).to.deep.equal({event: 'attr', key: 'foo', value: 'bar'});
expect(events.shift()).to.deep.equal({event: 'attr', key: 'spam', value: 'ham'});
expect(events.shift()).to.deep.equal({event: 'tag:close', target: 'div', previous: 'div'});
expect(events.shift()).to.be.undefined;
});
it('with xml namespaces', function(){
parser.parseHtml('<foo:div></foo:div>');
expect(events.shift()).to.deep.equal({event: 'tag:open', target: 'foo:div'});
expect(events.shift()).to.deep.equal({event: 'tag:close', target: 'foo:div', previous: 'foo:div'});
expect(events.shift()).to.be.undefined;
});
});
describe('should fail on', function(){
it('start tag with missing ">"', function(){
expect(() => {
parser.parseHtml('<p\n<p>foo</p></p>');
}).to.throw(InvalidTokenError);
});
});
describe('should parse attributes', function(){
it('without quotes', function(){
parser.parseHtml('<div foo=bar></div>');
expect(events.shift()).to.deep.equal({event: 'tag:open', target: 'div'});
expect(events.shift()).to.deep.equal({event: 'attr', key: 'foo', value: 'bar'});
expect(events.shift()).to.deep.equal({event: 'tag:close', target: 'div', previous: 'div'});
expect(events.shift()).to.be.undefined;
});
it('with single quotes', function(){
parser.parseHtml('<div foo=\'bar\'></div>');
expect(events.shift()).to.deep.equal({event: 'tag:open', target: 'div'});
expect(events.shift()).to.deep.equal({event: 'attr', key: 'foo', value: 'bar'});
expect(events.shift()).to.deep.equal({event: 'tag:close', target: 'div', previous: 'div'});
expect(events.shift()).to.be.undefined;
});
it('with double quote', function(){
parser.parseHtml('<div foo="bar"></div>');
expect(events.shift()).to.deep.equal({event: 'tag:open', target: 'div'});
expect(events.shift()).to.deep.equal({event: 'attr', key: 'foo', value: 'bar'});
expect(events.shift()).to.deep.equal({event: 'tag:close', target: 'div', previous: 'div'});
expect(events.shift()).to.be.undefined;
});
it('with nested quotes', function(){
parser.parseHtml('<div foo=\'"foo"\' bar="\'foo\'"></div>');
expect(events.shift()).to.deep.equal({event: 'tag:open', target: 'div'});
expect(events.shift()).to.deep.equal({event: 'attr', key: 'foo', value: '"foo"'});
expect(events.shift()).to.deep.equal({event: 'attr', key: 'bar', value: "'foo'"});
expect(events.shift()).to.deep.equal({event: 'tag:close', target: 'div', previous: 'div'});
expect(events.shift()).to.be.undefined;
});
it('without value', function(){
parser.parseHtml('<div foo></div>');
expect(events.shift()).to.deep.equal({event: 'tag:open', target: 'div'});
expect(events.shift()).to.deep.equal({event: 'attr', key: 'foo', value: undefined});
expect(events.shift()).to.deep.equal({event: 'tag:close', target: 'div', previous: 'div'});
expect(events.shift()).to.be.undefined;
});
it('with empty value', function(){
parser.parseHtml('<div foo="" bar=\'\'></div>');
expect(events.shift()).to.deep.equal({event: 'tag:open', target: 'div'});
expect(events.shift()).to.deep.equal({event: 'attr', key: 'foo', value: ''});
expect(events.shift()).to.deep.equal({event: 'attr', key: 'bar', value: ''});
expect(events.shift()).to.deep.equal({event: 'tag:close', target: 'div', previous: 'div'});
expect(events.shift()).to.be.undefined;
});
it('with dashes', function(){
parser.parseHtml('<div foo-bar-baz></div>');
expect(events.shift()).to.deep.equal({event: 'tag:open', target: 'div'});
expect(events.shift()).to.deep.equal({event: 'attr', key: 'foo-bar-baz', value: undefined});
expect(events.shift()).to.deep.equal({event: 'tag:close', target: 'div', previous: 'div'});
expect(events.shift()).to.be.undefined;
});
it('with spaces inside', function(){
parser.parseHtml('<div class="foo bar baz"></div>');
expect(events.shift()).to.deep.equal({event: 'tag:open', target: 'div'});
expect(events.shift()).to.deep.equal({event: 'attr', key: 'class', value: 'foo bar baz'});
expect(events.shift()).to.deep.equal({event: 'tag:close', target: 'div', previous: 'div'});
expect(events.shift()).to.be.undefined;
});
it('with uncommon characters', function(){
parser.parseHtml('<div a2?()!="foo"></div>');
expect(events.shift()).to.deep.equal({event: 'tag:open', target: 'div'});
expect(events.shift()).to.deep.equal({event: 'attr', key: 'a2?()!', value: 'foo'});
expect(events.shift()).to.deep.equal({event: 'tag:close', target: 'div', previous: 'div'});
expect(events.shift()).to.be.undefined;
});
it('with multiple attributes', function(){
parser.parseHtml('<div foo="bar" spam="ham"></div>');
expect(events.shift()).to.deep.equal({event: 'tag:open', target: 'div'});
expect(events.shift()).to.deep.equal({event: 'attr', key: 'foo', value: 'bar'});
expect(events.shift()).to.deep.equal({event: 'attr', key: 'spam', value: 'ham'});
expect(events.shift()).to.deep.equal({event: 'tag:close', target: 'div', previous: 'div'});
expect(events.shift()).to.be.undefined;
});
it('on self-closing elements', function(){
parser.parseHtml('<input type="text"/>');
expect(events.shift()).to.deep.equal({event: 'tag:open', target: 'input'});
expect(events.shift()).to.deep.equal({event: 'attr', key: 'type', value: 'text'});
expect(events.shift()).to.deep.equal({event: 'tag:close', target: 'input', previous: 'input'});
expect(events.shift()).to.be.undefined;
});
it('with xml namespaces', function(){
parser.parseHtml('<div foo:bar="baz"></div>');
expect(events.shift()).to.deep.equal({event: 'tag:open', target: 'div'});
expect(events.shift()).to.deep.equal({event: 'attr', key: 'foo:bar', value: 'baz'});
expect(events.shift()).to.deep.equal({event: 'tag:close', target: 'div', previous: 'div'});
expect(events.shift()).to.be.undefined;
});
});
describe('should handle optional end tags', function(){
it('<li>', function(){
parser.parseHtml(`
<ul>
<li>explicit</li>
<li>implicit
<li><strong>nested</strong>
<li><input>
</ul>`);
expect(events.shift()).to.deep.equal({event: 'tag:open', target: 'ul'});
expect(events.shift(), "explicit").to.deep.equal({event: 'tag:open', target: 'li'});
expect(events.shift(), "explicit").to.deep.equal({event: 'tag:close', target: 'li', previous: 'li'});
expect(events.shift(), "implicit").to.deep.equal({event: 'tag:open', target: 'li'});
expect(events.shift(), "implicit").to.deep.equal({event: 'tag:close', target: 'li', previous: 'li'});
expect(events.shift(), "nested").to.deep.equal({event: 'tag:open', target: 'li'});
expect(events.shift(), "nested").to.deep.equal({event: 'tag:open', target: 'strong'});
expect(events.shift(), "nested").to.deep.equal({event: 'tag:close', target: 'strong', previous: 'strong'});
expect(events.shift(), "nested").to.deep.equal({event: 'tag:close', target: 'li', previous: 'li'});
expect(events.shift(), "void").to.deep.equal({event: 'tag:open', target: 'li'});
expect(events.shift(), "void").to.deep.equal({event: 'tag:open', target: 'input'});
expect(events.shift(), "void").to.deep.equal({event: 'tag:close', target: 'input', previous: 'input'});
expect(events.shift(), "void").to.deep.equal({event: 'tag:close', target: 'ul', previous: 'li'});
expect(events.shift()).to.deep.equal({event: 'tag:close', target: 'ul', previous: 'ul'});
expect(events.shift()).to.be.undefined;
});
});
describe('dom:ready', function(){
let callback: EventCallback;
let document: DOMTree;
beforeEach(function(){
callback = chai.spy(function(event: string, data: any){
document = data.document;
});
parser.on('dom:ready', callback);
});
it('should trigger when parsing is complete', function(){
parser.parseHtml('<div></div>');
expect(callback).to.have.been.called.once();
});
it('should contain DOMTree as argument', function(){
parser.parseHtml('<div></div>');
expect(document).to.be.instanceof(DOMTree);
expect(document.root.children).to.have.lengthOf(1);
});
});
describe('regressiontesting', function(){
let htmlvalidate: HtmlValidate;
beforeEach(function(){
htmlvalidate = new HtmlValidate({
extends: ['htmlvalidate:recommended'],
});
});
it('multiline', function(){
this.timeout(10000); /* seems to fail a lot on CI runner, need to figure out why */
const report = htmlvalidate.validateFile('./test-files/parser/multiline.html');
expect(report.valid, "linting should report success").to.be.true;
});
it('xi:include', function(){
const report = htmlvalidate.validateFile('./test-files/parser/xi-include.html');
expect(report.valid, "linting should report success").to.be.true;
});
it('cdata', function(){
const report = htmlvalidate.validateFile('./test-files/parser/cdata.html');
expect(report.valid, "linting should report success").to.be.true;
});
});
});
|