-
Notifications
You must be signed in to change notification settings - Fork 2
/
render.js
153 lines (133 loc) · 4.19 KB
/
render.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
'use strict'
let cloneId = 0
let selectorScope
const hostRegex = /:host/g
const functionalHostRegex = /:host\((.*?)\)/g
module.exports = function renderFactory (config) {
config = validateAndCloneConfig(config)
config.template = cacheTemplate(config.template)
function render (elem) {
// fall back to non shadow mode (scoped style) for now, add polyfill later
if (config.shadow && elem.attachShadow) {
const shadowRoot = elem.attachShadow({mode: 'open'})
if (config.template) {
const template = document.importNode(config.template, true)
shadowRoot.appendChild(template)
}
if (config.style) {
const style = document.createElement('style')
style.appendChild(document.createTextNode(config.style))
shadowRoot.appendChild(style)
}
} else {
if (config.template) {
const template = document.importNode(config.template, true)
addContext(elem)
composeContentWithTemplate(elem, template)
}
if (config.style) {
addScopedStyle(elem, config.style)
config.style = undefined
}
}
}
render.$name = 'render'
render.$type = 'component'
return render
}
function addContext (elem) {
let child = elem.firstChild
while (child) {
child.$contextState = elem.$contextState
child = child.nextSibling
}
}
function composeContentWithTemplate (elem, template) {
let defaultSlot
const slots = template.querySelectorAll('slot')
for (let i = slots.length; i--;) {
const slot = slots[i]
if (slot.getAttribute('name')) {
const slotFillers = elem.querySelectorAll(`[slot=${slot.getAttribute('name')}]`)
if (slotFillers.length) {
slot.innerHTML = ''
for (let i = slotFillers.length; i--;) {
slot.appendChild(slotFillers[i])
}
}
} else {
defaultSlot = slot
}
}
if (defaultSlot && elem.firstChild) {
defaultSlot.innerHTML = ''
while (elem.firstChild) {
defaultSlot.appendChild(elem.firstChild)
}
}
elem.innerHTML = ''
elem.appendChild(template)
}
function addScopedStyle (elem, styleString) {
setSelectorScope(elem)
styleString = styleString
.replace(functionalHostRegex, `${selectorScope}$1`)
.replace(hostRegex, selectorScope)
const style = document.createElement('style')
style.appendChild(document.createTextNode(styleString))
document.head.insertBefore(style, document.head.firstChild)
scopeSheet(style.sheet)
}
function setSelectorScope (elem) {
const is = elem.getAttribute('is')
selectorScope = (is ? `${elem.tagName}[is="${is}"]` : elem.tagName).toLowerCase()
}
function scopeSheet (sheet) {
const rules = sheet.cssRules
for (let i = rules.length; i--;) {
const rule = rules[i]
if (rule.type === 1) {
const selectorText = rule.selectorText.split(',').map(scopeSelector).join(', ')
const styleText = rule.style.cssText
sheet.deleteRule(i)
sheet.insertRule(`${selectorText} { ${styleText} }`, i)
} else if (rule.type === 4) { // media rules
scopeSheet(rule)
}
}
}
function scopeSelector (selector) {
if (selector.indexOf(selectorScope) !== -1) {
return selector
}
return `${selectorScope} ${selector}`
}
function cacheTemplate (templateHTML) {
if (templateHTML) {
const template = document.createElement('template')
template.innerHTML = templateHTML
return template.content
}
}
function validateAndCloneConfig (rawConfig) {
const resultConfig = {}
if (typeof rawConfig !== 'object') {
throw new TypeError('config must be an object')
}
if (typeof rawConfig.template === 'string') {
resultConfig.template = rawConfig.template
} else if (rawConfig.template !== undefined) {
throw new TypeError('template config must be a string or undefined')
}
if (typeof rawConfig.style === 'string') {
resultConfig.style = rawConfig.style
} else if (rawConfig.style !== undefined) {
throw new TypeError('style config must be a string or undefined')
}
if (typeof rawConfig.shadow === 'boolean') {
resultConfig.shadow = rawConfig.shadow
} else if (rawConfig.shadow !== undefined) {
throw new TypeError('shadow config must be a boolean or undefined')
}
return resultConfig
}