-
-
Notifications
You must be signed in to change notification settings - Fork 179
/
DOMParser.ts
122 lines (109 loc) · 3.38 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
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
import Document from '../nodes/document/Document.js';
import * as PropertySymbol from '../PropertySymbol.js';
import XMLParser from '../xml-parser/XMLParser.js';
import DOMException from '../exception/DOMException.js';
import DocumentFragment from '../nodes/document-fragment/DocumentFragment.js';
import BrowserWindow from '../window/BrowserWindow.js';
import NodeTypeEnum from '../nodes/node/NodeTypeEnum.js';
/**
* DOM parser.
*
* Reference:
* https://developer.mozilla.org/en-US/docs/Web/API/DOMParser.
*/
export default class DOMParser {
readonly #window: BrowserWindow;
/**
* Constructor.
*
* @param window Window.
*/
constructor(window: BrowserWindow) {
this.#window = window;
}
/**
* Parses HTML and returns a root element.
*
* @param string HTML data.
* @param mimeType Mime type.
* @returns Root element.
*/
public parseFromString(string: string, mimeType: string): Document {
if (!mimeType) {
throw new DOMException('Second parameter "mimeType" is mandatory.');
}
const newDocument = <Document>this.#createDocument(mimeType);
while (newDocument[PropertySymbol.childNodes][PropertySymbol.items].length) {
newDocument.removeChild(newDocument[PropertySymbol.childNodes][PropertySymbol.items][0]);
}
const root = <DocumentFragment>XMLParser.parse(newDocument, string, { evaluateScripts: true });
let documentElement = null;
let documentTypeNode = null;
for (const node of root[PropertySymbol.childNodes][PropertySymbol.items]) {
if (node['tagName'] === 'HTML') {
documentElement = node;
} else if (node[PropertySymbol.nodeType] === NodeTypeEnum.documentTypeNode) {
documentTypeNode = node;
}
if (documentElement && documentTypeNode) {
break;
}
}
if (documentElement) {
if (documentTypeNode) {
newDocument.appendChild(documentTypeNode);
}
newDocument.appendChild(documentElement);
const body = newDocument.body;
if (body) {
while (root[PropertySymbol.childNodes][PropertySymbol.items].length) {
body.appendChild(root[PropertySymbol.childNodes][PropertySymbol.items][0]);
}
}
} else {
switch (mimeType) {
case 'image/svg+xml':
{
while (root[PropertySymbol.childNodes][PropertySymbol.items].length) {
newDocument.appendChild(root[PropertySymbol.childNodes][PropertySymbol.items][0]);
}
}
break;
case 'text/html':
default:
{
const documentElement = newDocument.createElement('html');
const bodyElement = newDocument.createElement('body');
const headElement = newDocument.createElement('head');
documentElement.appendChild(headElement);
documentElement.appendChild(bodyElement);
newDocument.appendChild(documentElement);
while (root[PropertySymbol.childNodes][PropertySymbol.items].length) {
bodyElement.appendChild(root[PropertySymbol.childNodes][PropertySymbol.items][0]);
}
}
break;
}
}
return newDocument;
}
/**
*
* @param mimeType Mime type.
* @returns Document.
*/
#createDocument(mimeType: string): Document {
switch (mimeType) {
case 'text/html':
return new this.#window.HTMLDocument();
case 'image/svg+xml':
return new this.#window.SVGDocument();
case 'text/xml':
case 'application/xml':
case 'application/xhtml+xml':
return new this.#window.XMLDocument();
default:
throw new DOMException(`Unknown mime type "${mimeType}".`);
}
}
}