Skip to content

Commit da4018e

Browse files
author
Alvin
committed
tries fix from https://github.com/stevage/json-editor/blob/0544a03bc6dd5f2ba4528d0f08787de7660788ed/src/templates/default.js instead of the one that is reported to break constants in enumSource
1 parent 065cf8b commit da4018e

File tree

1 file changed

+25
-48
lines changed

1 file changed

+25
-48
lines changed

src/templates/default.js

Lines changed: 25 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,34 @@
11
JSONEditor.defaults.templates["default"] = function() {
2+
function resolve(ref, context) {
3+
var dot = ref.indexOf('.');
4+
if (dot === -1)
5+
return context[ref];
6+
var predot = ref.slice(0, dot);
7+
if (!predot)
8+
return null;
9+
10+
if (context[predot] === undefined)
11+
return null;
12+
13+
return resolve(ref.slice(dot + 1), context[predot]);
14+
}
15+
216
return {
317
compile: function(template) {
4-
var matches = template.match(/{{\s*([a-zA-Z0-9\-_ \.]+)\s*}}/g);
5-
var l = matches && matches.length;
6-
7-
// Shortcut if the template contains no variables
8-
if(!l) return function() { return template; };
9-
10-
// Pre-compute the search/replace functions
11-
// This drastically speeds up template execution
12-
var replacements = [];
13-
var get_replacement = function(i) {
14-
var p = matches[i].replace(/[{}]+/g,'').trim().split('.');
15-
var n = p.length;
16-
var func;
17-
18-
if(n > 1) {
19-
var cur;
20-
func = function(vars) {
21-
cur = vars;
22-
for(i=0; i<n; i++) {
23-
cur = cur[p[i]];
24-
if(!cur) break;
25-
}
26-
return cur;
27-
};
28-
}
29-
else {
30-
p = p[0];
31-
func = function(vars) {
32-
return vars[p];
33-
};
34-
}
35-
36-
replacements.push({
37-
s: matches[i],
38-
r: func
39-
});
40-
};
41-
for(var i=0; i<l; i++) {
42-
get_replacement(i);
43-
}
44-
45-
// The compiled function
46-
return function(vars) {
18+
return function (vars) {
4719
var ret = template+"";
48-
var r;
49-
for(i=0; i<l; i++) {
50-
r = replacements[i];
51-
ret = ret.replace(r.s, r.r(vars));
20+
var re = /{{\s*([a-zA-Z0-9_.\-]+)\s*}}/g;
21+
var m = re.exec(ret);
22+
while (m) {
23+
var t = resolve(m[1], vars);
24+
if (t) {
25+
ret = ret.replace(m[0], t);
26+
re.lastIndex += (t.length - m[0].length); // handle short substitutions
27+
}
28+
m = re.exec(ret);
5229
}
5330
return ret;
5431
};
5532
}
5633
};
57-
};
34+
};

0 commit comments

Comments
 (0)