-
Notifications
You must be signed in to change notification settings - Fork 1
/
framework.js
126 lines (113 loc) · 3.05 KB
/
framework.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
function template(render){
return function (strings) {
const parts = [strings[0]]
for(let i = 1, l = arguments.length; i < l; i++) parts.push(arguments[i], strings[i])
// Concatenate the text using comments as placeholders.
const nodes = []
let node
let string = ''
for (const part of [].concat(...parts)) {
if (part instanceof Node) {
if (!node) {
node = document.createDocumentFragment()
string += `<!--o:${nodes.length}-->`
nodes.push(node)
}
node.appendChild(part)
} else {
node = undefined
string += part
}
}
/*const _strings = [...strings]
const nodes = []
let string = _strings.shift()
// Concatenate the text using comments as placeholders.
for (const part of args) {
if (part instanceof Node) {
string += `<!--o:${nodes.length}-->`
nodes.push(part)
} else if (Array.isArray(part)) {
let root
for (const _part of part) {
if (_part instanceof Node) {
if (!root) {
root = document.createDocumentFragment()
string += `<!--o:${nodes.length}-->`
nodes.push(root)
}
root.appendChild(_part)
} else {
root = undefined
string += _part
}
}
} else {
string += part
}
string += _strings.shift()
}*/
// Render the text.
const root = render(string)
// Walk the rendered content to replace comment placeholders.
if (nodes.length) {
const placeholders = {}
const walker = document.createTreeWalker(root, NodeFilter.SHOW_COMMENT, null, false)
while (walker.nextNode()) {
node = walker.currentNode
if (node.nodeValue.startsWith('o:')) {
const i = node.nodeValue.slice(2)
placeholders[i] = node
}
}
for (const i in placeholders) {
const node = placeholders[i]
node.parentNode.replaceChild(nodes[i], node)
}
}
// If the rendered content is a single node, detach and return the node.
return root.childNodes.length === 1 ?
root.removeChild(root.firstChild) :
root
}
}
const html = template(string => {
const template = document.createElement('template')
template.innerHTML = string.trim()
const content = document.importNode(template.content, true)
if(content.childNodes.length === 1){
return content
}else{
const div = document.createElement('div')
div.appendChild(content)
return div
}
})
Object.assign(Node.prototype, {
empty(){
while (this.hasChildNodes()) this.removeChild(this.lastChild)
},
replaceContents(...contents){
this.empty()
this.append(...contents)
}
})
Object.assign(EventTarget.prototype, {
on(){
if(arguments[0] instanceof Object){
return Object.entries(arguments[0]).map(args => this.on(...args))
}
this.addEventListener(...arguments)
return {
unobserve: () => this.removeEventListener(...arguments)
}
},
trigger(name, detail){
this.dispatchEvent(new CustomEvent(name, {detail}))
return this
}
})
Function.prototype.toJSON = Function.prototype.toString
const AsyncFunction = async function(){}.constructor
const GeneratorFunction = function*(){}.constructor
const AsyncGenerator = async function*(){}.constructor