forked from devpunks/snuggsi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.es
139 lines (102 loc) · 3.86 KB
/
index.es
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
void (_ => {
//create an observer instance
// Can always default to DOMContentLoaded
// https://bugs.webkit.org/show_bug.cgi?id=38995#c26
(new MutationObserver ( mutations => {
for (let mutation of mutations)
for (let node of mutation.addedNodes) {
/^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) {
let xhr = new XMLHttpRequest
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://bugs.webkit.org/show_bug.cgi?id=38995
// https://www.w3.org/TR/html5/document-metadata.html#the-link-element
// https://github.com/w3c/preload/pull/40
function onload (link) {
link = this.link
let
response =
this.response
, anchor =
link.nextChild
, template =
link.content =
response.querySelector ('template')
for (let node of document.querySelectorAll (link.id))
//(let node of document.getElementsByTagName (link.id))
template && stamp.call (node, template)
for (let node of response.querySelectorAll ('style,link,script'))
process (link, node, anchor)
}
function process (link, node, anchor) {
let
// https://chromium.googlesource.com/chromium/src.git/+/0661feafc9a84f03b04dd3719b8aaa255dfaec63/third_party/WebKit/Source/core/loader/LinkLoader.cpp
as = node.getAttribute ('as')
, clone =
document.createElement
('script' == as ? as : node.localName)
void
['id', 'rel', 'href', 'src', 'textContent', 'as', 'defer', 'crossOrigin'/* , media */]
// setAttribute won't work for textContent and likewise explicit set for crossorigin
// Mutually exclusive!!! ?!??!?!?!?!?
.map (attr => 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) {
template =
document.importNode (template, true)
let slot
[] // distribute attributes
.slice
.call (template.attributes)
.map (attr =>
! this.attributes [attr.name]
&& this.setAttribute (attr.name, attr.value))
for (let replacement of this.querySelectorAll ('[slot]'))
(slot = (template.content || template).querySelector
( 'slot[name=' + replacement.getAttribute ('slot') + ']' ))
&& // this could be slow
slot.parentNode.replaceChild (replacement, slot)
return this.innerHTML = template.innerHTML
}
}) ()