/
html-utils.ts
228 lines (181 loc) · 6.71 KB
/
html-utils.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
import { DOMImplementation } from '@xmldom/xmldom';
import type * as AST from 'parse5';
import { parse, parseFragment, serialize } from 'parse5';
import { isAttribute, isComment, isDocument, isDocumentFragment, isDocumentType, isElement, isText } from './xml-utils';
export {
escapeXML as escapeHTML,
escapeXMLAttribute as escapeHTMLAttribute
} from './xml-utils';
export function parseHTMLFromString(document: string): Document {
return parse(document, { treeAdapter: new XMLTreeAdapter() }) as unknown as Document;
}
export function parseHTMLFragmentFromString(fragment: string): DocumentFragment {
return parseFragment(fragment, { treeAdapter: new XMLTreeAdapter() }) as unknown as DocumentFragment;
}
export function serializeHTMLToString(node: Node): string {
if (node.parentNode && isDocument(node.parentNode)) {
node = node.parentNode;
}
else if (!isDocument(node) && !isDocumentFragment(node) && !isAttribute(node)) {
// Hackelihack
const documentElement = node;
node = {
childNodes: {
length: 1,
item: () => documentElement,
}
} as unknown as Document;
}
return serialize(node as any, { treeAdapter: new XMLTreeAdapter() });
}
class XMLTreeAdapter implements AST.TreeAdapter {
private _root: Document;
private _created = false;
private _template = Symbol('template');
private _docMode = Symbol('docMode');
private _location = Symbol('location');
constructor() {
this._root = new DOMImplementation().createDocument(null, null, null);
}
createDocument(): Document {
if (this._created) {
throw new Error('XMLTreeAdapter can only create one document per instance');
}
else {
this._created = true;
return this._root;
}
}
createDocumentFragment(): DocumentFragment {
return this._root.createDocumentFragment();
}
createElement(tagName: string, namespaceURI: string, attrs: AST.Attribute[]): Element {
const element = this._root.createElementNS(namespaceURI, tagName);
for (const attr of attrs) {
if (attr.namespace) {
element.setAttributeNS(attr.namespace, `${attr.prefix}:${attr.name}`, attr.value);
}
else {
element.setAttribute(attr.name, attr.value);
}
}
return element;
}
createCommentNode(data: string): Comment {
return this._root.createComment(data);
}
appendChild(parentNode: Node, newNode: Node): void {
parentNode.appendChild(newNode);
}
insertBefore(parentNode: Node, newNode: Node, referenceNode: Node): void {
parentNode.insertBefore(newNode, referenceNode);
}
setTemplateContent(templateElement: Element, contentElement: DocumentFragment): void {
(templateElement as any)[this._template] = contentElement;
}
getTemplateContent(templateElement: Element): DocumentFragment {
return (templateElement as any)[this._template];
}
setDocumentType(_document: Document, name: string, publicId: string, systemId: string): void {
console.log('setDocumentType not supported', name, publicId, systemId);
}
setDocumentMode(document: Document, mode: AST.DocumentMode): void {
(document as any)[this._docMode] = mode;
}
getDocumentMode(document: Document): AST.DocumentMode {
return (document as any)[this._docMode];
}
getNodeSourceCodeLocation(node: Node): AST.Location | AST.ElementLocation {
return (node as any)[this._location];
}
setNodeSourceCodeLocation(node: Node, location: AST.Location | AST.ElementLocation): void {
(node as any)[this._location] = location;
}
updateNodeSourceCodeLocation(node: Node, location: AST.EndLocation): void {
Object.assign((node as any)[this._location], location);
}
detachNode(node: Node): void {
if (node.parentNode) {
node.parentNode.removeChild(node);
}
}
insertText(parentNode: Node, text: string): void {
parentNode.appendChild(this._root.createTextNode(text)); // FIXME: Optimize
}
insertTextBefore(parentNode: Node, text: string, referenceNode: Node): void {
parentNode.insertBefore(this._root.createTextNode(text), referenceNode); // FIXME: Optimize
}
adoptAttributes(recipient: Element, attrs: AST.Attribute[]): void {
for (const attr of attrs) {
if (attr.namespace) {
if (!recipient.hasAttributeNS(attr.namespace, attr.name)) {
recipient.setAttributeNS(attr.namespace, `${attr.prefix}:${attr.name}`, attr.value);
}
}
else {
if (recipient.hasAttribute(attr.name)) {
recipient.setAttribute(attr.name, attr.value);
}
}
}
}
getFirstChild(node: Node): Node {
return node.firstChild!;
}
getChildNodes(node: Node): Node[] {
const nodes = [];
for (let i = 0; i < node.childNodes.length; ++i) {
nodes.push(node.childNodes.item(i));
}
return nodes;
}
getParentNode(node: Node): Node {
return node.parentNode!;
}
getAttrList(element: Element): AST.Attribute[] {
const attrs = [];
for (let i = 0; i < element.attributes.length; ++i) {
const attr = element.attributes.item(i)!;
attrs.push({
name: attr.name,
value: attr.value,
namespace: attr.namespaceURI || undefined,
prefix: attr.prefix || undefined,
});
}
return attrs;
}
getTagName(element: Element): string {
return element.tagName;
}
getNamespaceURI(element: Element): string {
return element.namespaceURI!;
}
getTextNodeContent(textNode: Text): string {
return textNode.nodeValue!;
}
getCommentNodeContent(commentNode: Comment): string {
return commentNode.nodeValue!;
}
getDocumentTypeNodeName(doctypeNode: DocumentType): string {
return doctypeNode.name;
}
getDocumentTypeNodePublicId(doctypeNode: DocumentType): string {
return doctypeNode.publicId;
}
getDocumentTypeNodeSystemId(doctypeNode: DocumentType): string {
return doctypeNode.systemId;
}
isTextNode(node: Node): boolean {
return isText(node);
}
isCommentNode(node: Node): boolean {
return isComment(node);
}
isDocumentTypeNode(node: Node): boolean {
return isDocumentType(node);
}
isElementNode(node: Node): boolean {
return isElement(node);
}
}