summaryrefslogtreecommitdiff
path: root/packages/webapi/src/lib/Element.ts
blob: ec096fb6d5af48217326eb5275e570c8a6b51471 (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
import * as _ from './utils'

export class Element extends Node {
	hasAttribute(name: string): boolean {
		void name

		return false
	}

	getAttribute(name: string): string | null {
		return null
	}

	setAttribute(name: string, value: string): void {
		void name
		void value
	}

	removeAttribute(name: string): void {
		void name
	}

	attachShadow(init: Partial<ShadowRootInit>) {
		if (arguments.length < 1) throw new TypeError(`Failed to execute 'attachShadow' on 'Element': 1 argument required, but only 0 present.`)

		if (init !== Object(init)) throw new TypeError(`Failed to execute 'attachShadow' on 'Element': The provided value is not of type 'ShadowRootInit'.`)

		if (init.mode !== 'open' && init.mode !== 'closed') throw new TypeError(`Failed to execute 'attachShadow' on 'Element': Failed to read the 'mode' property from 'ShadowRootInit': The provided value '${init.mode}' is not a valid enum value of type ShadowRootMode.`)

		const internals = _.internalsOf<ElementInternals>(this, 'Element', 'attachShadow')

		if (internals.shadowRoot) throw new Error('The operation is not supported.')

		internals.shadowInit = internals.shadowInit || {
			mode: init.mode,
			delegatesFocus: Boolean(init.delegatesFocus),
		}

		internals.shadowRoot = internals.shadowRoot || (/^open$/.test(internals.shadowInit.mode as string) ? Object.setPrototypeOf(new EventTarget(), ShadowRoot.prototype) as ShadowRoot : null)

		return internals.shadowRoot
	}

	get assignedSlot(): HTMLSlotElement | null {
		return null
	}

	get innerHTML(): string {
		_.internalsOf<ElementInternals>(this, 'Element', 'innerHTML')

		return ''
	}

	set innerHTML(value) {
		_.internalsOf<ElementInternals>(this, 'Element', 'innerHTML')

		void value
	}

	get shadowRoot(): ShadowRoot | null {
		const internals = _.internalsOf<ElementInternals>(this, 'Element', 'shadowRoot')

		return Object(internals.shadowInit).mode === 'open' ? internals.shadowRoot : null
	}

	get localName(): string {
		return _.internalsOf<ElementInternals>(this, 'Element', 'localName').localName as string
	}

	get nodeName(): string {
		return (_.internalsOf<ElementInternals>(this, 'Element', 'nodeName').localName as string).toUpperCase()
	}

	get tagName(): string {
		return (_.internalsOf<ElementInternals>(this, 'Element', 'tagName').localName as string).toUpperCase()
	}
}

export class HTMLElement extends Element {}

export class HTMLBodyElement extends HTMLElement {}

export class HTMLDivElement extends HTMLElement {}

export class HTMLHeadElement extends HTMLElement {}

export class HTMLHtmlElement extends HTMLElement {}

export class HTMLSpanElement extends HTMLElement {}

export class HTMLStyleElement extends HTMLElement {}

export class HTMLTemplateElement extends HTMLElement {}

export class HTMLUnknownElement extends HTMLElement {}

_.allowStringTag(Element)
_.allowStringTag(HTMLElement)
_.allowStringTag(HTMLBodyElement)
_.allowStringTag(HTMLDivElement)
_.allowStringTag(HTMLHeadElement)
_.allowStringTag(HTMLHtmlElement)
_.allowStringTag(HTMLSpanElement)
_.allowStringTag(HTMLStyleElement)
_.allowStringTag(HTMLTemplateElement)
_.allowStringTag(HTMLUnknownElement)

export interface ElementInternals {
	attributes: { [name: string]: string },
	localName?: string
	shadowRoot: ShadowRoot | null
	shadowInit: ShadowRootInit | void
}

export interface ShadowRootInit {
	mode: 'open' | 'closed'
	delegatesFocus: boolean
}