This repository has been archived by the owner on Mar 24, 2022. It is now read-only.
forked from inikulin/parse5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokenizer-location-info.test.ts
139 lines (127 loc) · 4.41 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
import * as assert from 'node:assert';
import { Tokenizer, TokenizerMode } from './index.js';
import { TokenType } from '../common/token.js';
import { getSubstringByLineCol, normalizeNewLine } from 'parse5-test-utils/utils/common.js';
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: '',
allowCDATA: true,
htmlChunks: ['<a>', '<![CDATA[ ', 'CDATA', ' ]]>', '<test>', ' <![CDATA[ ]]>\n'],
},
];
for (const testCase of testCases) {
const html = testCase.htmlChunks.join('');
const lines = html.split(/\r?\n/g);
const tokenizer = new Tokenizer({ sourceCodeLocationInfo: true });
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.allowCDATA = !!testCase.allowCDATA;
for (let token = tokenizer.getNextToken(), j = 0; token.type !== TokenType.EOF; ) {
if (token.type === TokenType.HIBERNATION) {
continue;
}
assert.ok(token.location);
//Offsets
let actual = html.substring(token.location.startOffset, token.location.endOffset);
assert.strictEqual(actual, testCase.htmlChunks[j]);
//Line/col
actual = getSubstringByLineCol(lines, token.location);
const expected = normalizeNewLine(testCase.htmlChunks[j]);
assert.strictEqual(actual, expected);
token = tokenizer.getNextToken();
j++;
}
}
});