-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpolyfill.js
275 lines (220 loc) · 6.13 KB
/
polyfill.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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
class HTMLTimeElement extends HTMLElement
{
get dateTime()
{
return this.getAttribute("datetime") || ""
}
set dateTime(value)
{
return this.setAttribute("datetime", value)
}
}
(function()
{
"use strict"
// if ("customElements" in window) return
function CustomElementRegistry()
{
throw new TypeError
}
Object.defineProperty(window, "CustomElementRegistry",
{
value: CustomElementRegistry,
writable: true,
configurable: true,
})
Object.assign(CustomElementRegistry.prototype,
{
define,
get,
whenDefined,
})
Object.defineProperty(CustomElementRegistry.prototype, Symbol.toStringTag,
{
value: "CustomElementRegistry",
configurable: true,
})
function assertContext(context)
{
if (context != customElements) throw new TypeError
}
function cb(value)
{
switch (typeof value)
{
case "undefined": return function() {}
case "function": return value
}
throw new TypeError
}
function defer()
{
var result = {}
result.promise = new Promise(function(resolve)
{
result.resolve = resolve
})
return result
}
function upgrade(entry, is)
{
function process(el)
{
Object.setPrototypeOf(el, entry.fn.prototype)
entry.connect.call(el)
var attrs = entry.attrs
if (!attrs) return
attrs.forEach(function(attr)
{
var value = el.getAttribute(attr)
if (value == null) return
entry.attrChange.call(el, attr, null, value)
})
new MutationObserver(function(records)
{
records.forEach(function(record)
{
var attr = record.attributeName
var old = record.oldValue
var value = el.getAttribute(attr)
entry.attrChange.call(el, attr, old, value)
})
})
.observe(el,
{
attributes: true,
attributeOldValue: true,
attributeFilter: attrs,
})
}
new MutationObserver(function(records)
{
records.forEach(function(record)
{
record.addedNodes.forEach(function(added)
{
if (!(added instanceof Element)) return
if (added.matches(is)) process(added)
added.querySelectorAll(is).forEach(process)
})
})
})
.observe(document.documentElement,
{
childList: true,
subtree: true,
})
}
function define(name, fn, options)
{
if (arguments.length < 2) throw new TypeError
if (!isConstructor(fn)) throw new TypeError
var selector = name = `${name}`
if (!isValidTag(name))
throw new DOMException("SyntaxError")
if (name in registry || hasConstructor(fn))
throw new DOMException("NotSupportedError")
if (options && options.extends != null)
{
var parentTag = `${options.extends}`
// if (isUnknown(parentTag))
// throw new DOMException("NotSupportedError")
selector = `${parentTag}[is=${name}]`
}
if (defIsRunning)
throw new DOMException("NotSupportedError")
defIsRunning = true
var entry = { fn }
try
{
var proto = fn.prototype
if (proto !== Object(proto)) throw new TypeError
Object.assign(entry,
{
connect: cb(proto.connectedCallback),
disconnect: cb(proto.disconnectedCallback),
adopt: cb(proto.adoptedCallback),
attrChange: cb(proto.attributeChangedCallback),
})
var attrs = fn.observedAttributes
if (attrs !== undefined)
{
entry.attrs = Array.from(attrs, function(attr)
{
return `${attr}`
})
}
}
finally
{
defIsRunning = false
}
upgrade(entry, selector)
var defer = promises[name]
if (!defer) return
defer.resolve()
delete promises[name]
}
function get(name)
{
assertContext(this)
var entry = registry[`${name}`]
return entry && entry.fn
}
function whenDefined(name)
{
assertContext(this)
name = `${name}`
if (!isValidTag(name))
return Promise.reject(new DOMException("SyntaxError"))
if (name in registry)
return Promise.resolve()
if (!(name in promises))
promises[name] = defer()
return promises[name].promise
}
var registry = Object.create(null)
var promises = Object.create(null)
var defIsRunning = false
function isUnknown(tag)
{
return HTMLUnknownElement == document.createElement(tag).constructor
}
function isConstructor(fn)
{
// TODO: check for [[Construct]] method
return typeof fn == "function"
}
var svgElements =
[
"annotation-xml",
"color-profile",
"font-face",
"font-face-src",
"font-face-uri",
"font-face-format",
"font-face-name",
"missing-glyph",
]
function isValidTag(name)
{
return /^[a-z]/.test(name)
&& !/[A-Z]/.test(name)
&& name.includes("-")
&& !svgElements.includes(name)
}
function hasConstructor(fn)
{
return Object.values(registry).some(function(entry)
{
return entry.fn == fn
})
}
var customElements = Object.create(CustomElementRegistry.prototype)
Object.defineProperty(window, "customElements",
{
value: customElements,
enumerable: true,
configurable: true,
})
})()