-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
171 lines (151 loc) · 4.91 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
// See https://developer.mozilla.org/en-US/docs/Web/API/Node
interface Node {
ownerDocument: Document;
parentNode: Node;
parentElement: Element;
}
// See https://developer.mozilla.org/en-US/docs/Web/API/Element
class Document implements Node {
head: Element;
body: Element;
activeElement: Element;
ownerDocument: Document;
parentNode: Node;
parentElement: Element;
createElement(tagName: string) {
const element = new Element();
element.tagName = tagName;
element.parentNode = this;
element.ownerDocument = this;
return element;
}
}
// See https://developer.mozilla.org/en-US/docs/Web/API/Document
class Element implements Node {
tagName: string;
children: Element[] = [];
ownerDocument: Document;
parentNode: Node;
parentElement: Element;
append(element: Element) {
this.children.push(element);
element.parentNode = this;
element.parentElement = this;
element.ownerDocument = this.ownerDocument;
}
get outerHTML() {
let html = `<${this.tagName}>`;
for (const child of this.children) {
html += child.outerHTML;
}
html += `</${this.tagName}>`;
return html;
}
}
// See https://developer.mozilla.org/en-US/docs/Web/API/Text
class Text implements Node {
textContent: string;
ownerDocument: Document;
parentNode: Node;
parentElement: Element;
}
// TODO: See if we can use some API from `HTMLRewriter` to not have to keep this
const autoClosingTags: { [tag: string]: string[] } = {
'DD': ['DT'],
'DT': ['DD'],
'P': ['DD', 'P'],
'DL': ['P'],
};
/**
* Implements the `DOMParser` class.
* Note that `parseFromString` method is `async` because the underlying
* `HTMLRewriter` is event-based.
* @see https://developer.mozilla.org/en-US/docs/Web/API/DOMParser
* @see https://bun.sh/docs/api/html-rewriter
* @see https://developers.cloudflare.com/workers/runtime-apis/html-rewriter
*/
export default class DOMParser {
/**
* Parses the HTML text and returns a `Document` object.
* Note that this method is `async` because the underlying `HTMLRewriter` is
* event-based.
*
* @param string The HTML string to parse.
* @param mimeType The `text/html` MIME type.
* @returns A `Document` object representing the HTML document.
*/
parseFromString(string: string, mimeType = 'text/html') {
if (mimeType !== 'text/html') {
throw new TypeError('Only text/html is supported');
}
return new Promise<Document>((resolve, reject) => {
const document = new Document();
new HTMLRewriter()
.on('*', {
element(element) {
// TODO: Carry over the HTML element attributes
if (element.tagName === 'HTML' || element.tagName === 'html') {
if (document.activeElement) {
throw new Error('Only one HTML element is allowed');
}
return;
}
if (element.tagName === 'HEAD' || element.tagName === 'head') {
if (document.head) {
throw new Error('Only one HEAD element is allowed');
}
document.head = document.createElement('head');
document.activeElement = document.head;
return;
}
if (element.tagName === 'BODY' || element.tagName === 'body') {
if (document.body) {
throw new Error('Only one BODY element is allowed');
}
document.body = document.createElement('body');
document.activeElement = document.body;
return;
}
// Create a `body` in case the top-level element came before `body`
if (!document.activeElement) {
document.body = document.createElement('body');
document.activeElement = document.body;
}
const activeElement = document.createElement(element.tagName);
document.activeElement.append(activeElement);
document.activeElement = activeElement;
element.onEndTag(() => {
document.activeElement = document.activeElement.parentElement;
});
},
comments(comment) {
console.log('element comments', comment.text);
},
text(_text) {
//console.log('element text', JSON.stringify(text.text));
}
})
.onDocument({
doctype() {
console.log('doctype');
},
comments() {
console.log('document comments');
},
text(_text) {
//console.log('document text', JSON.stringify(text.text));
},
end(_end) {
if (!document.head) {
document.head = document.createElement('head');
}
if (!document.body) {
document.body = document.createElement('body');
}
resolve(document);
}
})
.transform(string);
});
}
}