-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolyfill.js
163 lines (133 loc) · 4.16 KB
/
polyfill.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
export default function polyfill({ completePolyfill }) {
if (typeof window.AnonymousElement === 'function') return
var anonymousElements = new WeakMap()
var isAnonymous = Symbol('isAnonymous')
var allowAnonDef = false
var usedNames = {}
var defineElement = window.customElements.define
window.customElements.define = function(...args) {
const name = args[0]
const type = args[1]
if (!allowAnonDef && type[isAnonymous]) return
return defineElement.apply(window.customElements, args)
}
class AnonymousElement extends HTMLElement {}
if (completePolyfill) {
const innerHTML = Object.getOwnPropertyDescriptor(
Element.prototype,
'innerHTML',
)
const outerHTML = Object.getOwnPropertyDescriptor(
Element.prototype,
'outerHTML',
)
Object.defineProperty(Element.prototype, 'innerHTML', {
get() {
if (this.childNodes) {
const result = []
for (let i = 0; i < this.childNodes.length; i++) {
if (this.childNodes[i] instanceof Text) {
result.push(this.childNodes[i].textContent)
} else if (this.childNodes[i] instanceof Comment) {
result.push(`<!--${this.childNodes[i].textContent}-->`)
} else {
result.push(this.childNodes[i].outerHTML)
}
}
return result.join('')
}
return innerHTML.get.call(this)
},
set() {
return innerHTML.set.call(this)
},
})
Object.defineProperty(Element.prototype, 'outerHTML', {
get() {
if (this.childNodes) {
const result = []
for (let i = 0; i < this.childNodes.length; i++) {
if (this.childNodes[i] instanceof Text) {
result.push(this.childNodes[i].textContent)
} else if (this.childNodes[i] instanceof Comment) {
result.push(`<!--${this.childNodes[i].textContent}-->`)
} else {
result.push(this.childNodes[i].outerHTML)
}
}
if (this instanceof AnonymousElement) {
return result.join('')
}
const tagName = this.tagName.toLowerCase()
let attrs = []
for (let i = 0; i < this.attributes.length; i++) {
attrs.push(
`${this.attributes[i].name}="${this.attributes[i].value}"`,
)
}
attrs = attrs.length ? ' ' + attrs.join(' ') : ''
return `<${tagName}${attrs}>${result.join('')}</${tagName}>`
}
return outerHTML.get.call(this)
},
set() {
return outerHTML.set.call(this)
},
})
}
AnonymousElement[isAnonymous] = true
window.AnonymousElement = new Proxy(AnonymousElement, {
construct: function(target, args, newTarget) {
if (!isRegistered(newTarget)) {
registerAnonymousElement(newTarget)
}
var instance = Reflect.construct(...arguments)
instance.style.display = 'contents !important'
return instance
},
})
function generateRandomName(Component) {
var baseName = decamelize(Component.name)
var suffix = randomString()
if (!/[a-z\-]+/.test(baseName)) {
return `${suffix}---anonymous`
}
return `${baseName}---anonymous-${suffix}`
}
function decamelize(string) {
var letters = string.split('')
var result = []
letters.forEach(function(letter) {
var lowerCased = letter.toLowerCase()
if (lowerCased !== letter) {
result.push('-', letter.toLowerCase())
} else {
result.push(letter)
}
})
while (result[0] === '-') {
result.shift()
}
return result.join('')
}
function randomString() {
return Math.random()
.toString(36)
.slice(2)
.replace(/[^a-z]/g, '')
}
function isRegistered(CustomElement) {
return anonymousElements.has(CustomElement)
}
function registerAnonymousElement(CustomElement) {
var name = generateRandomName(CustomElement)
usedNames[name] = true
allowAnonDef = true
try {
customElements.define(name, CustomElement)
} finally {
allowAnonDef = false
}
anonymousElements.set(CustomElement, name)
}
}