forked from inikulin/parse5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokenizer-location-info.test.ts
188 lines (171 loc) · 5.99 KB
/
tokenizer-location-info.test.ts
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
import * as assert from 'node:assert';
import { Tokenizer, TokenizerMode, TokenHandler } from './index.js';
import { Location, EOFToken, CharacterToken, DoctypeToken, TagToken, CommentToken } from '../common/token.js';
import { getSubstringByLineCol, normalizeNewLine } from 'parse5-test-utils/utils/common.js';
interface LocationInfoTestCase {
initialMode: typeof TokenizerMode[keyof typeof TokenizerMode];
lastStartTagName: string;
htmlChunks: string[];
}
/** Receives events and immediately compares them against the expected values. */
class LocationInfoHandler implements TokenHandler {
public sawEof = false;
/** The index of the last html chunk. */
private idx = 0;
/** All of the lines in the input. */
private lines: string[];
constructor(private testCase: LocationInfoTestCase, private html: string) {
this.lines = html.split(/\r?\n/g);
}
private validateLocation(location: Location | null): void {
assert.ok(location);
//Offsets
const actual = this.html.substring(location.startOffset, location.endOffset);
const chunk = this.testCase.htmlChunks[this.idx];
assert.strictEqual(actual, chunk);
//Line/col
const line = getSubstringByLineCol(this.lines, location);
const expected = normalizeNewLine(chunk);
assert.strictEqual(line, expected);
this.idx += 1;
}
onComment({ location }: CommentToken): void {
this.validateLocation(location);
}
onDoctype({ location }: DoctypeToken): void {
this.validateLocation(location);
}
onStartTag({ location }: TagToken): void {
this.validateLocation(location);
}
onEndTag({ location }: TagToken): void {
this.validateLocation(location);
}
onCharacter({ location }: CharacterToken): void {
this.validateLocation(location);
}
onNullCharacter({ location }: CharacterToken): void {
this.validateLocation(location);
}
onWhitespaceCharacter({ location }: CharacterToken): void {
this.validateLocation(location);
}
onEof({ location }: EOFToken): void {
assert.ok(location);
assert.strictEqual(location.endOffset, location.startOffset);
assert.strictEqual(location.endOffset, this.html.length);
assert.strictEqual(this.idx, this.testCase.htmlChunks.length);
this.sawEof = true;
}
}
it('Location Info (Tokenizer)', () => {
const testCases = [
{
initialMode: TokenizerMode.DATA,
lastStartTagName: '',
htmlChunks: [
'\r\n',
'<!DOCTYPE html>',
'\n',
'<!-- Test -->',
'\n',
'<head>',
'\n ',
'<meta charset="utf-8">',
'<title>',
' ',
'node.js',
'\u0000',
'</title>',
'\n',
'</head>',
'\n',
'<body id="front">',
'\n',
'<div id="intro">',
'\n ',
'<p\n>',
'\n ',
'Node.js',
' ',
'is',
' ',
'a',
'\n ',
'platform',
' ',
'built',
' ',
'on',
'\n ',
'<a href="http://code.google.com/p/v8/">',
'\n ',
"Chrome's",
' ',
'JavaScript',
' ',
'runtime',
'\n ',
'</a>',
'\n',
'</div>',
'</body>',
],
},
{
initialMode: TokenizerMode.RCDATA,
lastStartTagName: 'title',
htmlChunks: ['<div>Test', ' \n ', 'hey', ' ', 'ya!', '</title>', '<!--Yo-->'],
},
{
initialMode: TokenizerMode.RAWTEXT,
lastStartTagName: 'style',
htmlChunks: ['.header{', ' \n ', 'color:red;', '\n', '}', '</style>', 'Some', ' ', 'text'],
},
{
initialMode: TokenizerMode.SCRIPT_DATA,
lastStartTagName: 'script',
htmlChunks: ['var', ' ', 'a=c', ' ', '-', ' ', 'd;', '\n', 'a<--d;', '</script>', '<div>'],
},
{
initialMode: TokenizerMode.PLAINTEXT,
lastStartTagName: 'plaintext',
htmlChunks: ['Text', ' \n', 'Test</plaintext><div>'],
},
{
initialMode: TokenizerMode.DATA,
lastStartTagName: '',
htmlChunks: [
'\n',
'<!-- regular comment -->',
'<! bogus comment >',
'<? another bogus comment >',
'</!yet another bogus comment>',
'<![CDATA[ cdata as a bogus comment >',
],
},
{
initialMode: TokenizerMode.DATA,
lastStartTagName: '',
inForeignNode: true,
htmlChunks: ['<a>', '<![CDATA[ ', 'CDATA', ' ]]>', '<test>', ' <![CDATA[ ]]>\n'],
},
];
for (const testCase of testCases) {
const html = testCase.htmlChunks.join('');
const handler = new LocationInfoHandler(testCase, html);
const tokenizer = new Tokenizer({ sourceCodeLocationInfo: true }, handler);
const lastChunkIdx = testCase.htmlChunks.length - 1;
for (let i = 0; i < testCase.htmlChunks.length; i++) {
tokenizer.write(testCase.htmlChunks[i], i === lastChunkIdx);
}
// NOTE: set small waterline for testing purposes
tokenizer.preprocessor.bufferWaterline = 8;
tokenizer.state = testCase.initialMode;
tokenizer.lastStartTagName = testCase.lastStartTagName;
tokenizer.inForeignNode = !!testCase.inForeignNode;
while (!handler.sawEof) {
tokenizer.getNextToken();
}
}
});