-
Notifications
You must be signed in to change notification settings - Fork 3
/
svg.js
42 lines (40 loc) · 1.28 KB
/
svg.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
import EVENTS from './src/events.js'
/**
* @param {string} tagName
* @return {Element}
*/
export default function(tagName) {
//@ts-ignore
var node = tagName.nodeType === 1 ? tagName : document.createElementNS('http://www.w3.org/2000/svg', tagName)
for (var i=1; i<arguments.length; ++i) {
var arg = arguments[i]
if (arg != null) {
//TODO skip .constructor
if (!arg.constructor || arg.constructor === Object) for (var j=0, ks=Object.keys(arg); j<ks.length; ++j) {
var key = ks[j],
val = arg[key]
if (typeof val !== 'string') {
node[key] = val
//set synthetic events for onUpperCaseName
if (key[0] === 'o' && key[1] === 'n' && key.charCodeAt(2) < 91 && key.charCodeAt(2) > 64 && !EVENTS[key]) {
document.addEventListener(key.slice(2).toLowerCase(), function(e) { //eslint-disable-line
var tgt = e.target
do if (tgt[key]) return tgt[key](e)
//@ts-ignore
while((tgt = tgt.parentNode))
})
EVENTS[key] = true
}
}
else node.setAttribute(key, val)
}
else {
if (Array.isArray(arg)) for (var k=0; k<arg.length; ++k) node.appendChild(
arg[k].nodeType ? arg[k] : document.createTextNode(arg[k])
)
else node.appendChild(arg.nodeType ? arg : document.createTextNode(arg))
}
}
}
return node
}