-
Notifications
You must be signed in to change notification settings - Fork 0
/
undertone.js
170 lines (147 loc) · 5.33 KB
/
undertone.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
/*! Undertone class | MIT License */
/* eslint-disable-next-line no-unused-vars */
class Undertone extends window.HTMLElement {
// Initialize state
constructor () {
super()
document._undertone || (
document._undertone = {
// Internal registry for loaded custom elements
registry: {}
}
)
// Initialize shadow DOM
this.attachShadow({ mode: 'open' })
}
// Render component
_render (html, scoped) {
html &&
this.addEventListener(':connect', () => {
const { registry } = document._undertone
let name = this.tagName.toLowerCase()
if (!registry[name]) {
// Parse each custom element definition only once
registry[name] = (() => {
// Cache the markup as an HTML template
let markup = document.createElement('template')
markup.innerHTML = html
let scripts = []
// Parse remote scripts
markup.content.querySelectorAll('script')
.forEach((script) => {
// Replicate the element to execute benign script
let code = document.createElement('script')
code.src = script.src
let ref = script.getAttribute('ref')
ref && code.setAttribute('ref', ref)
scripts.push(code)
script.remove()
})
let links = []
// Parse <link rel="stylesheet"> elements
markup.content.querySelectorAll('link[rel="stylesheet"][href]')
.forEach((element) => {
links.push(element.cloneNode(true))
element.remove()
})
// Combine global/component-defined styles into a single tag
let style = document.createElement('style')
if (!scoped) {
// Apply global styles and markup to shadow DOM
let sheets = document.styleSheets
for (let i = 0; i < sheets.length; i++) {
if (sheets[i].href) {
// Delegate global stylesheets
style.textContent += (
'@import url(' +
JSON.stringify(document.styleSheets[i].href) +
');'
)
} else {
// Delegate global styles
for (var j = 0; j < sheets[i].cssRules.length; j++) {
style.textContent += sheets[i].cssRules[j].cssText
}
}
}
}
let imports = []
// Parse component-defined styles
markup.content.querySelectorAll('style')
.forEach((element) => {
if (element.hasAttribute('light-dom')) {
imports.push(element.cloneNode(true))
element.removeAttribute('light-dom')
} else {
let match = (
element.textContent
.match(/@import url\(['"]?.+?['"]?\);?\s*/g)
)
if (match) {
// @import detected (remote stylesheet)
for (let i = 0; i < match.length; i++) {
let css = document.createElement('style')
css.textContent = match[i].trim()
imports.push(css)
element.textContent = (
element.textContent.replace(match[i], '')
)
}
}
style.textContent += element.textContent
}
element.remove()
})
return { markup, scripts, links, style, imports }
})()
}
this.refs || (this.refs = {})
;['imports', 'links', 'scripts'].forEach((key) => {
for (let i = 0; i < registry[name][key].length; i++) {
let element = registry[name][key][i].cloneNode(true)
// [ref] attribute vanishes when element is cloned
let ref = registry[name][key][i].getAttribute('ref')
if (ref) this.refs[ref] = element
// Attach remote stylesheet to host
this.appendChild(element)
}
})
// Apply shadow root styles
this.shadowRoot.appendChild(
registry[name].style.cloneNode(true)
)
// Render markup
this.shadowRoot.appendChild(
registry[name].markup.cloneNode(true).content
)
// Initialize DOM references
this.shadowRoot.querySelectorAll('[ref]')
.forEach((element) => {
this.refs[element.getAttribute('ref')] = element
element.removeAttribute('ref')
})
// Mirror attributes to element properties
for (let i = 0; i < this.attributes.length; i++) {
this[this.attributes[i].name] = this.attributes[i].value
}
// Announce
this.dispatchEvent(new window.CustomEvent(':ready'))
})
}
// Invoke when element is inserted into the DOM
connectedCallback () {
this.dispatchEvent(new window.CustomEvent(':connect'))
}
// Invoke when element is removed from the DOM
disconnectedCallback () {
this.dispatchEvent(new window.CustomEvent(':disconnect'))
}
// Call when observed attribute is added, removed, updated, or replaced
attributeChangedCallback (key, prior, value) {
// Mirror the attribute and property
this[key] = value
this.dispatchEvent(
new window.CustomEvent(':update', { detail: { key, value } })
)
}
}