-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
314 lines (250 loc) · 7.81 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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
(function(w) {
const RMSCRIPT = /<script.*?>.*?<\/script>/gi;
const RMLINE = /[\r\n]/g;
const STARTTAG = /{{|{%/;
const CONDITION = /\((.*?)\)/;
const tagsToReplace = {
"&": "&",
"<": "<",
">": ">"
};
w.tpl = tpl;
function tpl(param) {
let body = document.querySelector("body");
let html = body.innerHTML.replace(RMLINE, "").replace(RMSCRIPT, "");
// 块级栈
let blockStack = [];
let astTree = [];
// 解析变量函数
param._s = str => str;
// 过滤器处理函数
param._f = (name, str) => name(str);
// 编译html字符串为ast语法树
function parse(html = "") {
// html 为空时
if (!html.trim()) {
blockStack = [];
if (html.length) {
return " ";
}
return "";
}
// 匹配正则
const startMatchs = html.match(STARTTAG) || {};
// 是否是块级作用域
const isBlock = startMatchs[0] === "{%";
const startIndex = startMatchs.index;
if (startIndex !== 0) {
let len = html.length;
let text = html.slice(0, startIndex || len);
if (text) {
let ast = { type: 1, text };
// 存在块级表明该文本节点在块级中
if (blockStack.length) {
if (!blockStack[blockStack.length - 1].children) {
blockStack[blockStack.length - 1].children = [];
}
blockStack[blockStack.length - 1].children.push(ast);
} else {
astTree.push(ast);
}
if (html.slice(startIndex || len).length) {
return parse(html.slice(startIndex || len));
}
}
blockStack = [];
return;
}
const endIndex = html.indexOf(isBlock ? "%}" : "}}");
// html 只有 {{ 或 {% 时,没有 }} 或 %} 处理语句
if (endIndex === -1) {
let ast = { type: 1, text: html.slice(0, 2) };
if (blockStack.length) {
if (!blockStack[blockStack.length - 1].children) {
blockStack[blockStack.length - 1].children = [];
}
blockStack[blockStack.length - 1].children.push(ast);
} else {
astTree.push(ast);
}
if (html.slice(2).length) {
return parse(html.slice(2));
}
return;
}
// 块级作用域
if (isBlock) {
// 拿到{% %} 里面的东西
let item = html
.trim()
.slice(startIndex + 2, endIndex)
.trim();
let block = "";
let isElse = false;
// end结束
if (item === "}") {
// 出栈
let last = blockStack.pop();
if (blockStack.length) {
if (!blockStack[blockStack.length - 1].children) {
blockStack[blockStack.length - 1].children = [];
}
blockStack[blockStack.length - 1].children.push(last);
} else {
astTree.push(last);
}
return parse(html.slice(endIndex + 2));
}
// 拿到内部的是for 还是 if
if (item.indexOf("if") !== -1 || item.indexOf("else") !== -1) {
block = "if";
if (item.indexOf("if") !== 0) {
block = "else if";
isElse = true;
}
} else if (item.includes("for")) {
block = "for";
}
// 拿到条件语句
let matchs = [...new Set(item.match(CONDITION))];
// 对条件语句进行标签替换,innerHTML 拿到的 > < = 默认会被转义
item = (matchs[1] || "").replace(/(<)?(>)?(&)?;?/g, replaceTag);
let ast = {
type: 3,
block,
item
};
// 如果是else 或者 else if
if (isElse) {
let last = blockStack.pop();
blockStack.push({
...ast,
...(ast.item ? ast.item : { item: last.item }) // else 拿 if的判断语句,else if 用自己的
});
astTree.push(last);
} else {
blockStack.push(ast);
}
} else {
// {{ 变量
let text = html.slice(startIndex + 2, endIndex).trim();
// 切割var字串,看有没有过滤器的存在。
let tmpVarArr = text.split("|");
// 过滤器函数字符串
let f = "";
// 变量字串
let varStr = tmpVarArr.shift().trim();
if (tmpVarArr.length) {
tmpVarArr.forEach((item, idx) => {
let f_ = item.trim(); // 过滤器函数名
f = f ? `_f(${f_}, ${f})` : `_f(${f_}, ${varStr})`;
});
}
let ast = {
type: 2,
item: `{{${text}}}`,
expression: f || `_s(${varStr})`
};
if (blockStack.length) {
if (!blockStack[blockStack.length - 1].children) {
blockStack[blockStack.length - 1].children = [];
}
blockStack[blockStack.length - 1].children.push(ast);
} else {
astTree.push(ast);
}
}
return parse(html.slice(endIndex + 2));
}
// 处理ast 语法,将其转化成html字串
function generate() {
let html = "";
astTree.forEach(item => {
if (item.type === 1) {
html += item.text;
} else if (item.type === 2) {
html += genVar(item);
} else if (item.type === 3) {
if (item.block.includes("if")) {
html += genIf(item, param);
} else {
html += genFor(item, param);
}
}
});
return html;
}
// 处理if 语句
function genIf(ast, scope = param) {
let str = "";
let result = new Function("scope", `with(scope) { return ${ast.item} }`)(
scope
);
// 旧版本写法
// let varName = ast.item.split(/[=><]/g)[0].trim();
// let result = new Function(varName, "return " + ast.item)(scope[varName]);
result = ast.block === "if" ? result : !result;
// 为假不进行下一步处理
if (!result) return str;
ast.children.forEach(item => {
if (item.type === 3) {
if (item.block.includes("if")) {
str += genIf(item);
} else {
str += genFor(item);
}
} else if (item.type === 2) {
str += genVar(item);
} else {
str += item.text;
}
});
return str;
}
// 处理变量函数
function genVar(ast, scope = param) {
let expression = ast["expression"];
return new Function(
"scope",
"expression",
`with(scope) { return ${expression} }`
)(scope, expression);
}
// 处理for循环
function genFor(ast, scope = param) {
let params = ast.item.split(";").map(i => i.trim());
let tmp = params[0].split("=").map(i => i.trim());
// 解析语句,拿到要用的词句
let varName = tmp[0].substring(3).trim();
let varVal = ~~tmp[1];
let condition = ~~params[1].split(/[<>=]={0}/g)[1].trim();
let way = params[2].substr(params[2].length - 2);
let str = "";
for (let i = varVal; i < condition; way === "++" ? i++ : i--) {
let scope_;
ast.children.forEach(item => {
scope_ = { ...scope, ...{ [varName]: i } };
if (item.type === 3) {
if (item.block.includes("if")) {
str += genIf(item, scope_);
} else {
str += genFor(item, scope_);
}
} else if (item.type === 2) {
str += genVar(item, scope_);
} else {
str += item.text;
}
});
}
return str;
}
parse(html);
// console.log(astTree);
// let htmlStr = generate(astTree);
body.innerHTML = generate(astTree);
}
function replaceTag(tag) {
return tagsToReplace[tag] || tag;
}
})(window);