-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
index.js
680 lines (513 loc) · 19.8 KB
/
index.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
// http://nshipster.com/method-swizzling/
// HTMLElement Swizzle - To swizzle a method is to change a class’s dispatch table in order to resolve messages from an existing selector to a different implementation, while aliasing the original method implementation to a new selector.
// 3.2.3 HTML element constructors
// https://html.spec.whatwg.org/multipage/dom.html#html-element-constructors
// Satisfy Element interface document.createElement
// - https://dom.spec.whatwg.org/#concept-element-interface
var HTMLElement =
/*
// Domenic discusses
// https://esdiscuss.org/topic/extending-an-es6-class-using-es5-syntax#content-1
I believe this will work in most cases:
function B() {
const obj = new A();
Object.setPrototypeOf(obj, new.target.prototype); // or B.prototype, but if you derive from B you'll have to do this dance again
// use obj instead of this
return obj;
}
Also, in general you should do
instead of
B.prototype = Object.create(A.prototype);
for slightly better semantics, including class-side inheritance and not clobbering .constructor.
*/
( function (_) {
function E () {}
E.prototype =
window.HTMLElement.prototype
// Prevent `.constructor` clobbering
// E.__proto__ = window.HTMLElement
// https://github.com/whatwg/html/issues/1704
// E.prototype.__proto__
// = (E.__proto__ = HTMLElement).prototype
// Domenic's method
// Object
// .setPrototypeOf
// (Object.setPrototypeOf (B, A).prototype, A.prototype)
return E
})()
var TokenList = function (node) {
var this$1 = this;
var
visit = function (node) { return node.attributes && [].slice
.call (node.attributes)
.map(collect)
|| collect (node); }
, collect = function (node) { return /{(\w+|#)}/.test (node.textContent)
&& (node.text = node.textContent)
.match (/[^{]+(?=})/g)
.map (function (symbol) { return (this$1 [symbol] || (this$1 [symbol] = [])).push (node); }); }
, walker =
document.createNodeIterator
(node, NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_TEXT, visit, null)
while (walker.nextNode ()) { null } // Walk all nodes and do nothing.
};
TokenList.prototype.bind = function (context) {
var
tokenize = function (symbol) { return function (node) { return (node.textContent
= node.textContent
.split ('{'+symbol+'}')
.join(context [symbol])); }; }
for (var symbol in this)
{ symbol != 'bind'
&& this [symbol].map
(function (node) { return (node.textContent = node.text); }) }
for (var symbol$1 in this)
{ symbol$1 != 'bind'
&& this [symbol$1].map
(tokenize (symbol$1)) } // more than one occurrence
};
// https://codereview.chromium.org/1987413002
// https://github.com/whatwg/fetch/pull/442
// https://chromium.googlesource.com/chromium/src.git/+/a5a314d3249ecf1c291b417fbe067e8c2a65fad2
//
// Link rel preload as attribute doesn't support the as=document value
// https://bugs.chromium.org/p/chromium/issues/detail?id=593267
//
// Requests with useStreamOnResponse flag don't reuse preloaded resources
// https://bugs.chromium.org/p/chromium/issues/detail?id=652228
//
// Spurious warning preloading script
// https://bugs.chromium.org/p/chromium/issues/detail?id=655698
//
// WPT
// https://github.com/w3c/web-platform-tests/pull/4505
//
// w3c preload Tighter definition of "load was successful"
// https://github.com/w3c/preload/issues/83
void ( function (_) {
//create an observer instance
// Can always default to DOMContentLoaded
// https://bugs.webkit.org/show_bug.cgi?id=38995#c26
(new MutationObserver ( function (mutations) {
for (var i$1 = 0, list$1 = mutations; i$1 < list$1.length; i$1 += 1)
{
var mutation = list$1[i$1];
for (var i = 0, list = mutation.addedNodes; i < list.length; i += 1) {
var node = list[i];
/^p/.test (node.rel)
&& /\-/.test (node.id)
&& load (node)
!! /\-/.test (node.localName)
&& (link = document.querySelector ('#'+node.localName))
&& link.content
&& stamp.call (node, link.content)
&& customElements.upgrade (node)
}
}
}))
.observe (document.documentElement, { childList: true, subtree: true })
void
[].slice
.call (document.querySelectorAll ('[rel^=pre][id~="-"]'))
.map (load)
// XHR Specs
// https://xhr.spec.whatwg.org
// Progress events
// https://xhr.spec.whatwg.org/#interface-progressevent
// Loader - https://trac.webkit.org/browser/trunk/WebCore/loader/loader.cpp
function load (link) {
var xhr = new XMLHttpRequest
// Destination - https://fetch.spec.whatwg.org/#requestdestination
xhr.link = link
xhr.onload = onload
// progress events won't fire unless defining before open
xhr.open ('GET', link.href)
xhr.responseType = 'document'
// Max requests
xhr.send ()
}
// https://github.com/w3c/preload/pull/40
// https://bugs.webkit.org/show_bug.cgi?id=38995
// https://www.w3.org/TR/html5/document-metadata.html#the-link-element
function onload () {
var
link = this.link
, response =
this.response
, anchor =
link.nextChild
, template =
link.content =
response.querySelector ('template')
// https://www.nczonline.net/blog/2010/09/28/why-is-getelementsbytagname-faster-that-queryselectorall
for (var i = 0, list = document.querySelectorAll (link.id); i < list.length; i += 1)
{
var node = list[i];
template && stamp.call (node, template)
}
for (var i$1 = 0, list$1 = response.querySelectorAll ('style,link,script'); i$1 < list$1.length; i$1 += 1)
{
var node$1 = list$1[i$1];
process (link, node$1, anchor)
}
}
function process (link, node, anchor) {
var
// https://chromium.googlesource.com/chromium/src.git/+/0661feafc9a84f03b04dd3719b8aaa255dfaec63/third_party/WebKit/Source/core/loader/LinkLoader.cpp
// HTML WhatWG scripting
// https://html.spec.whatwg.org/multipage/scripting.html
// https://html.spec.whatwg.org/multipage/scripting.html#prepare-a-script
// Classic script graph - https://html.spec.whatwg.org/multipage/webappapis.html#fetch-a-classic-script
// Module script tree - https://html.spec.whatwg.org/multipage/webappapis.html#fetch-a-module-script-tree
// Concept Script script - https://html.spec.whatwg.org/multipage/scripting.html#concept-script-script
as = node.getAttribute ('as')
, clone =
document.createElement
('script' == as ? as : node.localName)
void
// 'type' is used for data blocks (i.e. `type=text/recipe` or `type=application/x-game-data`
// https://html.spec.whatwg.org/multipage/scripting.html#data-block
['id', 'rel', 'href', 'src', 'textContent', 'as', 'defer', 'crossOrigin' ]
// setAttribute won't work for textContent and likewise explicit set for crossorigin
.map (function (attr) { return node [attr] && attr in clone && (clone [attr] = node [attr]); })
// use rel = 'preload stylesheet' for async
// or use media=snuggsi => media || 'all' trick
// loadCSS - https://github.com/filamentgroup/loadCSS
// http://keithclark.co.uk/articles/loading-css-without-blocking-render
'style' == as
// https://www.smashingmagazine.com/2016/02/preload-what-is-it-good-for/#markup-based-async-loader
&& (clone.rel = 'stylesheet')
'script' == as // smelly
&& (clone.src = clone.href)
link
.parentNode
.insertBefore (clone, anchor)
}
// Slot replacement & light DOM stamping
// https://github.com/w3c/webcomponents/issues/288
// https://dom.spec.whatwg.org/#slot-assigned-nodes
function stamp (template) {
var this$1 = this;
template =
document.importNode (template, true)
var slot
[] // distribute attributes
.slice
.call (template.attributes)
.map (function (attr) { return ! this$1.attributes [attr.name]
&& this$1.setAttribute (attr.name, attr.value); })
for (var i = 0, list = this.querySelectorAll ('[slot]'); i < list.length; i += 1)
{
var replacement = list[i];
(slot = (template.content || template).querySelector
( 'slot[name=' + replacement.getAttribute ('slot') + ']' ))
&& // this could be slow
slot.parentNode.replaceChild (replacement, slot)
}
return this.innerHTML = template.innerHTML
}
}) ()
var Template = function (template) {
var
range = document.createRange ()
template
= typeof template === 'string'
? document.querySelector ( 'template[name=' + template + ']' )
: template
range.selectNodeContents ( template.content )
var
fragment = range.cloneContents ()
, tokenize = function (context, index) {
var
clone = fragment.cloneNode (true)
typeof context != 'object'
&& ( context = { self: context })
context ['#'] = index
void (new TokenList (clone))
.bind (context)
return clone
}
, bind = function (context) {
range.deleteContents ()
context && []
.concat (context)
.map (tokenize)
.reverse () // Range.insertNode does prepend
.map (function (fragment) { return range.insertNode (fragment); })
}
range.setStartAfter (template)
template.bind = bind
return template
}
window.customElements =
window.customElements
|| {/* microfill */}
void ( function (_) { /* CustomElementRegistry */
customElements.define = function ( name, constructor ) {
!! /\-/.test (name)
&& (customElements [name] = constructor)
&& [].slice
// https://www.nczonline.net/blog/2010/09/28/why-is-getelementsbytagname-faster-that-queryselectorall
.call ( document.querySelectorAll (name) )
.map ( customElements.upgrade )
}
customElements.upgrade = function (root) {
var candidates = []
// Here's where we can swizzle
// https://github.com/whatwg/html/issues/1704#issuecomment-241881091
Object.setPrototypeOf
(root, customElements [root.localName].prototype)
root.connectedCallback ()
}
void (new MutationObserver ( function (mutations) {
for (var i$1 = 0, list$1 = mutations; i$1 < list$1.length; i$1 += 1)
{
var mutation = list$1[i$1];
for (var i = 0, list = mutation.addedNodes; i < list.length; i += 1)
{
var root = list[i];
!! /\-/.test (root.localName)
&& customElements [root.localName]
&& customElements.upgrade (root)
}
}
}))
.observe (document.documentElement, { childList: true, subtree: true })
})() /* CustomElementRegistry */
function ParentNode (Element) {
// DOM Levels
// (https://developer.mozilla.org/fr/docs/DOM_Levels)
//
// Living Standard HTML5 ParentNode
// https://dom.spec.whatwg.org/#parentnode
//
// MDN ParentNode
// https://developer.mozilla.org/en-US/docs/Web/API/ParentNode
//
// ElementTraversal interface
// https://www.w3.org/TR/ElementTraversal/#interface-elementTraversal
return /*@__PURE__*/(function (Element) {
function anonymous () {
Element.apply(this, arguments);
}
if ( Element ) anonymous.__proto__ = Element;
anonymous.prototype = Object.create( Element && Element.prototype );
anonymous.prototype.constructor = anonymous;
anonymous.prototype.select = function ( )
{
var ref;
return (ref = this).selectAll.apply ( ref, arguments ) [0] };
anonymous.prototype.selectAll = function ( strings ) {
var tokens = [], len = arguments.length - 1;
while ( len-- > 0 ) tokens[ len ] = arguments[ len + 1 ];
strings = [ ].concat ( strings )
return [].slice.call
(this.querySelectorAll
(tokens.reduce // denormalize selector
(function (part, token) { return part + token + strings.shift (); }
, strings.shift ())))
};
return anonymous;
}(Element))
}
function EventTarget (HTMLElement) { // why buble
// DOM Levels
// (https://developer.mozilla.org/fr/docs/DOM_Levels)
//
// WHATWG Living Standard HTML5 EventTarget
// https://dom.spec.whatwg.org/#eventtarget
//
// MDN EventTarget
// https://developer.mozilla.org/en-US/docs/Web/API/EventTarget
//
// DOM Level 3 EventTarget
// https://www.w3.org/TR/2000/REC-DOM-Level-2-Events-20001113/events.html#Events-EventTarget
//
// DOM Level 2 EventTarget
// (AKA Str🎱 W3C #fockery) ➡️ https://annevankesteren.nl/2016/01/film-at-11
// 😕 https://w3c.github.io/uievents/DOM3-Events.html#interface-EventTarget
//❓❓ https://www.w3.org/TR/2000/REC-DOM-Level-2-Events-20001113/events.html
// https://www.w3.org/TR/2000/REC-DOM-Level-2-Events-20001113/events.html#Events-EventTarget
// Within https://w3c.github.io/uievents/#conf-interactive-ua
// EventTarget links to WHATWG - https://dom.spec.whatwg.org/#eventtarget
return /*@__PURE__*/(function (HTMLElement) {
function anonymous () {
HTMLElement.apply(this, arguments);
}
if ( HTMLElement ) anonymous.__proto__ = HTMLElement;
anonymous.prototype = Object.create( HTMLElement && HTMLElement.prototype );
anonymous.prototype.constructor = anonymous;
anonymous.prototype.on = function ( event, handler ) {
this.addEventListener
(event, this.renderable (handler))
};
anonymous.prototype.renderable = function ( handler ) {
var this$1 = this;
// BIG BUG IN IE!!!
//
// https://connect.microsoft.com/IE/feedback/details/790389/event-defaultprevented-returns-false-after-preventdefault-was-called
//
// https://github.com/webcomponents/webcomponents-platform/blob/master/webcomponents-platform.js#L16
return function (event) { return handler.call (this$1, event) !== false
// check render availability
&& event.defaultPrevented
|| this$1.render (); }
};
//off (event, listener = 'on' + this [event])
// // MDN EventTarget.removeEventListener
// // https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/removeEventListener
// //
// // WHATWG Living Standard EventTarget.removeEventListener
// // https://dom.spec.whatwg.org/#dom-eventtarget-removeeventlistener
// //
// // DOM Level 2 EventTarget.removeEventListener
// // https://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-EventTarget-removeEventListener
// { this.removeEventListener (event, listener) }
//dispatch (event)
// // MDN EventTarget.dispatchEvent
// // https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/dispatchEvent
// //
// // WHATWG Living Standard EventTarget.dispatchEvent
// // https://dom.spec.whatwg.org/#dom-eventtarget-dispatchevent
// //
// // DOM Level 2 EventTarget.dispatchEvent
// // https://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-EventTarget-dispatchEvent
// { }
// Reflection - https://en.wikipedia.org/wiki/Reflection_(computer_programming)
// Type Introspection - https://en.wikipedia.org/wiki/Type_introspection
//
// In computing, type introspection is the ability of a program
// to examine the type or properties of an object at runtime.
// Some programming languages possess this capability.
//
// Introspection should not be confused with reflection,
// which goes a step further and is the ability for a program to manipulate the values,
// meta-data, properties and/or functions of an object at runtime.
anonymous.prototype.reflect = function (handler) {
/^on/.test (handler) // is a W3C `on`event
&& handler in HTMLElement.prototype // `on*`
&& // automagically delegate event
this.on ( handler.substr (2), this [handler] )
};
anonymous.prototype.register = function (node, handler, event) {
for (var i = 0, list = [].slice.call (node.attributes); i < list.length; i += 1)
{
var attribute = list[i];
/^on/.test (event = attribute.name)
// https://www.quirksmode.org/js/events_tradmod.html
// because under traditional registration the handler value is wrapped in scope `{ onfoo }`
&& ( handler = (/{\s*(\w+)/.exec (node [event]) || []) [1])
&& ( node [event] = this.renderable (this [handler]) )
}
};
return anonymous;
}(HTMLElement)) // class
} // EventTarget
function GlobalEventHandlers (Element) {
// Living Standard HTML5 GlobalEventHandlers
// https://html.spec.whatwg.org/multipage/webappapis.html#globaleventhandlers
//
// MDN GlobalEventHandlers
// https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers
//
// MDN on* Events
// https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Event_handlers
//
// DOM Level 0
// This event handling model was introduced by Netscape Navigator,
// and remains the most cross-browser model as of 2005
// https://en.wikipedia.org/wiki/DOM_events#DOM_Level_0#DOM_Level_0
//
// All Event Handling Models
// https://en.wikipedia.org/wiki/DOM_events#Event_handling_models
//
// Inline Model
// https://en.wikipedia.org/wiki/DOM_events#Inline_model
//
// Traditional Model
// https://en.wikipedia.org/wiki/DOM_events#Traditional_model
//
// Traditional Registration
// http://www.quirksmode.org/js/events_tradmod.html
// HandleEvent Registration - https://viperhtml.js.org/hyperhtml/documentation/#essentials-6
return /*@__PURE__*/(function (Element) {
function anonymous () {
Element.apply(this, arguments);
}
if ( Element ) anonymous.__proto__ = Element;
anonymous.prototype = Object.create( Element && Element.prototype );
anonymous.prototype.constructor = anonymous;
anonymous.prototype.onconnect = function (event) {
this.templates =
this
.selectAll ('template[name]')
.map (Template)
this.tokens =
new TokenList (this)
Element.prototype.onconnect
&& Element.prototype.onconnect.call (this, event)
};
return anonymous;
}(Element)) // class
} // GlobalEventHandlers
var Custom = function (Element) { return ( /*@__PURE__*/(function (superclass) {
function anonymous () {
superclass.apply(this, arguments);
}
if ( superclass ) anonymous.__proto__ = superclass;
anonymous.prototype = Object.create( superclass && superclass.prototype );
anonymous.prototype.constructor = anonymous;
anonymous.prototype.connectedCallback = function () {
this.context = {}
superclass.prototype.initialize
&& superclass.prototype.initialize.call (this)
Object
.getOwnPropertyNames (Element.prototype)
.map (this.reflect, this)
this.addEventListener
('connect', this.onconnect)
this.addEventListener.call
(this, 'idle', superclass.prototype.onidle)
this.dispatchEvent
(new Event ('connect'))
this.render ()
};
anonymous.prototype.render = function () {
for (var i = 0, list = this.templates; i < list.length; i += 1)
{
var template = list[i];
template.bind
(this [template.getAttribute ('name')])
}
this
.tokens
.bind (this)
this.register (this)
this
// possibly restrict to elements with on event
.selectAll ('*')
.map (this.register, this)
this.dispatchEvent
(new Event ('idle'))
};
return anonymous;
}(( ParentNode
( EventTarget
( GlobalEventHandlers
( Element ))))))); }
// http://2ality.com/2013/09/window.html
// http://tobyho.com/2013/03/13/window-prop-vs-global-var
var Element = function (tag) { return (
// const constructor =// swizzle
// typeof tag === 'string'
// // ? HTMLCustomElement
// // : HTMLElement
//https://gist.github.com/allenwb/53927e46b31564168a1d
// https://github.com/w3c/webcomponents/issues/587#issuecomment-271031208
// https://github.com/w3c/webcomponents/issues/587#issuecomment-254017839
function (Element) { return customElements.define
( tag + '', Custom (Element) ); }
// Assign `window.Element.prototype` in case of feature checking on `Element`
// E.prototype = Element.prototype
// return E
); }