-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocument.js
53 lines (52 loc) · 1.62 KB
/
document.js
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
globalThis.document = {
createElement(tagName) {
return {
tagName,
attributes: {},
children: [],
setAttribute(name, value) {
this.attributes[name] = value;
},
appendChild(child) {
this.children.push(child);
},
get innerHTML() {
return this.children.reduce((prev, child) => prev + child.textContent, '');
},
get outerHTML() {
const shelfClosingTags = [
'area',
'base',
'br',
'col',
'embed',
'hr',
'img',
'input',
'link',
'meta',
'param',
'source',
'track',
'wbr',
];
const attributes = Object.keys(this.attributes)
.map((name) => `${name}="${this.attributes[name]}"`)
.join(' ');
const tagName2 = this.tagName?.toLowerCase() || 'div';
const innerHTML = this.innerHTML;
return `<${tagName2}${attributes ? ` ${attributes}` : ''}>${innerHTML}${
shelfClosingTags.includes(tagName2) ? '' : `</${tagName2}>`
}`;
},
};
},
createTextNode(text) {
return {
textContent: text,
};
},
createElementNS(namespaceURI, tagName) {
return this.createElement(tagName);
},
};