-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathVElement.modern.js
105 lines (98 loc) · 3.03 KB
/
VElement.modern.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
function VElement(type, props, children) {
this.nodeName = type
this.props = props
this.children = children
}
function skipFalseAndFunction(a) {
return a !== false && (Object(a) !== a)
}
function createSVG(type) {
return document.createElementNS('http://www.w3.org/2000/svg', type)
}
var svgTags = avalon.oneObject('circle,defs,ellipse,image,line,' +
'path,polygon,polyline,rect,symbol,text,use,g,svg')
var rvml = /^\w+\:\w+/
if (avalon.browser) {
var supportTemplate = 'content' in document.createElement('template')
}
VElement.prototype = {
constructor: VElement,
toDOM: function () {
if (this.dom)
return this.dom
var dom, tagName = this.nodeName
if (avalon.modern && svgTags[tagName]) {
dom = createSVG(tagName)
} else {
dom = document.createElement(tagName)
}
var props = this.props || {}
var wid = props['ms-important'] ||
props['ms-controller'] || this.wid
if (wid) {
var scope = avalon.scopes[wid]
var element = scope && scope.vmodel && scope.vmodel.$element
if (element) {
var oldVdom = element.vtree[0]
if (oldVdom.children) {
this.children = oldVdom.children
}
return element
}
}
for (var i in props) {
var val = props[i]
if (skipFalseAndFunction(val)) {
dom.setAttribute(i, val + '')
}
}
var c = this.children || []
var template = c[0] ? c[0].nodeValue : ''
switch (this.nodeName) {
case 'xmp':
case 'script':
case 'style':
case 'noscript':
dom.innerHTML = template
break
case 'template':
if (supportTemplate) {
dom.innerHTML = template
} else {
dom.textContent = template
}
break
default:
if (!this.isVoidTag) {
this.children.forEach(function (c) {
c && dom.appendChild(avalon.vdom(c, 'toDOM'))
})
}
break
}
return this.dom = dom
},
toHTML: function () {
var arr = []
var props = this.props || {}
for (var i in props) {
var val = props[i]
if (skipFalseAndFunction(val)) {
arr.push(i + '=' + avalon.quote(props[i] + ''))
}
}
arr = arr.length ? ' ' + arr.join(' ') : ''
var str = '<' + this.nodeName + arr
if (this.isVoidTag) {
return str + '/>'
}
str += '>'
if (this.children) {
str += this.children.map(function (c) {
return c ? avalon.vdom(c, 'toHTML') : ''
}).join('')
}
return str + '</' + this.nodeName + '>'
}
}
module.exports = VElement