-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathlexer.ts
179 lines (170 loc) · 5.02 KB
/
lexer.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
let source: string, i: number;
export interface ParsedTag {
tagName: string;
start: number;
end: number;
attributes: ParsedAttribute[];
innerStart: number;
innerEnd: number;
}
export interface ParsedAttribute {
nameStart: number;
nameEnd: number;
valueStart: number;
valueEnd: number;
}
const alwaysSelfClosing = ["link", "base"];
export function parseHtml(
_source: string,
tagNames: string[] = ["script", "link", "base", "!--"]
) {
const scripts: ParsedTag[] = [];
source = _source;
i = 0;
let curScript: ParsedTag = {
tagName: undefined,
start: -1,
end: -1,
attributes: [],
innerStart: -1,
innerEnd: -1,
};
while (i < source.length) {
while (source.charCodeAt(i++) !== 60 /*<*/)
if (i === source.length) return scripts;
const start = i - 1;
const tagName = readTagName()?.toLowerCase();
if (tagName === "!--") {
while (
source.charCodeAt(i) !== 45 /*-*/ ||
source.charCodeAt(i + 1) !== 45 /*-*/ ||
source.charCodeAt(i + 2) !== 62 /*>*/
)
if (++i === source.length) return scripts;
scripts.push({
tagName: "!--",
start: start,
end: i + 3,
attributes: [],
innerStart: start + 3,
innerEnd: i,
});
i += 3;
} else if (tagName === undefined) {
return scripts;
} else if (tagNames.includes(tagName)) {
curScript.tagName = tagName;
curScript.start = i - tagName.length - 2;
const attributes = curScript.attributes;
let attr;
while ((attr = scanAttr())) attributes.push(attr);
let selfClosing = alwaysSelfClosing.includes(tagName);
if (
source.charCodeAt(i - 2) === 47 /*/*/ &&
source.charCodeAt(i - 1) === 62 /*>*/
)
selfClosing = true;
if (selfClosing) {
curScript.end = i;
} else {
curScript.innerStart = i;
while (true) {
while (source.charCodeAt(i++) !== 60 /*<*/)
if (i === source.length) return scripts;
const tag = readTagName();
if (tag === undefined) return scripts;
if (tag === `/${curScript.tagName}`) {
curScript.innerEnd = i - 8;
while (scanAttr());
curScript.end = i;
break;
}
}
}
scripts.push(curScript);
curScript = {
tagName: undefined,
start: -1,
end: -1,
attributes: [],
innerStart: -1,
innerEnd: -1,
};
} else {
while (scanAttr());
}
}
return scripts;
}
function readTagName(): string | null {
let start = i;
let ch;
while (!isWs((ch = source.charCodeAt(i++))) && ch !== 62 /*>*/)
if (i === source.length) return null;
return source.slice(start, ch === 62 ? --i : i - 1);
}
function scanAttr(): ParsedAttribute | null {
let ch;
while (isWs((ch = source.charCodeAt(i))))
if (++i === source.length) return null;
if (
ch === 62 /*>*/ ||
(ch === 47 /*/*/ && (ch = source.charCodeAt(++i)) === 62) /*>*/
) {
i++;
return null;
}
const nameStart = i;
while (!isWs((ch = source.charCodeAt(i++))) && ch !== 61 /*=*/) {
if (i === source.length) return null;
if (ch === 62 /*>*/) {
if (nameStart + 2 === i && source.charCodeAt(nameStart) === 47 /*/*/)
return null;
return { nameStart, nameEnd: --i, valueStart: -1, valueEnd: -1 };
}
}
const nameEnd = i - 1;
if (ch !== 61 /*=*/) {
while (isWs((ch = source.charCodeAt(i))) && ch !== 61 /*=*/) {
if (++i === source.length) return null;
if (ch === 62 /*>*/) return null;
}
if (ch !== 61 /*=*/)
return { nameStart, nameEnd, valueStart: -1, valueEnd: -1 };
}
while (isWs((ch = source.charCodeAt(i++)))) {
if (i === source.length) return null;
if (ch === 62 /*>*/) return null;
}
if (ch === 34 /*"*/) {
const valueStart = i;
while (source.charCodeAt(i++) !== 34 /*"*/)
if (i === source.length) return null;
return { nameStart, nameEnd, valueStart, valueEnd: i - 1 };
} else if (ch === 39 /*'*/) {
const valueStart = i;
while (source.charCodeAt(i++) !== 39 /*'*/)
if (i === source.length) return null;
return { nameStart, nameEnd, valueStart, valueEnd: i - 1 };
} else {
const valueStart = i - 1;
i++;
while (!isWs((ch = source.charCodeAt(i))) && ch !== 62 /*>*/)
if (++i === source.length) return null;
return { nameStart, nameEnd, valueStart, valueEnd: i };
}
}
export function isWs(ch) {
return ch === 32 || (ch < 14 && ch > 8);
}
// function logScripts (source: string, scripts: ParsedTag[]) {
// for (const script of scripts) {
// for (const { nameStart, nameEnd, valueStart, valueEnd } of script.attributes) {
// console.log('Name: ' + source.slice(nameStart, nameEnd));
// if (valueStart !== -1)
// console.log('Value: ' + source.slice(valueStart, valueEnd));
// }
// console.log('"' + source.slice(script.innerStart, script.innerEnd) + '"');
// console.log('"' + source.slice(script.start, script.end) + '"');
// }
// }