mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-18 05:50:43 -07:00
coverage
This commit is contained in:
+2
-2
@@ -2,7 +2,7 @@ function TokenizationError(message, input, line) {
|
||||
Error.captureStackTrace(this, this.constructor);
|
||||
this.name = this.constructor.name;
|
||||
|
||||
this.message = message || "";
|
||||
this.message = message;
|
||||
this.input = input;
|
||||
this.line = line;
|
||||
}
|
||||
@@ -14,7 +14,7 @@ function ParseError(message, input, line, e) {
|
||||
this.name = this.constructor.name;
|
||||
this.originalError = e;
|
||||
|
||||
this.message = message || "";
|
||||
this.message = message;
|
||||
this.input = input;
|
||||
this.line = line;
|
||||
}
|
||||
|
||||
+31
-57
@@ -4,7 +4,7 @@ const Promise = require('any-promise');
|
||||
var render = {
|
||||
|
||||
renderTemplates: function(templates, scope, opts) {
|
||||
if(!scope) throw new Error('unable to evalTemplates: scope undefined');
|
||||
if (!scope) throw new Error('unable to evalTemplates: scope undefined');
|
||||
opts = opts || {};
|
||||
opts.strict_filters = opts.strict_filters || false;
|
||||
|
||||
@@ -15,80 +15,54 @@ var render = {
|
||||
// emptyPromise.then(renderTag(template0).then(renderTag(template1).then(renderTag(template2)...
|
||||
var lastPromise = templates.reduce((promise, template) => {
|
||||
return promise.then(() => {
|
||||
if (scope.safeGet('forloop.skip')) {
|
||||
return Promise.resolve('');
|
||||
}
|
||||
if (scope.safeGet('forloop.stop')) {
|
||||
throw new Error('forloop.stop'); // this will stop/break the sequential promise chain and go to the catch
|
||||
}
|
||||
var promiseLink = Promise.resolve('');
|
||||
switch (template.type) {
|
||||
case 'tag':
|
||||
// Add Promises to the chain
|
||||
promiseLink = this.renderTag(template, scope, this.register)
|
||||
.then((partial) => {
|
||||
if (partial === undefined) {
|
||||
return true; // basically a noop (do nothing)
|
||||
}
|
||||
return html += partial;
|
||||
});
|
||||
break;
|
||||
case 'html':
|
||||
promiseLink = Promise.resolve(template.value)
|
||||
.then((partial) => {
|
||||
return html += partial;
|
||||
});
|
||||
break;
|
||||
case 'output':
|
||||
var val = this.evalOutput(template, scope, opts);
|
||||
promiseLink = Promise.resolve(val === undefined ? '' : stringify(val))
|
||||
.then((partial) => {
|
||||
return html += partial;
|
||||
});
|
||||
break;
|
||||
}
|
||||
|
||||
var promiseLink = Promise.resolve('');
|
||||
switch (template.type) {
|
||||
case 'tag':
|
||||
// Add Promises to the chain
|
||||
promiseLink = this.renderTag(template, scope, this.register)
|
||||
.then((partial) => {
|
||||
if (partial === undefined) {
|
||||
return true; // basically a noop (do nothing)
|
||||
}
|
||||
return html += partial;
|
||||
});
|
||||
break;
|
||||
case 'html':
|
||||
promiseLink = Promise.resolve(template.value)
|
||||
.then((partial) => {
|
||||
return html += partial;
|
||||
});
|
||||
break;
|
||||
case 'output':
|
||||
var val = this.evalOutput(template, scope, opts);
|
||||
promiseLink = Promise.resolve(val === undefined ? '' : stringify(val))
|
||||
.then((partial) => {
|
||||
return html += partial;
|
||||
});
|
||||
break;
|
||||
}
|
||||
|
||||
return promiseLink;
|
||||
})
|
||||
.catch((error) => {
|
||||
if (error.message === 'forloop.skip') {
|
||||
// the error is a controlled, purposeful stop. so just return the html that we have up to this point
|
||||
return html;
|
||||
} else {
|
||||
// rethrow actual error
|
||||
throw error;
|
||||
}
|
||||
});
|
||||
return promiseLink;
|
||||
});
|
||||
}, Promise.resolve('')); // start the reduce chain with a resolved Promise. After first run, the "promise" argument
|
||||
// in our reduce callback will be the returned promise from our "then" above. In this
|
||||
// case, that's the promise returned from this.renderTag or a resolved promise with raw html.
|
||||
|
||||
return lastPromise
|
||||
.then((renderedHtml) => {
|
||||
return renderedHtml;
|
||||
})
|
||||
.catch((error) => {
|
||||
throw error;
|
||||
});
|
||||
|
||||
return lastPromise;
|
||||
},
|
||||
|
||||
renderTag: function(template, scope, register) {
|
||||
if (template.name === 'continue') {
|
||||
scope.set('forloop.skip', true);
|
||||
return Promise.resolve('');
|
||||
}
|
||||
if (template.name === 'break') {
|
||||
scope.set('forloop.stop', true);
|
||||
scope.set('forloop.skip', true);
|
||||
return Promise.reject(new Error('forloop.stop')); // this will stop the sequential promise chain
|
||||
}
|
||||
return template.render(scope, register);
|
||||
},
|
||||
|
||||
evalOutput: function(template, scope, opts) {
|
||||
if(!scope) throw new Error('unable to evalOutput: scope undefined');
|
||||
if (!scope) throw new Error('unable to evalOutput: scope undefined');
|
||||
var val = Syntax.evalExp(template.initial, scope);
|
||||
template.filters.some(filter => {
|
||||
if (filter.error) {
|
||||
|
||||
+140
-142
@@ -2,162 +2,160 @@ const _ = require('./util/underscore.js');
|
||||
const lexical = require('./lexical.js');
|
||||
|
||||
var Scope = {
|
||||
safeGet: function(str) {
|
||||
var i;
|
||||
// get all
|
||||
if (str === undefined) {
|
||||
var ctx = {};
|
||||
for (i = this.scopes.length - 1; i >= 0; i--) {
|
||||
var scp = this.scopes[i];
|
||||
for (var k in scp) {
|
||||
if (scp.hasOwnProperty(k)) {
|
||||
ctx[k] = scp[k];
|
||||
}
|
||||
}
|
||||
}
|
||||
return ctx;
|
||||
}
|
||||
// get one path
|
||||
for (i = this.scopes.length - 1; i >= 0; i--) {
|
||||
var v = this.getPropertyByPath(this.scopes[i], str);
|
||||
if (v !== undefined) return v;
|
||||
}
|
||||
},
|
||||
get: function(str) {
|
||||
var val = this.safeGet(str);
|
||||
if (val === undefined && this.opts.strict) {
|
||||
throw new Error(`[strict_variables] undefined variable: ${str}`);
|
||||
}
|
||||
return val;
|
||||
},
|
||||
set: function(k, v) {
|
||||
this.setPropertyByPath(this.scopes[this.scopes.length - 1], k, v);
|
||||
return this;
|
||||
},
|
||||
push: function(ctx) {
|
||||
if (!ctx) throw new Error(`trying to push ${ctx} into scopes`);
|
||||
return this.scopes.push(ctx);
|
||||
},
|
||||
pop: function() {
|
||||
return this.scopes.pop();
|
||||
},
|
||||
safeGet: function(str) {
|
||||
var i;
|
||||
// get all
|
||||
if (str === undefined) {
|
||||
var ctx = {};
|
||||
for (i = this.scopes.length - 1; i >= 0; i--) {
|
||||
var scp = this.scopes[i];
|
||||
for (var k in scp) {
|
||||
if (scp.hasOwnProperty(k)) {
|
||||
ctx[k] = scp[k];
|
||||
}
|
||||
}
|
||||
}
|
||||
return ctx;
|
||||
}
|
||||
// get one path
|
||||
for (i = this.scopes.length - 1; i >= 0; i--) {
|
||||
var v = this.getPropertyByPath(this.scopes[i], str);
|
||||
if (v !== undefined) return v;
|
||||
}
|
||||
},
|
||||
get: function(str) {
|
||||
var val = this.safeGet(str);
|
||||
if (val === undefined && this.opts.strict) {
|
||||
throw new Error(`[strict_variables] undefined variable: ${str}`);
|
||||
}
|
||||
return val;
|
||||
},
|
||||
set: function(k, v) {
|
||||
this.setPropertyByPath(this.scopes[this.scopes.length - 1], k, v);
|
||||
return this;
|
||||
},
|
||||
push: function(ctx) {
|
||||
if (!ctx) throw new Error(`trying to push ${ctx} into scopes`);
|
||||
return this.scopes.push(ctx);
|
||||
},
|
||||
pop: function() {
|
||||
return this.scopes.pop();
|
||||
},
|
||||
unshift: function(ctx) {
|
||||
if (!ctx) throw new Error('trying to push $(ctx) into scopes');
|
||||
if (!ctx) throw new Error(`trying to push ${ctx} into scopes`);
|
||||
return this.scopes.unshift(ctx);
|
||||
},
|
||||
shift: function() {
|
||||
return this.scopes.shift();
|
||||
},
|
||||
setPropertyByPath: function(obj, path, val) {
|
||||
if (_.isString(path)) {
|
||||
var paths = path.replace(/\[/g, '.').replace(/\]/g, '').split('.');
|
||||
for (var i = 0; i < paths.length; i++) {
|
||||
var key = paths[i];
|
||||
if (i === paths.length - 1) {
|
||||
return obj[key] = val;
|
||||
}
|
||||
if (undefined === obj[key]) obj[key] = {};
|
||||
// case for readonly objects
|
||||
obj = obj[key] || {};
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
return obj[path] = val;
|
||||
},
|
||||
setPropertyByPath: function(obj, path, val) {
|
||||
if (_.isString(path)) {
|
||||
var paths = path.replace(/\[/g, '.').replace(/\]/g, '').split('.');
|
||||
for (var i = 0; i < paths.length; i++) {
|
||||
var key = paths[i];
|
||||
if (i === paths.length - 1) {
|
||||
return obj[key] = val;
|
||||
}
|
||||
if (undefined === obj[key]) obj[key] = {};
|
||||
// case for readonly objects
|
||||
obj = obj[key] || {};
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
getPropertyByPath: function(obj, path) {
|
||||
if (_.isString(path) && path.length) {
|
||||
var paths = this.propertyAccessSeq(path);
|
||||
paths.forEach(p => obj = obj && obj[p]);
|
||||
return obj;
|
||||
}
|
||||
return obj[path];
|
||||
},
|
||||
getPropertyByPath: function(obj, path) {
|
||||
if (_.isString(path) && path.length) {
|
||||
var paths = this.propertyAccessSeq(path);
|
||||
paths.forEach(p => obj = obj && obj[p]);
|
||||
return obj;
|
||||
}
|
||||
return obj[path];
|
||||
},
|
||||
|
||||
/*
|
||||
* Parse property access sequence from access string
|
||||
* @example
|
||||
* accessSeq("foo.bar") // ['foo', 'bar']
|
||||
* accessSeq("foo['bar']") // ['foo', 'bar']
|
||||
* accessSeq("foo['b]r']") // ['foo', 'b]r']
|
||||
* accessSeq("foo[bar.coo]") // ['foo', 'bar'], for bar.coo == 'bar'
|
||||
*/
|
||||
propertyAccessSeq: function(str) {
|
||||
var seq = [],
|
||||
name = '';
|
||||
for (var i = 0; i < str.length; i++) {
|
||||
if (str[i] === '[') {
|
||||
seq.push(name);
|
||||
name = '';
|
||||
/*
|
||||
* Parse property access sequence from access string
|
||||
* @example
|
||||
* accessSeq("foo.bar") // ['foo', 'bar']
|
||||
* accessSeq("foo['bar']") // ['foo', 'bar']
|
||||
* accessSeq("foo['b]r']") // ['foo', 'b]r']
|
||||
* accessSeq("foo[bar.coo]") // ['foo', 'bar'], for bar.coo == 'bar'
|
||||
*/
|
||||
propertyAccessSeq: function(str) {
|
||||
var seq = [],
|
||||
name = '';
|
||||
for (var i = 0; i < str.length; i++) {
|
||||
if (str[i] === '[') {
|
||||
seq.push(name);
|
||||
name = '';
|
||||
|
||||
var delemiter = str[i + 1];
|
||||
// foo[bar.coo]
|
||||
if (delemiter !== "'" && delemiter !== '"') {
|
||||
var j = matchRightBracket(str, i + 1);
|
||||
if (j === -1) {
|
||||
throw new Error(`unbalanced []: ${str}`);
|
||||
}
|
||||
name = str.slice(i + 1, j);
|
||||
// foo[1]
|
||||
if(lexical.isInteger(name)){
|
||||
seq.push(name);
|
||||
}
|
||||
// foo["bar"]
|
||||
else{
|
||||
seq.push(this.get(name));
|
||||
}
|
||||
name = '';
|
||||
i = j;
|
||||
}
|
||||
// foo["bar"]
|
||||
else {
|
||||
var j = str.indexOf(delemiter, i + 2);
|
||||
if (j === -1) {
|
||||
throw new Error(`unbalanced ${delemiter}: ${str}`);
|
||||
}
|
||||
name = str.slice(i + 2, j);
|
||||
seq.push(name);
|
||||
name = '';
|
||||
i = j + 1;
|
||||
}
|
||||
}
|
||||
// foo.bar
|
||||
else if (str[i] === ".") {
|
||||
seq.push(name);
|
||||
name = '';
|
||||
}
|
||||
//foo.bar
|
||||
else {
|
||||
name += str[i];
|
||||
}
|
||||
}
|
||||
if (name.length) seq.push(name);
|
||||
return seq;
|
||||
}
|
||||
var delemiter = str[i + 1];
|
||||
// foo[bar.coo]
|
||||
if (delemiter !== "'" && delemiter !== '"') {
|
||||
var j = matchRightBracket(str, i + 1);
|
||||
if (j === -1) {
|
||||
throw new Error(`unbalanced []: ${str}`);
|
||||
}
|
||||
name = str.slice(i + 1, j);
|
||||
// foo[1]
|
||||
if(lexical.isInteger(name)){
|
||||
seq.push(name);
|
||||
}
|
||||
// foo["bar"]
|
||||
else{
|
||||
seq.push(this.get(name));
|
||||
}
|
||||
name = '';
|
||||
i = j;
|
||||
}
|
||||
// foo["bar"]
|
||||
else {
|
||||
var j = str.indexOf(delemiter, i + 2);
|
||||
if (j === -1) {
|
||||
throw new Error(`unbalanced ${delemiter}: ${str}`);
|
||||
}
|
||||
name = str.slice(i + 2, j);
|
||||
seq.push(name);
|
||||
name = '';
|
||||
i = j + 1;
|
||||
}
|
||||
}
|
||||
// foo.bar
|
||||
else if (str[i] === ".") {
|
||||
seq.push(name);
|
||||
name = '';
|
||||
}
|
||||
//foo.bar
|
||||
else {
|
||||
name += str[i];
|
||||
}
|
||||
}
|
||||
if (name.length) seq.push(name);
|
||||
return seq;
|
||||
}
|
||||
};
|
||||
|
||||
function matchRightBracket(str, begin) {
|
||||
var stack = 1; // count of '[' - count of ']'
|
||||
for (var i = begin; i < str.length; i++) {
|
||||
if (str[i] === '[') {
|
||||
stack++;
|
||||
}
|
||||
if (str[i] === ']') {
|
||||
stack--;
|
||||
if (stack === 0) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
var stack = 1; // count of '[' - count of ']'
|
||||
for (var i = begin; i < str.length; i++) {
|
||||
if (str[i] === '[') {
|
||||
stack++;
|
||||
}
|
||||
if (str[i] === ']') {
|
||||
stack--;
|
||||
if (stack === 0) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
exports.factory = function(_ctx, opts) {
|
||||
opts = opts || {};
|
||||
opts.strict = opts.strict || false;
|
||||
opts = opts || {};
|
||||
opts.strict = opts.strict || false;
|
||||
|
||||
var scope = Object.create(Scope);
|
||||
scope.opts = opts;
|
||||
scope.scopes = [_ctx || {}];
|
||||
return scope;
|
||||
var scope = Object.create(Scope);
|
||||
scope.opts = opts;
|
||||
scope.scopes = [_ctx || {}];
|
||||
return scope;
|
||||
};
|
||||
|
||||
+4
-1
@@ -1,9 +1,12 @@
|
||||
const lexical = require('./lexical.js');
|
||||
const TokenizationError = require('./error.js').TokenizationError;
|
||||
const _ = require('./util/underscore.js');
|
||||
|
||||
function parse(html) {
|
||||
var tokens = [];
|
||||
if (!html) return tokens;
|
||||
if (!_.isString(html)) {
|
||||
throw new TokenizationError('illegal input type');
|
||||
}
|
||||
|
||||
var syntax = /({%(.*?)%})|({{(.*?)}})/g;
|
||||
var result, htmlFragment, token;
|
||||
|
||||
Reference in New Issue
Block a user