-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.mjs
391 lines (360 loc) · 10.8 KB
/
index.mjs
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
import { parse, fragment, serialize, serializeOuter } from '@begin/parse5'
import isCustomElement from './lib/is-custom-element.mjs'
import { encode, decode } from './lib/transcode.mjs'
import walk from './lib/walk.mjs'
import { customAlphabet } from 'nanoid'
const alphabet = '0123456789abcdefghijklmnopqrstuvwxyz';
const nanoid = customAlphabet(alphabet, 7);
export default function Enhancer(options={}) {
const {
initialState={},
elements=[],
scriptTransforms=[],
styleTransforms=[],
uuidFunction=nanoid,
bodyContent=false,
enhancedAttr=true
} = options
const store = Object.assign({}, initialState)
function processCustomElements({ node }) {
const collectedStyles = []
const collectedScripts = []
const collectedLinks = []
const context = {}
walk(node, child => {
if (isCustomElement(child.tagName)) {
if (elements[child.tagName]) {
const {
frag:expandedTemplate,
styles:stylesToCollect,
scripts:scriptsToCollect,
links:linksToCollect
} = expandTemplate({
node: child,
elements,
state: {
context,
instanceID: uuidFunction(),
store
},
styleTransforms,
scriptTransforms
})
if (enhancedAttr) {
child.attrs.push({ name: 'enhanced', value:'✨' })
}
collectedScripts.push(scriptsToCollect)
collectedStyles.push(stylesToCollect)
collectedLinks.push(linksToCollect)
fillSlots(child, expandedTemplate)
}
}
})
return {
collectedStyles,
collectedScripts,
collectedLinks
}
}
return function html(strings, ...values) {
const doc = parse(render(strings, ...values))
const html = doc.childNodes.find(node => node.tagName === 'html')
const body = html.childNodes.find(node => node.tagName === 'body')
const head = html.childNodes.find(node => node.tagName === 'head')
const {
collectedStyles,
collectedScripts,
collectedLinks
} = processCustomElements({ node: body })
if (collectedScripts.length) {
const uniqueScripts = collectedScripts.flat().reduce((acc, script) => {
const scriptSrc = script?.attrs?.find(a => a.name === 'src')
const scriptSrcValue = scriptSrc?.value
const scriptContents = script?.childNodes?.[0]?.value
if (scriptContents || scriptSrc) {
return {
...acc,
[scriptContents || scriptSrcValue]: script
}
}
return {...acc}
}, {})
appendNodes(body, Object.values(uniqueScripts))
}
if (collectedStyles.length) {
const uniqueStyles = collectedStyles.flat().reduce((acc, style) => {
if (style?.childNodes?.[0]?.value) {
return { ...acc, [style.childNodes[0].value]: '' }
}
return {...acc}
}, { })
const mergedCss = Object.keys(uniqueStyles)
mergedCss.sort((a, b) => {
const aStart = a.trim().substring(0,7)
const bStart = b.trim().substring(0,7)
if (aStart === '@import' && bStart !== '@import') return -1
if (aStart !== '@import' && bStart === '@import') return 1
return 0
})
const mergedCssString = mergedCss.join('\n')
const mergedStyles = mergedCssString? `<style>${mergedCssString}</style>`:''
if (mergedStyles) {
const stylesNodeHead = [fragment(mergedStyles).childNodes[0]]
appendNodes(head, stylesNodeHead)
}
}
if (collectedLinks.length) {
const uniqueLinks = collectedLinks.flat().reduce((acc, link) => {
if (link) {
return {
...acc,
[normalizeLinkHtml(link)]: link
}
}
return {...acc}
}, {})
appendNodes(head, Object.values(uniqueLinks))
}
return (bodyContent
? serializeOuter(body.childNodes[0])
: serialize(doc))
.replace(/__b_\d+/g, '')
}
}
function render(strings, ...values) {
const collect = []
for (let i = 0; i < strings.length - 1; i++) {
collect.push(strings[i], encode(values[i]))
}
collect.push(strings[strings.length - 1])
return collect.join('')
}
function expandTemplate({ node, elements, state, styleTransforms, scriptTransforms }) {
const tagName = node.tagName
const frag = renderTemplate({
name: node.tagName,
elements,
attrs: node.attrs,
state
}) || ''
const styles= []
const scripts = []
const links = []
for (const node of frag.childNodes) {
if (node.nodeName === 'script') {
frag.childNodes.splice(frag.childNodes.indexOf(node), 1)
const transformedScript = applyScriptTransforms({ node, scriptTransforms, tagName })
if (transformedScript) {
scripts.push(transformedScript)
}
}
if (node.nodeName === 'style') {
frag.childNodes.splice(frag.childNodes.indexOf(node), 1)
const transformedStyle = applyStyleTransforms({ node, styleTransforms, tagName, context: 'markup' })
if (transformedStyle) {
styles.push(transformedStyle)
}
}
if (node.nodeName === 'link') {
frag.childNodes.splice(frag.childNodes.indexOf(node), 1)
links.push(node)
}
}
return { frag, styles, scripts, links }
}
function normalizeLinkHtml(node) {
const attrs = Array.from(node.attrs)
.sort((a, b) => {
if (a.name < b.name) {
return -1;
} else if (b.name < a.name) {
return 1
}
return 0
})
.map(attr => `${attr.name}="${attr.value}"`)
return `<link ${attrs.join(' ')} />`
}
function renderTemplate({ name, elements, attrs=[], state={} }) {
attrs = attrs ? attrsToState(attrs) : {}
state.attrs = attrs
const templateRenderFunction = elements[name]?.render || elements[name]?.prototype?.render
const template = templateRenderFunction
? templateRenderFunction
: elements[name]
if (template && typeof template === 'function') {
return fragment(template({ html: render, state }))
}
else {
throw new Error(`Could not find the template function for ${name}`)
}
}
function attrsToState(attrs=[], obj={}) {
[...attrs].forEach(attr => obj[attr.name] = decode(attr.value))
return obj
}
function fillSlots(node, template) {
const slots = findSlots(template)
const inserts = findInserts(node)
const usedSlots = []
const usedInserts = []
const unnamedSlots = []
for (let i=0; i<slots.length; i++) {
let hasSlotName = false
const slot = slots[i]
const slotAttrs = slot.attrs || []
const slotAttrsLength = slotAttrs.length
for (let i=0; i < slotAttrsLength; i++) {
const attr = slotAttrs[i]
if (attr.name === 'name') {
hasSlotName = true
const slotName = attr.value
const insertsLength = inserts.length
for (let i=0; i < insertsLength; i ++) {
const insert = inserts[i]
const insertAttrs = insert.attrs || []
const insertAttrsLength = insertAttrs.length
for (let i=0; i < insertAttrsLength; i++) {
const attr = insertAttrs[i]
if (attr.name === 'slot') {
const insertSlot = attr.value
if (insertSlot === slotName) {
const slotParentChildNodes = slot.parentNode.childNodes
slotParentChildNodes.splice(
slotParentChildNodes
.indexOf(slot),
1,
insert
)
usedSlots.push(slot)
usedInserts.push(insert)
}
}
}
}
}
}
if (!hasSlotName) {
unnamedSlots.push([slot, node])
}
}
unnamedSlots.forEach(([ slot, node ]) => {
const nodeChildren = node.childNodes
.filter(node => !usedInserts.includes(node))
const children = nodeChildren.length
? nodeChildren
: [ ...slot.childNodes ]
const slotParentChildNodes = slot.parentNode.childNodes
slotParentChildNodes.splice(
slotParentChildNodes
.indexOf(slot),
1,
...children
)
})
const unusedSlots = slots.filter(slot => !usedSlots.includes(slot))
const nodeChildNodes = node.childNodes
replaceSlots(template, unusedSlots)
nodeChildNodes.splice(
0,
nodeChildNodes.length,
...template.childNodes
)
}
function findSlots(node) {
const elements = []
const find = (node) => {
for (const child of node.childNodes) {
if (child.tagName === 'slot') {
elements.push(child)
}
if (child.childNodes) {
find(child)
}
}
}
find(node)
return elements
}
function findInserts(node) {
const elements = []
const find = (node) => {
for (const child of node.childNodes) {
const hasSlot = child.attrs?.find(attr => attr.name === 'slot')
if (hasSlot) {
elements.push(child)
}
}
}
find(node)
return elements
}
function replaceSlots(node, slots) {
slots.forEach(slot => {
const value = slot.attrs.find(attr => attr.name === 'name')?.value
const asTag = slot.attrs.find(attr => attr.name === 'as')?.value
const name = 'slot'
const slotChildren = slot.childNodes.filter(
n => {
return !n.nodeName.startsWith('#')
}
)
if (value) {
if (!slotChildren.length || slotChildren.length > 1) {
// Only has text nodes
const wrapperSpan = {
nodeName: asTag ? asTag : 'span',
tagName: asTag ? asTag : 'span',
attrs: [{ value, name }],
namespaceURI: 'http://www.w3.org/1999/xhtml',
childNodes: []
}
wrapperSpan.childNodes.push(...slot.childNodes)
slot.childNodes.length = 0
slot.childNodes.push(wrapperSpan)
}
if (slotChildren.length === 1) {
slotChildren[0].attrs.push({ value, name })
}
const slotParentChildNodes = slot.parentNode.childNodes
slotParentChildNodes.splice(
slotParentChildNodes
.indexOf(slot),
1,
...slot.childNodes
)
}
})
return node
}
function applyScriptTransforms({ node, scriptTransforms, tagName }) {
const attrs = node?.attrs || []
if (node.childNodes.length) {
const raw = node.childNodes[0].value
let out = raw
scriptTransforms.forEach(transform => {
out = transform({ attrs, raw: out, tagName })
})
if (out.length) {
node.childNodes[0].value = out
}
}
return node
}
function applyStyleTransforms({ node, styleTransforms, tagName, context='' }) {
const attrs = node?.attrs || []
if (node.childNodes.length) {
const raw = node.childNodes[0].value
let out = raw
styleTransforms.forEach(transform => {
out = transform({ attrs, raw: out, tagName, context })
})
if (out.length) {
node.childNodes[0].value = out
}
}
return node
}
function appendNodes(target, nodes) {
target.childNodes.push(...nodes)
}