-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathindex.js
173 lines (160 loc) · 4.97 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
'use strict';
require('./dom_parser.js')
function XML2DataTree(root, context, newRoot, layerOps, layerIdx) {
if (!newRoot)
newRoot = { tag: 'body', children: [] };
else if (!newRoot.children)
throw new Error('cannot add child to leaf node');
if (!root.attributes && root.nodeType == 3) {
var newNode = parseTextNode(root, context);
} else {
var newNode = parseElementNode(root, context, layerOps, layerIdx);
}
if (newNode == null)
return;
if (newNode.tag !== 'wx-block') {
if (newNode.items && newNode.array && newNode.array.length)
newNode.array.forEach(n => newRoot.children.push(n));
else
newRoot.children.push(newNode);
}
var newLayerOps = [];
var newLayerIdx = 0;
if (newNode.items && newNode.array && newNode.array.length) {
newNode.array.forEach((n, idx) => {
n = n.tag === 'wx-block' ? newRoot : n;
var newContext = JSON.parse(JSON.stringify(context));
newContext[newNode.itemName] = newNode.items[idx];
newContext[newNode.indexName] = idx;
for (var i = 0; i < root.childNodes.length; ++i) {
var child = root.childNodes[i];
if (child.nodeType != 1 && child.nodeType != 3)
continue;
if (child.nodeType == 3 && !child.nodeValue.trim().length)
continue;
XML2DataTree(child, newContext, n, newLayerOps, newLayerIdx++);
}
});
} else {
newNode = newNode.tag === 'wx-block' ? newRoot : newNode;
for (var i = 0; i < root.childNodes.length; ++i) {
var child = root.childNodes[i];
if (child.nodeType != 1 && child.nodeType != 3)
continue;
if (child.nodeType == 3 && !child.nodeValue.trim().length)
continue;
XML2DataTree(child, context, newNode, newLayerOps, newLayerIdx++);
}
}
return newRoot;
}
function parseTextNode(node, data) {
return stringEvaluate(node.nodeValue.trim(), data, false);
}
function parseElementNode(node, data, layerOps, layerIdx) {
var ret = {};
ret.tag = 'wx-' + (node.tagName == 'IMG' ? 'image' : node.tagName.toLowerCase());
ret.attr = {};
ret.children = [];
for (var i = 0; i < node.attributes.length; ++i) {
var attr = node.attributes[i];
var value = stringEvaluate(attr.value.trim(), data, attr.name === 'data');
var name = attr.name;
if (name.split('-').length > 1)
name = name.split('-')
.map((p, i) => i != 0 ? p[0].toUpperCase() + p.substring(1) : p)
.join('');
if (ret.items && ret.array) {
ret.array.forEach(c => c.attr[name] = value);
} else {
ret.attr[name] = value;
}
//if (name.startsWith('wx:')) {
if (name.substring(0, 3) === 'wx:') {
ret = processWxAttribute(name, value, ret, layerOps, layerIdx);
if (!ret)
break;
}
}
return ret;
}
function processWxAttribute(name, value, node, operations, idx) {
switch (name) {
case 'wx:if':
var condition = !!value;
operations[idx] = ['if', condition];
if (condition == false)
return;
break;
case 'wx:elif':
var condition = !!value;
operations[idx] = ['elif', condition];
if (operations[idx-1] == undefined || operations[idx-1][0] != 'if')
throw new Error('no corresponding wx:if found');
if (operations[idx-1][1] == true || condition == false)
return;
break;
case 'wx:else':
if (operations[idx-1] == undefined || (operations[idx-1][0] != 'if' && operations[idx-1][0] != 'elif'))
throw new Error('no corresponding wx:if/elif found');
if (operations[idx-1][1] == true || (operations[idx-1][0] == 'elif' && operations[idx-2][1] == true))
return;
break;
case 'wx:for':
var items = Array.isArray(value) ? value : [];
delete node.attr[name];
node = {
tag: node.tag,
indexName: node.indexName ? node.indexName : 'index',
itemName: node.itemName ? node.itemName : 'item',
items: items,
array: items.map(i => { return { tag: node.tag, attr: node.attr, children: [] } })
};
break;
case 'wx:forIndex':
node.indexName = value;
if (node.items && node.array)
node.array.forEach(c => delete c.attr[name]);
else if (node.attr)
node.attr[name];
break;
case 'wx:forItem':
node.itemName = value;
if (node.items && node.array)
node.array.forEach(c => delete c.attr[name]);
else if (node.attr)
node.attr[name];
break;
}
return node;
}
function stringEvaluate(str, data, toObject) {
if (str.match(/^{{[^{}]+}}$/)) {
return mustacheEvaluate(str.replace(/^{{/, '').replace(/}}$/, ''), data, toObject);
} else {
return str.replace(
/({{[^{}]+}})/g,
v => mustacheEvaluate(v.replace(/^{{/, '').replace(/}}$/, ''), data, toObject));
}
}
function mustacheEvaluate(str, data, toObject) {
var s = '(function() { ';
for (var k in data) {
s += 'var ' + k + ' = JSON.parse(\'' + JSON.stringify(data[k]) + '\');';
}
s += 'return ';
var exp = toObject ? s + '{' + str + '}})()' : s + str + '})()';
try {
var ret = eval(exp);
} catch (e) {
console.log(exp)
console.error(e)
}
return ret;
}
function parser(input, data) {
var doc = new DOMParser().parseFromString(input, 'text/html');
var tree = XML2DataTree(doc.body, data, null, []);
return tree.children[0];
}
module.exports = parser;