UNPKG

3.25 kBJavaScriptView Raw
1var _ = require('lodash');
2var entities = require('html-entities');
3
4var parse = require('./parse-js');
5var constructEvalFunction = require('./construct-eval-function');
6
7var htmlEntities = new entities.AllHtmlEntities();
8var parseJS = parse.parseJS;
9var tryToParseJS = parse.tryToParseJS;
10
11var transformTextNodes = module.exports = function (DOM, variables, exclude, options) {
12 var newDOM = [];
13
14 _.forEach(DOM, function (node) {
15 var name = node.name;
16 var attrs = node.attrs || {};
17 var children = node.children;
18 var value = node.value;
19 var isDEach = name === 'd-each';
20 var excludeLocal = {};
21
22 if (isDEach) {
23 excludeLocal[attrs.item || '$item'] = true;
24 excludeLocal[attrs.index || '$index'] = true;
25 }
26
27 if (node.attrs) {
28 node.attrs = _.mapValues(attrs, function (value, attr) {
29 if (value === '') {
30 return true;
31 }
32
33 if (value[0] !== '{' || value[value.length - 1] !== '}') {
34 return htmlEntities.decode(value);
35 }
36
37 var parsed = parseJS(value.slice(1, -1), options);
38 var isUID = attr === 'uid';
39 var usedVariables = {};
40
41 _.forEach(parsed.vars, function (value, variable) {
42 if (isDEach && isUID && excludeLocal[variable]) {
43 return;
44 }
45
46 if (exclude[variable]) {
47 return;
48 }
49
50 usedVariables[variable] = value;
51 });
52
53 _.assign(variables, usedVariables);
54
55 var evalFunction = constructEvalFunction(parsed.js);
56
57 if (!options.keepOriginal) {
58 return evalFunction;
59 }
60
61 return {
62 original: parsed.original,
63 function: evalFunction
64 };
65 });
66 }
67
68 if (name !== '#text') {
69 _.assign(exclude, excludeLocal);
70
71 if (children) {
72 node.children = transformTextNodes(children, variables, exclude, options);
73 }
74
75 newDOM.push(node);
76
77 return;
78 }
79
80 while (value.length) {
81 var match = value.match(/\{/);
82
83 if (!match) {
84 newDOM.push({
85 name: '#text',
86 value: htmlEntities.decode(value)
87 });
88
89 break;
90 }
91
92 var index = match.index;
93
94 if (index) {
95 newDOM.push({
96 name: '#text',
97 value: htmlEntities.decode(value.slice(0, index))
98 });
99 value = value.slice(index);
100 }
101
102 var parsed = tryToParseJS(value.slice(1), options);
103
104 if (!parsed) {
105 newDOM.push({
106 name: '#text',
107 value: htmlEntities.decode(value)
108 });
109
110 break;
111 }
112
113 var usedVariables = {};
114
115 _.forEach(parsed.vars, function (value, variable) {
116 if (!exclude[variable]) {
117 usedVariables[variable] = value;
118 }
119 });
120
121 _.assign(variables, usedVariables);
122
123 var evalFunction = constructEvalFunction(parsed.js);
124 var textNodeValue;
125
126 if (!options.keepOriginal) {
127 textNodeValue = evalFunction;
128 } else {
129 textNodeValue = {
130 original: parsed.original,
131 function: evalFunction
132 };
133 }
134
135 newDOM.push({
136 name: '#text',
137 value: textNodeValue
138 });
139 value = parsed.rest;
140 }
141 });
142
143 return newDOM;
144};
145
\No newline at end of file