-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathserverRender.js
181 lines (165 loc) · 4.92 KB
/
serverRender.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
function serverRender(vm, str) {
var vdom = avalon.lexer(str)
var templates = {}
collectTemplate(vdom, templates)
var oldTree = avalon.speedUp(vdom)
var render = avalon.render(oldTree)
var vtree = render(vm)
var html = avalon.vdom(vtree, 'toHTML', false)
return {
templates: templates,
html: html
}
}
function collectTemplate(vdoms, obj) {
for (var i = 0, el; el = vdoms[i++]; ) {
var props = el.props
if (props) {
var id = props['ms-controller'] || props['ms-important']
if (id) {
obj[id] = VElement.prototype.toHTML.call(el, true)
continue
}
}
if (el.children) {
collectTemplate(el.children, obj)
}
}
}
//重写4大构造器,用于生成更少的HTML
function VElement(type, props, children) {
this.nodeName = type
this.props = props
this.children = children
}
function classNames() {
var classes = []
for (var i = 0; i < arguments.length; i++) {
var arg = arguments[i]
var argType = typeof arg
if (argType === 'string' || argType === 'number' || arg === true) {
classes.push(arg)
} else if (Array.isArray(arg)) {
classes.push(classNames.apply(null, arg))
} else if (argType === 'object') {
for (var key in arg) {
if (arg.hasOwnProperty(key) && arg[key]) {
classes.push(key)
}
}
}
}
return classes.join(' ')
}
//toHTML有两种模式 page模式与模板模式
//page模式就是将数据塞进模板中,并去掉所有ms-*(除了ms-controller, ms-imporant)属性与注释占位符
VElement.prototype = {
constructor: VElement,
toHTML: function (dir) {
var arr = []
var props = this.props || {}
var attr = this['ms-attr']
if (attr && typeof attr === 'object') {
if (Array.isArray(attr)) {//转换成对象
attr = avalon.mix.apply({}, attr)
}
for (var i in attr) {
props[i] = attr[i]
if (!attr[i]) {
delete props[i]
}
}
}
var cls = this['ms-class']
if (cls) {
var oldClass = props['class'] || ''
var newClass = classNames(cls)
var classArray = []
var uniq = {}
String(oldClass + ' ' + newClass).replace(/\S+/g, function (i) {
if (!uniq[i]) {
uniq[i] = 1
classArray.push(i)
}
})
props['class'] = classArray.join(' ')
}
for (var i in props) {
var val = props[i]
if (i.charAt(0) === ':') {
continue
}
if (i.indexOf('ms-') === 0) {
if (dir) {
arr.push(i.replace(/^ms\-/, ':') + '=' + avalon.quote(val + ''))
} else {
if (i === 'ms-controller' || i === 'ms-important') {
arr.push(i + '=' + avalon.quote(val + ''))
}
continue
}
} else {
arr.push(i + '=' + avalon.quote(val + ''))
}
}
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', dir) : ''
}).join('')
}
return str + '</' + this.nodeName + '>'
}
}
function VComment(text) {
this.nodeName = '#comment'
this.nodeValue = text
}
VComment.prototype = {
constructor: VComment,
toHTML: function () {
return '<!--' + this.nodeValue + '-->' + (this.template || "")
}
}
function VText(text) {
this.nodeName = '#text'
this.nodeValue = text
}
VText.prototype = {
constructor: VText,
toHTML: function () {
return this.nodeValue
}
}
function VFragment(a) {
this.nodeName = '#document-fragment'
this.children = a
}
VFragment.prototype = {
constructor: VFragment,
toHTML: function (dir) {
return this.children.map(function (a) {
return avalon.vdom(a, 'toHTML', dir)
}).join('')
}
}
avalon.vdom = function (obj, method, dir) {
switch (obj.nodeName) {
case '#text':
return VText.prototype[method].call(obj)
case '#comment':
return VComment.prototype[method].call(obj)
case '#document-fragment':
return VFragment.prototype[method].call(obj, dir)
case void(0):
return (new VFragment(obj))[method]()
default:
return VElement.prototype[method].call(obj, dir)
}
}
module.exports = serverRender