-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
133 lines (123 loc) · 2.85 KB
/
index.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
/**
* Created by army8735 on 2018/1/5.
*/
'use strict';
const migi = require('migi');
const Component = require('./Component');
const Obj = require('./Obj');
const attr = migi.attr;
const util = migi.util;
const selfClose = migi.selfClose;
migi.Component = Component;
migi.Obj = Obj;
function render(element) {
return element.str;
}
function createCp(cp, props, children) {
let c = new cp([migi.uid++, props, children]);
return c.toString();
}
function createVd(name, props, children, uid = migi.uid++) {
props = props || [];
children = children || [];
let res = {};
let str = '<' + name;
let dangerouslySetInnerHTML;
props.forEach(function(prop) {
let k = prop[0];
let v = prop[1];
// 忽略事件
if(/^on[a-zA-Z]/.test(k)) {}
//Obj类型
else if(v instanceof Obj) {
let s = v.toString(true);
//忽略特殊html不转义
if(k === 'dangerouslySetInnerHTML') {
dangerouslySetInnerHTML = s;
return;
}
if(k === 'className') {
k = 'class';
}
else if(k === 'htmlFor') {
k = 'for';
}
let special = attr.special(name, k);
switch(special) {
case attr.RENDER_EXIST:
if(v.v) {
str += ' ' + k + '="' + s + '"';
}
break;
default:
str += ' ' + k + '="' + s + '"';
break;
}
}
else {
let s = Array.isArray(v) ? util.joinSourceArray(v) : util.stringify(v);
if(k === 'dangerouslySetInnerHTML') {
dangerouslySetInnerHTML = s;
return;
}
if(k === 'className') {
k = 'class';
}
else if(k === 'htmlFor') {
k = 'for';
}
let special = attr.special(name, k);
switch(special) {
case attr.RENDER_EXIST:
if(v) {
str += ' ' + k + '="' + s + '"';
}
break;
default:
str += ' ' + k + '="' + s + '"';
break;
}
}
});
str += ' migi-uid="' + uid + '"';
//自闭合标签特殊处理
if(selfClose.hasOwnProperty(name)) {
str += '/>';
res.str = str;
return res;
}
str += '>';
//有dangerouslySetInnerHTML直接返回
if(dangerouslySetInnerHTML) {
str += dangerouslySetInnerHTML;
}
//渲染children
else {
children.forEach(function(child) {
str += renderChild(child);
});
}
str +='</' + name + '>';
res.str = str;
return res;
}
function renderChild(child) {
if(Array.isArray(child)) {
let s = '';
child.forEach(function(item) {
s += renderChild(item);
});
return s;
}
else if(child instanceof Obj) {
return child.toString();
}
else if(child && child.str) {
return child.str;
}
return util.encodeHtml(util.stringify(child));
}
migi.preRender = migi.render = render;
migi.createCp = createCp;
migi.createVd = createVd;
module.exports = migi;