-
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathindex.ts
More file actions
225 lines (178 loc) · 5.25 KB
/
Copy pathindex.ts
File metadata and controls
225 lines (178 loc) · 5.25 KB
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
import { Parser, ParserOptions } from 'htmlparser2';
import { LocationTracker, SourceLocation } from './location-tracker';
export type Directive = {
name: string | RegExp;
start: string;
end: string;
};
export type Options = {
directives?: Directive[];
sourceLocations?: boolean;
} & ParserOptions;
export type Tag = string | boolean;
export type Attributes = Record<string, string | number | boolean>;
export type Content = NodeText | Array<Node | Node[]>;
export type NodeText = { text: string | number };
export type NodeTag = {
tag?: Tag;
attrs?: Attributes;
content?: Content;
location?: SourceLocation;
};
export type Node = (NodeText | NodeTag) & { parent?: NodeTag | Node[] };
const defaultOptions: ParserOptions = {
lowerCaseTags: false,
lowerCaseAttributeNames: false,
decodeEntities: false
};
const defaultDirectives: Directive[] = [
{
name: '!doctype',
start: '<',
end: '>'
}
];
export const parser = (html: string, options: Options = {}): Node[] => {
const locationTracker = new LocationTracker(html);
const bufArray: Node[] = [];
const results: Node[] = [];
function bufferArrayLast(): Node {
return bufArray[bufArray.length - 1];
}
function appendChild(parent: NodeTag | Node[], child: Node) {
child.parent = parent;
if (Array.isArray(parent)) {
parent.push(child);
return;
}
if (!Array.isArray(parent.content)) {
parent.content = [];
}
parent.content.push(child);
}
function isDirective(directive: Directive, tag: string): boolean {
if (directive.name instanceof RegExp) {
const regex = new RegExp(directive.name.source, 'i');
return regex.test(tag);
}
if (tag !== directive.name) {
return false;
}
return true;
}
function normalizeArributes(attrs: Attributes): Attributes {
const result: Attributes = {};
Object.keys(attrs).forEach((key: string) => {
const object: Attributes = {};
object[key] = String(attrs[key]).replace(/"/g, '"');
Object.assign(result, object);
});
return result;
}
function onprocessinginstruction(name: string, data: string) {
const directives = defaultDirectives.concat(options.directives ?? []);
const last = bufferArrayLast();
for (const directive of directives) {
const directiveText = directive.start + data + directive.end;
if (isDirective(directive, name.toLowerCase())) {
if (last === undefined) {
appendChild(results, { text: directiveText });
return;
}
if ((typeof last === 'object') && !('text' in last)) {
if (last.content === undefined) {
last.content = [];
}
if (Array.isArray(last.content)) {
appendChild(last, { text: directiveText });
}
}
}
}
}
function oncomment(data: string) {
const last = bufferArrayLast();
const comment = `<!--${data}-->`;
if (last === undefined) {
appendChild(results, { text: comment });
return;
}
if ((typeof last === 'object') && !('text' in last)) {
if (last.content === undefined) {
last.content = [];
}
if (Array.isArray(last.content)) {
appendChild(last, { text: comment });
}
}
}
function onopentag(tag: string, attrs: Attributes) {
const start = locationTracker.getPosition(parser.startIndex);
const buf: NodeTag = { tag };
if (options.sourceLocations) {
buf.location = {
start,
end: start
};
}
if (Object.keys(attrs).length > 0) {
buf.attrs = normalizeArributes(attrs);
}
bufArray.push(buf);
}
function onclosetag() {
const buf: Node | undefined = bufArray.pop();
if (buf && typeof buf === 'object' && !('text' in buf) &&
buf.location && parser.endIndex !== null) {
buf.location.end = locationTracker.getPosition(parser.endIndex);
}
if (buf) {
const last = bufferArrayLast();
if (bufArray.length <= 0) {
appendChild(results, buf);
return;
}
if ((typeof last === 'object') && !('text' in last)) {
if (last.content === undefined) {
last.content = [];
}
if (Array.isArray(last.content)) {
appendChild(last, buf);
}
}
}
}
function ontext(text: string) {
const last: Node = bufferArrayLast();
if (last === undefined) {
appendChild(results, { text });
return;
}
if ((typeof last === 'object') && !('text' in last)) {
if (last.content && Array.isArray(last.content) && last.content.length > 0) {
const lastContentNode = last.content[last.content.length - 1];
if (('text' in lastContentNode) &&
typeof lastContentNode.text === 'string' && !lastContentNode.text.startsWith('<!--')) {
lastContentNode.text += String(text);
return;
}
}
if (last.content === undefined) {
last.content = [];
}
if (Array.isArray(last.content)) {
appendChild(last, { text });
}
}
}
const parser = new Parser({
onprocessinginstruction,
oncomment,
onopentag,
onclosetag,
ontext
}, { ...defaultOptions, ...options });
parser.write(html);
parser.end();
return results;
};