-
Notifications
You must be signed in to change notification settings - Fork 0
/
DOMParser.ts
48 lines (45 loc) · 1.36 KB
/
DOMParser.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
import parse from "https://denopkg.com/nekobato/deno-xml-parser/index.ts";
import { Document } from "./Document.ts";
import { Element } from "./Element.ts";
import { XMLDocument } from "./XMLDocument.ts";
function parseElement(node: any, parent: Element | null): Element {
const element = new Element(node.name, parent);
if (node.content) {
element.textContent = node.content;
}
for (const [key, value] of Object.entries(node.attributes)) {
element.setAttribute(key, value as string);
}
if (Array.isArray(node.children)) {
for (const child of node.children) {
parseElement(child, element);
}
}
parent?.appendChild(element);
return element;
}
export type XMLMimeType =
| "text/xml"
| "application/xml"
| "application/xhtml+xml"
| "image/svg+xml";
export class DOMParser {
public parseFromString(value: string, mimeType: "text/html"): Document;
public parseFromString(value: string, mimeType: XMLMimeType): XMLDocument;
parseFromString(
value: string,
mimeType: "text/html" | XMLMimeType,
): Document | XMLDocument {
const document = parse(value);
if (!document.root) {
// TODO: implement spec for errors
throw new Error();
}
const root = parseElement(document.root, null);
if (mimeType === "text/html") {
return new Document(root);
} else {
return new XMLDocument(root);
}
}
}