vue-blox-mustache
Version:
Adds mustache template support to vue-blox.
331 lines (330 loc) • 11.5 kB
JavaScript
var ee = typeof globalThis < "u" ? globalThis : typeof window < "u" ? window : typeof global < "u" ? global : typeof self < "u" ? self : {}, $ = {}, re = {
get exports() {
return $;
},
set exports(M) {
$ = M;
}
};
(function(M, l) {
(function(E, y) {
M.exports = y();
})(ee, function() {
/*!
* mustache.js - Logic-less {{mustache}} templates with JavaScript
* http://github.com/janl/mustache.js
*/
var E = Object.prototype.toString, y = Array.isArray || function(e) {
return E.call(e) === "[object Array]";
};
function U(n) {
return typeof n == "function";
}
function h(n) {
return y(n) ? "array" : typeof n;
}
function g(n) {
return n.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, "\\$&");
}
function R(n, e) {
return n != null && typeof n == "object" && e in n;
}
function x(n, e) {
return n != null && typeof n != "object" && n.hasOwnProperty && n.hasOwnProperty(e);
}
var q = RegExp.prototype.test;
function V(n, e) {
return q.call(n, e);
}
var F = /\S/;
function D(n) {
return !V(F, n);
}
var G = {
"&": "&",
"<": "<",
">": ">",
'"': """,
"'": "'",
"/": "/",
"`": "`",
"=": "="
};
function H(n) {
return String(n).replace(/[&<>"'`=\/]/g, function(r) {
return G[r];
});
}
var z = /\s*/, J = /\s+/, K = /\s*=/, L = /\s*\}/, Q = /#|\^|\/|>|\{|&|=|!/;
function X(n, e) {
if (!n)
return [];
var r = !1, t = [], s = [], a = [], i = !1, o = !1, u = "", c = 0;
function d() {
if (i && !o)
for (; a.length; )
delete s[a.pop()];
else
a = [];
i = !1, o = !1;
}
var C, T, N;
function B(k) {
if (typeof k == "string" && (k = k.split(J, 2)), !y(k) || k.length !== 2)
throw new Error("Invalid tags: " + k);
C = new RegExp(g(k[0]) + "\\s*"), T = new RegExp("\\s*" + g(k[1])), N = new RegExp("\\s*" + g("}" + k[1]));
}
B(e || w.tags);
for (var f = new A(n), S, v, b, O, W, P; !f.eos(); ) {
if (S = f.pos, b = f.scanUntil(C), b)
for (var _ = 0, m = b.length; _ < m; ++_)
O = b.charAt(_), D(O) ? (a.push(s.length), u += O) : (o = !0, r = !0, u += " "), s.push(["text", O, S, S + 1]), S += 1, O === `
` && (d(), u = "", c = 0, r = !1);
if (!f.scan(C))
break;
if (i = !0, v = f.scan(Q) || "name", f.scan(z), v === "=" ? (b = f.scanUntil(K), f.scan(K), f.scanUntil(T)) : v === "{" ? (b = f.scanUntil(N), f.scan(L), f.scanUntil(T), v = "&") : b = f.scanUntil(T), !f.scan(T))
throw new Error("Unclosed tag at " + f.pos);
if (v == ">" ? W = [v, b, S, f.pos, u, c, r] : W = [v, b, S, f.pos], c++, s.push(W), v === "#" || v === "^")
t.push(W);
else if (v === "/") {
if (P = t.pop(), !P)
throw new Error('Unopened section "' + b + '" at ' + S);
if (P[1] !== b)
throw new Error('Unclosed section "' + P[1] + '" at ' + S);
} else
v === "name" || v === "{" || v === "&" ? o = !0 : v === "=" && B(b);
}
if (d(), P = t.pop(), P)
throw new Error('Unclosed section "' + P[1] + '" at ' + f.pos);
return Z(Y(s));
}
function Y(n) {
for (var e = [], r, t, s = 0, a = n.length; s < a; ++s)
r = n[s], r && (r[0] === "text" && t && t[0] === "text" ? (t[1] += r[1], t[3] = r[3]) : (e.push(r), t = r));
return e;
}
function Z(n) {
for (var e = [], r = e, t = [], s, a, i = 0, o = n.length; i < o; ++i)
switch (s = n[i], s[0]) {
case "#":
case "^":
r.push(s), t.push(s), r = s[4] = [];
break;
case "/":
a = t.pop(), a[5] = s[2], r = t.length > 0 ? t[t.length - 1][4] : e;
break;
default:
r.push(s);
}
return e;
}
function A(n) {
this.string = n, this.tail = n, this.pos = 0;
}
A.prototype.eos = function() {
return this.tail === "";
}, A.prototype.scan = function(e) {
var r = this.tail.match(e);
if (!r || r.index !== 0)
return "";
var t = r[0];
return this.tail = this.tail.substring(t.length), this.pos += t.length, t;
}, A.prototype.scanUntil = function(e) {
var r = this.tail.search(e), t;
switch (r) {
case -1:
t = this.tail, this.tail = "";
break;
case 0:
t = "";
break;
default:
t = this.tail.substring(0, r), this.tail = this.tail.substring(r);
}
return this.pos += t.length, t;
};
function j(n, e) {
this.view = n, this.cache = { ".": this.view }, this.parent = e;
}
j.prototype.push = function(e) {
return new j(e, this);
}, j.prototype.lookup = function(e) {
var r = this.cache, t;
if (r.hasOwnProperty(e))
t = r[e];
else {
for (var s = this, a, i, o, u = !1; s; ) {
if (e.indexOf(".") > 0)
for (a = s.view, i = e.split("."), o = 0; a != null && o < i.length; )
o === i.length - 1 && (u = R(a, i[o]) || x(a, i[o])), a = a[i[o++]];
else
a = s.view[e], u = R(s.view, e);
if (u) {
t = a;
break;
}
s = s.parent;
}
r[e] = t;
}
return U(t) && (t = t.call(this.view)), t;
};
function p() {
this.templateCache = {
_cache: {},
set: function(e, r) {
this._cache[e] = r;
},
get: function(e) {
return this._cache[e];
},
clear: function() {
this._cache = {};
}
};
}
p.prototype.clearCache = function() {
typeof this.templateCache < "u" && this.templateCache.clear();
}, p.prototype.parse = function(e, r) {
var t = this.templateCache, s = e + ":" + (r || w.tags).join(":"), a = typeof t < "u", i = a ? t.get(s) : void 0;
return i == null && (i = X(e, r), a && t.set(s, i)), i;
}, p.prototype.render = function(e, r, t, s) {
var a = this.getConfigTags(s), i = this.parse(e, a), o = r instanceof j ? r : new j(r, void 0);
return this.renderTokens(i, o, t, e, s);
}, p.prototype.renderTokens = function(e, r, t, s, a) {
for (var i = "", o, u, c, d = 0, C = e.length; d < C; ++d)
c = void 0, o = e[d], u = o[0], u === "#" ? c = this.renderSection(o, r, t, s, a) : u === "^" ? c = this.renderInverted(o, r, t, s, a) : u === ">" ? c = this.renderPartial(o, r, t, a) : u === "&" ? c = this.unescapedValue(o, r) : u === "name" ? c = this.escapedValue(o, r, a) : u === "text" && (c = this.rawValue(o)), c !== void 0 && (i += c);
return i;
}, p.prototype.renderSection = function(e, r, t, s, a) {
var i = this, o = "", u = r.lookup(e[1]);
function c(T) {
return i.render(T, r, t, a);
}
if (u) {
if (y(u))
for (var d = 0, C = u.length; d < C; ++d)
o += this.renderTokens(e[4], r.push(u[d]), t, s, a);
else if (typeof u == "object" || typeof u == "string" || typeof u == "number")
o += this.renderTokens(e[4], r.push(u), t, s, a);
else if (U(u)) {
if (typeof s != "string")
throw new Error("Cannot use higher-order sections without the original template");
u = u.call(r.view, s.slice(e[3], e[5]), c), u != null && (o += u);
} else
o += this.renderTokens(e[4], r, t, s, a);
return o;
}
}, p.prototype.renderInverted = function(e, r, t, s, a) {
var i = r.lookup(e[1]);
if (!i || y(i) && i.length === 0)
return this.renderTokens(e[4], r, t, s, a);
}, p.prototype.indentPartial = function(e, r, t) {
for (var s = r.replace(/[^ \t]/g, ""), a = e.split(`
`), i = 0; i < a.length; i++)
a[i].length && (i > 0 || !t) && (a[i] = s + a[i]);
return a.join(`
`);
}, p.prototype.renderPartial = function(e, r, t, s) {
if (t) {
var a = this.getConfigTags(s), i = U(t) ? t(e[1]) : t[e[1]];
if (i != null) {
var o = e[6], u = e[5], c = e[4], d = i;
u == 0 && c && (d = this.indentPartial(i, c, o));
var C = this.parse(d, a);
return this.renderTokens(C, r, t, d, s);
}
}
}, p.prototype.unescapedValue = function(e, r) {
var t = r.lookup(e[1]);
if (t != null)
return t;
}, p.prototype.escapedValue = function(e, r, t) {
var s = this.getConfigEscape(t) || w.escape, a = r.lookup(e[1]);
if (a != null)
return typeof a == "number" && s === w.escape ? String(a) : s(a);
}, p.prototype.rawValue = function(e) {
return e[1];
}, p.prototype.getConfigTags = function(e) {
return y(e) ? e : e && typeof e == "object" ? e.tags : void 0;
}, p.prototype.getConfigEscape = function(e) {
if (e && typeof e == "object" && !y(e))
return e.escape;
};
var w = {
name: "mustache.js",
version: "4.1.0",
tags: ["{{", "}}"],
clearCache: void 0,
escape: void 0,
parse: void 0,
render: void 0,
Scanner: void 0,
Context: void 0,
Writer: void 0,
/**
* Allows a user to override the default caching strategy, by providing an
* object with set, get and clear methods. This can also be used to disable
* the cache by setting it to the literal `undefined`.
*/
set templateCache(n) {
I.templateCache = n;
},
/**
* Gets the default or overridden caching object from the default writer.
*/
get templateCache() {
return I.templateCache;
}
}, I = new p();
return w.clearCache = function() {
return I.clearCache();
}, w.parse = function(e, r) {
return I.parse(e, r);
}, w.render = function(e, r, t, s) {
if (typeof e != "string")
throw new TypeError('Invalid template! Template should be a "string" but "' + h(e) + '" was given as the first argument for mustache#render(template, view, partials)');
return I.render(e, r, t, s);
}, w.escape = H, w.Scanner = A, w.Context = j, w.Writer = p, w;
});
})(re);
class te {
run({ context: l, key: E, value: y, variables: U, buildContext: h }) {
const g = this.evaluate(y, U, 0, 10);
l.setProp({
propName: E,
value: g
});
}
evaluate(l, E, y = 0, U = 10) {
if (typeof l == "string" && l.includes("{{") && l.includes("}}"))
try {
let h = $.render(l, E);
return h.includes("{{") && y < 10 && (h = this.evaluate(h, E, y + 1)), h;
} catch (h) {
throw new Error(`The call to runMustache() for value ${l} threw the error: ${h}`);
}
else if (typeof l == "object" && Array.isArray(l)) {
const h = [];
for (let g = 0; g < l.length; g += 1) {
const R = l[g], x = this.evaluate(R, E, 0);
h.push(x);
}
return h;
} else if (typeof l == "object") {
const h = {}, g = Object.keys(l);
for (let R = 0; R < g.length; R += 1) {
const x = g[R], q = l[x], V = this.evaluate(q, E, 0);
h[x] = V;
}
return h;
}
return l;
}
}
function ne() {
return new te();
}
export {
te as BloxPluginMustache,
ne as getPluginMustache
};