summaryrefslogtreecommitdiff
path: root/packages/webapi/src/lib/Document.ts
blob: b9c30b3a3955ff602a5b14cd00c2fda7c782b91c (plain) (blame)
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
import { Text } from './CharacterData'
import { TreeWalker } from './TreeWalker'
import * as _ from './utils'

export class Document extends Node {
	createElement(name: string) {
		const internals = _.internalsOf<DocumentInternals>(
			this,
			'Document',
			'createElement'
		)

		const customElementInternals: CustomElementRegistryInternals =
			_.INTERNALS.get(internals.target.customElements)

		name = String(name).toLowerCase()

		const TypeOfHTMLElement =
			internals.constructorByName.get(name) ||
			(customElementInternals &&
				customElementInternals.constructorByName.get(name)) ||
			HTMLUnknownElement

		const element = Object.setPrototypeOf(
			new EventTarget(),
			TypeOfHTMLElement.prototype
		) as HTMLElement

		_.INTERNALS.set(element, {
			attributes: {},
			localName: name,
			ownerDocument: this,
			shadowInit: null as unknown as ShadowRootInit,
			shadowRoot: null as unknown as ShadowRoot,
		} as ElementInternals)

		return element
	}

	createNodeIterator(
		root: Node,
		whatToShow: number = NodeFilter.SHOW_ALL,
		filter?: NodeIteratorInternals['filter']
	) {
		const target = Object.create(NodeIterator.prototype)

		_.INTERNALS.set(target, {
			filter,
			pointerBeforeReferenceNode: false,
			referenceNode: root,
			root,
			whatToShow,
		} as NodeIteratorInternals)

		return target
	}

	createTextNode(data: string) {
		return new Text(data)
	}

	createTreeWalker(
		root: Node,
		whatToShow: number = NodeFilter.SHOW_ALL,
		filter?: NodeFilter,
		expandEntityReferences?: boolean
	) {
		const target = Object.create(TreeWalker.prototype)

		_.INTERNALS.set(target, {
			filter,
			currentNode: root,
			root,
			whatToShow,
		} as TreeWalkerInternals)

		return target
	}

	get adoptedStyleSheets(): StyleSheet[] {
		return []
	}

	get styleSheets(): StyleSheet[] {
		return []
	}

	body!: HTMLBodyElement
	documentElement!: HTMLHtmlElement
	head!: HTMLHeadElement
}

export class HTMLDocument extends Document {}

_.allowStringTag(Document)
_.allowStringTag(HTMLDocument)

export const initDocument = (target: Target, exclude: Set<string>) => {
	if (exclude.has('document')) return

	const EventTarget = target.EventTarget || globalThis.EventTarget
	const HTMLDocument = target.HTMLDocument || globalThis.HTMLDocument

	const document: HTMLDocument = (target.document = Object.setPrototypeOf(
		new EventTarget(),
		HTMLDocument.prototype
	))

	_.INTERNALS.set(document, {
		target,
		constructorByName: new Map<string, Function>([
			['body', target.HTMLBodyElement],
			['canvas', target.HTMLCanvasElement],
			['div', target.HTMLDivElement],
			['head', target.HTMLHeadElement],
			['html', target.HTMLHtmlElement],
			['img', target.HTMLImageElement],
			['span', target.HTMLSpanElement],
			['style', target.HTMLStyleElement],
		]),
		nameByConstructor: new Map(),
	} as DocumentInternals)

	const initElement = (name: string, Class: Function) => {
		const target = Object.setPrototypeOf(new EventTarget(), Class.prototype)

		_.INTERNALS.set(target, {
			attributes: {},
			localName: name,
			ownerDocument: document,
			shadowRoot: null as unknown as ShadowRoot,
			shadowInit: null as unknown as ShadowRootInit,
		} as ElementInternals)

		return target
	}

	document.body = initElement('body', target.HTMLBodyElement) as HTMLBodyElement
	document.head = initElement('head', target.HTMLHeadElement) as HTMLHeadElement
	document.documentElement = initElement(
		'html',
		target.HTMLHtmlElement
	) as HTMLHtmlElement
}

interface DocumentInternals {
	body: HTMLBodyElement
	documentElement: HTMLHtmlElement
	head: HTMLHeadElement
	constructorByName: Map<string, Function>
	nameByConstructor: Map<Function, string>
	target: Target
}

interface CustomElementRegistryInternals {
	constructorByName: Map<string, Function>
	nameByConstructor: Map<Function, string>
}

interface ElementInternals {
	attributes: { [name: string]: string }
	localName: string
	ownerDocument: Document
	shadowRoot: ShadowRoot
	shadowInit: ShadowRootInit
}

interface ShadowRootInit extends Record<any, any> {
	mode?: string
}

interface Target extends Record<any, any> {
	HTMLBodyElement: typeof HTMLBodyElement
	HTMLDivElement: typeof HTMLDivElement
	HTMLElement: typeof HTMLElement
	HTMLHeadElement: typeof HTMLHeadElement
	HTMLHtmlElement: typeof HTMLHtmlElement
	HTMLSpanElement: typeof HTMLSpanElement
	HTMLStyleElement: typeof HTMLStyleElement
	customElements: CustomElementRegistry
	document: DocumentInternals
}

interface NodeIteratorInternals {
	filter: NodeFilter
	pointerBeforeReferenceNode: boolean
	referenceNode: Node
	root: Node
	whatToShow: number
}

interface TreeWalkerInternals {
	filter: NodeFilter
	currentNode: Node
	root: Node
	whatToShow: number
}