-
-
Notifications
You must be signed in to change notification settings - Fork 63
/
index.ts
239 lines (194 loc) · 6.2 KB
/
index.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
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
import { ElementType } from "domelementtype";
import {
ChildNode,
Element,
DataNode,
Text,
Comment,
CDATA,
Document,
ProcessingInstruction,
ParentNode,
} from "./node.js";
export * from "./node.js";
export interface DomHandlerOptions {
/**
* Add a `startIndex` property to nodes.
* When the parser is used in a non-streaming fashion, `startIndex` is an integer
* indicating the position of the start of the node in the document.
*
* @default false
*/
withStartIndices?: boolean;
/**
* Add an `endIndex` property to nodes.
* When the parser is used in a non-streaming fashion, `endIndex` is an integer
* indicating the position of the end of the node in the document.
*
* @default false
*/
withEndIndices?: boolean;
/**
* Treat the markup as XML.
*
* @default false
*/
xmlMode?: boolean;
}
// Default options
const defaultOpts: DomHandlerOptions = {
withStartIndices: false,
withEndIndices: false,
xmlMode: false,
};
interface ParserInterface {
startIndex: number | null;
endIndex: number | null;
}
type Callback = (error: Error | null, dom: ChildNode[]) => void;
type ElementCallback = (element: Element) => void;
export class DomHandler {
/** The elements of the DOM */
public dom: ChildNode[] = [];
/** The root element for the DOM */
public root: Document = new Document(this.dom);
/** Called once parsing has completed. */
private readonly callback: Callback | null;
/** Settings for the handler. */
private readonly options: DomHandlerOptions;
/** Callback whenever a tag is closed. */
private readonly elementCB: ElementCallback | null;
/** Indicated whether parsing has been completed. */
private done = false;
/** Stack of open tags. */
protected tagStack: ParentNode[] = [this.root];
/** A data node that is still being written to. */
protected lastNode: DataNode | null = null;
/** Reference to the parser instance. Used for location information. */
private parser: ParserInterface | null = null;
/**
* @param callback Called once parsing has completed.
* @param options Settings for the handler.
* @param elementCB Callback whenever a tag is closed.
*/
public constructor(
callback?: Callback | null,
options?: DomHandlerOptions | null,
elementCB?: ElementCallback,
) {
// Make it possible to skip arguments, for backwards-compatibility
if (typeof options === "function") {
elementCB = options;
options = defaultOpts;
}
if (typeof callback === "object") {
options = callback;
callback = undefined;
}
this.callback = callback ?? null;
this.options = options ?? defaultOpts;
this.elementCB = elementCB ?? null;
}
public onparserinit(parser: ParserInterface): void {
this.parser = parser;
}
// Resets the handler back to starting state
public onreset(): void {
this.dom = [];
this.root = new Document(this.dom);
this.done = false;
this.tagStack = [this.root];
this.lastNode = null;
this.parser = null;
}
// Signals the handler that parsing is done
public onend(): void {
if (this.done) return;
this.done = true;
this.parser = null;
this.handleCallback(null);
}
public onerror(error: Error): void {
this.handleCallback(error);
}
public onclosetag(): void {
this.lastNode = null;
const elem = this.tagStack.pop() as Element;
if (this.options.withEndIndices) {
elem.endIndex = this.parser!.endIndex;
}
if (this.elementCB) this.elementCB(elem);
}
public onopentag(name: string, attribs: { [key: string]: string }): void {
const type = this.options.xmlMode ? ElementType.Tag : undefined;
const element = new Element(name, attribs, undefined, type);
this.addNode(element);
this.tagStack.push(element);
}
public ontext(data: string): void {
const { lastNode } = this;
if (lastNode && lastNode.type === ElementType.Text) {
lastNode.data += data;
if (this.options.withEndIndices) {
lastNode.endIndex = this.parser!.endIndex;
}
} else {
const node = new Text(data);
this.addNode(node);
this.lastNode = node;
}
}
public oncomment(data: string): void {
if (this.lastNode && this.lastNode.type === ElementType.Comment) {
this.lastNode.data += data;
return;
}
const node = new Comment(data);
this.addNode(node);
this.lastNode = node;
}
public oncommentend(): void {
this.lastNode = null;
}
public oncdatastart(): void {
const text = new Text("");
const node = new CDATA([text]);
this.addNode(node);
text.parent = node;
this.lastNode = text;
}
public oncdataend(): void {
this.lastNode = null;
}
public onprocessinginstruction(name: string, data: string): void {
const node = new ProcessingInstruction(name, data);
this.addNode(node);
}
protected handleCallback(error: Error | null): void {
if (typeof this.callback === "function") {
this.callback(error, this.dom);
} else if (error) {
throw error;
}
}
protected addNode(node: ChildNode): void {
const parent = this.tagStack[this.tagStack.length - 1];
const previousSibling = parent.children[parent.children.length - 1] as
| ChildNode
| undefined;
if (this.options.withStartIndices) {
node.startIndex = this.parser!.startIndex;
}
if (this.options.withEndIndices) {
node.endIndex = this.parser!.endIndex;
}
parent.children.push(node);
if (previousSibling) {
node.prev = previousSibling;
previousSibling.next = node;
}
node.parent = parent;
this.lastNode = null;
}
}
export default DomHandler;