This commit is contained in:
harttle
2016-11-09 02:00:04 +08:00
parent 5c0209ec8b
commit 4bdc169d0f
3 changed files with 164 additions and 66 deletions
+160 -63
View File
@@ -258,7 +258,7 @@ var _engine = {
}).then(function (tpl) { }).then(function (tpl) {
return _this.render(tpl, ctx, opts); return _this.render(tpl, ctx, opts);
}).catch(function (e) { }).catch(function (e) {
if (e instanceof Errors.RenderBreak) { if (e instanceof Errors.RenderBreakError) {
return e.html; return e.html;
} }
throw e; throw e;
@@ -368,7 +368,7 @@ factory.evalValue = Syntax.evalValue;
factory.Types = { factory.Types = {
ParseError: Errors.ParseError, ParseError: Errors.ParseError,
TokenizationEroor: Errors.TokenizationError, TokenizationEroor: Errors.TokenizationError,
RenderBreak: Errors.RenderBreak, RenderBreakError: Errors.RenderBreakError,
AssertionError: Errors.AssertionError AssertionError: Errors.AssertionError
}; };
@@ -728,16 +728,19 @@ module.exports = function (Tag, Filter) {
function parseToken(token, tokens) { function parseToken(token, tokens) {
try { try {
switch (token.type) { var tpl = null;
case 'tag': if (token.type === 'tag') {
return parseTag(token, tokens); tpl = parseTag(token, tokens);
case 'output': } else if (token.type === 'output') {
return parseOutput(token.value); tpl = parseOutput(token.value);
case 'html': } else {
return token; // token.type === 'html'
tpl = token;
} }
tpl.token = token;
return tpl;
} catch (e) { } catch (e) {
throw new ParseError(e.message, token.input, token.line, e); throw new ParseError(e, token);
} }
} }
@@ -773,7 +776,10 @@ module.exports = function (Tag, Filter) {
} }
return { return {
parse: parse, parseTag: parseTag, parseStream: parseStream, parseOutput: parseOutput parse: parse,
parseTag: parseTag,
parseStream: parseStream,
parseOutput: parseOutput
}; };
}; };
@@ -783,9 +789,9 @@ module.exports = function (Tag, Filter) {
var Syntax = require('./syntax.js'); var Syntax = require('./syntax.js');
var Promise = require('any-promise'); var Promise = require('any-promise');
var mapSeries = require('./util/promise.js').mapSeries; var mapSeries = require('./util/promise.js').mapSeries;
var RenderBreak = require('./util/error.js').RenderBreak; var RenderBreakError = require('./util/error.js').RenderBreakError;
var RenderError = require('./util/error.js').RenderError;
var assert = require('./util/assert.js'); var assert = require('./util/assert.js');
var _ = require('./util/underscore.js');
var render = { var render = {
@@ -799,22 +805,27 @@ var render = {
return renderTemplate.call(_this, tpl).then(function (partial) { return renderTemplate.call(_this, tpl).then(function (partial) {
return html += partial; return html += partial;
}).catch(function (e) { }).catch(function (e) {
if (e instanceof RenderBreak) { if (e instanceof RenderBreakError) {
e.resolvedHTML = html; e.resolvedHTML = html;
}
throw e; throw e;
}
throw new RenderError(e, tpl);
}); });
}).then(function () { }).then(function () {
return html; return html;
}); });
function renderTemplate(template) { function renderTemplate(template) {
var _this2 = this;
if (template.type === 'tag') { if (template.type === 'tag') {
return this.renderTag(template, scope).then(function (partial) { return this.renderTag(template, scope).then(function (partial) {
return partial === undefined ? '' : partial; return partial === undefined ? '' : partial;
}); });
} else if (template.type === 'output') { } else if (template.type === 'output') {
return Promise.resolve(this.evalOutput(template, scope)).then(function (partial) { return Promise.resolve().then(function () {
return _this2.evalOutput(template, scope);
}).then(function (partial) {
return partial === undefined ? '' : stringify(partial); return partial === undefined ? '' : stringify(partial);
}); });
} else { } else {
@@ -826,10 +837,10 @@ var render = {
renderTag: function renderTag(template, scope) { renderTag: function renderTag(template, scope) {
if (template.name === 'continue') { if (template.name === 'continue') {
return Promise.reject(new RenderBreak('continue')); return Promise.reject(new RenderBreakError('continue'));
} }
if (template.name === 'break') { if (template.name === 'break') {
return Promise.reject(new RenderBreak('break')); return Promise.reject(new RenderBreakError('break'));
} }
return template.render(scope); return template.render(scope);
}, },
@@ -864,13 +875,12 @@ function stringify(val) {
module.exports = factory; module.exports = factory;
},{"./syntax.js":13,"./util/assert.js":16,"./util/error.js":17,"./util/promise.js":19,"./util/underscore.js":21,"any-promise":3}],12:[function(require,module,exports){ },{"./syntax.js":13,"./util/assert.js":16,"./util/error.js":17,"./util/promise.js":19,"any-promise":3}],12:[function(require,module,exports){
'use strict'; 'use strict';
var _ = require('./util/underscore.js'); var _ = require('./util/underscore.js');
var lexical = require('./lexical.js'); var lexical = require('./lexical.js');
var assert = require('./util/assert.js'); var assert = require('./util/assert.js');
var referenceError = /undefined variable|Cannot read property .* of undefined/;
var Scope = { var Scope = {
getAll: function getAll() { getAll: function getAll() {
@@ -885,7 +895,17 @@ var Scope = {
try { try {
return this.getPropertyByPath(this.scopes[i], str); return this.getPropertyByPath(this.scopes[i], str);
} catch (e) { } catch (e) {
if (!referenceError.test(e.message) || this.opts.strict_variables) { if (/undefined variable/.test(e.message)) {
continue;
}
if (/Cannot read property/.test(e.message)) {
if (this.opts.strict_variables) {
e.message += ': ' + str;
throw e;
} else {
continue;
}
} else {
e.message += ': ' + str; e.message += ': ' + str;
throw e; throw e;
} }
@@ -1097,6 +1117,7 @@ module.exports = {
'use strict'; 'use strict';
var lexical = require('./lexical.js'); var lexical = require('./lexical.js');
var _ = require('./util/underscore.js');
var Promise = require('any-promise'); var Promise = require('any-promise');
var Syntax = require('./syntax.js'); var Syntax = require('./syntax.js');
var assert = require('./util/assert.js'); var assert = require('./util/assert.js');
@@ -1119,7 +1140,19 @@ module.exports = function () {
var _tagInstance = { var _tagInstance = {
render: function render(scope) { render: function render(scope) {
var obj = hash(this.token.args, scope); var obj = hash(this.token.args, scope);
return this.tagImpl.render && this.tagImpl.render(scope, obj) || Promise.resolve(''); var impl = this.tagImpl;
if (typeof impl.render !== 'function') {
return Promise.resolve('');
}
return Promise.resolve().then(function () {
return typeof impl.render === 'function' ? impl.render(scope, obj) : '';
}).catch(function (e) {
if (_.isError(e)) {
throw e;
}
var msg = 'Please reject with an Error in ' + impl.render + ', got ' + e;
throw new Error(msg);
});
}, },
parse: function parse(token, tokens) { parse: function parse(token, tokens) {
this.type = 'tag'; this.type = 'tag';
@@ -1150,11 +1183,13 @@ module.exports = function () {
} }
return { return {
construct: construct, register: register, clear: clear construct: construct,
register: register,
clear: clear
}; };
}; };
},{"./lexical.js":8,"./syntax.js":13,"./util/assert.js":16,"any-promise":3}],15:[function(require,module,exports){ },{"./lexical.js":8,"./syntax.js":13,"./util/assert.js":16,"./util/underscore.js":21,"any-promise":3}],15:[function(require,module,exports){
'use strict'; 'use strict';
var lexical = require('./lexical.js'); var lexical = require('./lexical.js');
@@ -1163,9 +1198,9 @@ var _ = require('./util/underscore.js');
var assert = require('../src/util/assert.js'); var assert = require('../src/util/assert.js');
function parse(html) { function parse(html) {
var tokens = []; assert(_.isString(html), 'illegal input type');
assert(_.isString(html), new TokenizationError('illegal input type'));
var tokens = [];
var syntax = /({%(.*?)%})|({{(.*?)}})/g; var syntax = /({%(.*?)%})|({{(.*?)}})/g;
var result, htmlFragment, token; var result, htmlFragment, token;
var lastMatchEnd = 0, var lastMatchEnd = 0,
@@ -1188,7 +1223,7 @@ function parse(html) {
var match = token.value.match(lexical.tagLine); var match = token.value.match(lexical.tagLine);
if (!match) { if (!match) {
throw new TokenizationError('illegal tag: ' + token.raw, token.input, token.line); throw new TokenizationError('illegal tag syntax', token);
} }
token.name = match[1]; token.name = match[1];
token.args = match[2]; token.args = match[2];
@@ -1220,17 +1255,10 @@ function parse(html) {
raw: match[offset], raw: match[offset],
value: match[offset + 1].trim(), value: match[offset + 1].trim(),
line: getLineNum(match), line: getLineNum(match),
input: getLineContent(match) input: html
}; };
} }
function getLineContent(match) {
var idx1 = match.input.lastIndexOf('\n', match.index);
var idx2 = match.input.indexOf('\n', match.index);
if (idx2 === -1) idx2 = match.input.length;
return match.input.slice(idx1 + 1, idx2);
}
function getLineNum(match) { function getLineNum(match) {
var lines = match.input.slice(lastMatchBegin + 1, match.index).split('\n'); var lines = match.input.slice(lastMatchBegin + 1, match.index).split('\n');
parsedLinesCount += lines.length - 1; parsedLinesCount += lines.length - 1;
@@ -1259,44 +1287,60 @@ function assert(predicate, message) {
module.exports = assert; module.exports = assert;
},{"./error.js":17}],17:[function(require,module,exports){ },{"./error.js":17}],17:[function(require,module,exports){
"use strict"; 'use strict';
function TokenizationError(message, input, line) { var _ = require('./underscore.js');
function TokenizationError(message, token) {
if (Error.captureStackTrace) { if (Error.captureStackTrace) {
Error.captureStackTrace(this, this.constructor); Error.captureStackTrace(this, this.constructor);
} }
this.name = this.constructor.name; this.name = this.constructor.name;
this.message = message; this.input = token.input;
this.input = input; this.line = token.line;
this.line = line;
var context = mkContext(token.input, token.line);
this.message = message + '\n' + context;
} }
TokenizationError.prototype = Object.create(Error.prototype); TokenizationError.prototype = Object.create(Error.prototype);
TokenizationError.prototype.constructor = TokenizationError; TokenizationError.prototype.constructor = TokenizationError;
function ParseError(message, input, line, e) { function ParseError(e, token) {
if (Error.captureStackTrace) {
Error.captureStackTrace(this, this.constructor);
}
this.name = this.constructor.name; this.name = this.constructor.name;
this.originalError = e; this.stack = e.stack;
this.message = message; this.input = token.input;
this.input = input; this.line = token.line;
this.line = line;
var context = mkContext(token.input, token.line);
this.message = e.message + '\n' + context;
} }
ParseError.prototype = Object.create(Error.prototype); ParseError.prototype = Object.create(Error.prototype);
ParseError.prototype.constructor = ParseError; ParseError.prototype.constructor = ParseError;
function RenderBreak(message) { function RenderError(e, tpl) {
this.name = this.constructor.name;
this.stack = e.stack;
this.input = tpl.token.input;
this.line = tpl.token.line;
var context = mkContext(tpl.token.input, tpl.token.line);
this.message = e.message + '\n' + context;
}
RenderError.prototype = Object.create(Error.prototype);
RenderError.prototype.constructor = RenderError;
function RenderBreakError(message) {
if (Error.captureStackTrace) { if (Error.captureStackTrace) {
Error.captureStackTrace(this, this.constructor); Error.captureStackTrace(this, this.constructor);
} }
this.name = this.constructor.name; this.name = this.constructor.name;
this.message = message; this.message = message || '';
} }
RenderBreak.prototype = Object.create(Error.prototype); RenderBreakError.prototype = Object.create(Error.prototype);
RenderBreak.prototype.constructor = RenderBreak; RenderBreakError.prototype.constructor = RenderBreakError;
function AssertionError(message) { function AssertionError(message) {
if (Error.captureStackTrace) { if (Error.captureStackTrace) {
@@ -1308,11 +1352,34 @@ function AssertionError(message) {
AssertionError.prototype = Object.create(Error.prototype); AssertionError.prototype = Object.create(Error.prototype);
AssertionError.prototype.constructor = AssertionError; AssertionError.prototype.constructor = AssertionError;
function mkContext(input, line) {
var lines = input.split('\n');
var begin = Math.max(line - 2, 1);
var end = Math.min(line + 3, lines.length);
var context = _.range(begin, end + 1).map(function (l) {
return [l === line ? '>> ' : ' ', align(l, end), '| ', lines[l - 1]].join('');
}).join('\n');
return context;
}
function align(n, max) {
var length = (max + '').length;
var str = n + '';
var blank = Array(length - str.length).join(' ');
return blank + str;
}
module.exports = { module.exports = {
TokenizationError: TokenizationError, ParseError: ParseError, RenderBreak: RenderBreak, AssertionError: AssertionError TokenizationError: TokenizationError,
ParseError: ParseError,
RenderBreakError: RenderBreakError,
AssertionError: AssertionError,
RenderError: RenderError
}; };
},{}],18:[function(require,module,exports){ },{"./underscore.js":21}],18:[function(require,module,exports){
'use strict'; 'use strict';
var fs = require('fs'); var fs = require('fs');
@@ -1592,6 +1659,12 @@ function isString(value) {
return value instanceof String || typeof value === 'string'; return value instanceof String || typeof value === 'string';
} }
function isError(value) {
var signature = Object.prototype.toString.call(value);
// [object XXXError]
return signature.substr(-6, 5) === 'Error' || typeof value.message == 'string' && typeof value.name == 'string';
}
/* /*
* Iterates over own enumerable string keyed properties of an object and invokes iteratee for each property. * Iterates over own enumerable string keyed properties of an object and invokes iteratee for each property.
* The iteratee is invoked with three arguments: (value, key, object). * The iteratee is invoked with three arguments: (value, key, object).
@@ -1672,9 +1745,34 @@ function isObject(value) {
return value !== null && (typeof value === 'undefined' ? 'undefined' : _typeof(value)) === 'object'; return value !== null && (typeof value === 'undefined' ? 'undefined' : _typeof(value)) === 'object';
} }
/*
* A function to create flexibly-numbered lists of integers,
* handy for each and map loops. start, if omitted, defaults to 0; step defaults to 1.
* Returns a list of integers from start (inclusive) to stop (exclusive),
* incremented (or decremented) by step, exclusive.
* Note that ranges that stop before they start are considered to be zero-length instead of
* negative — if you'd like a negative range, use a negative step.
*/
function range(start, stop, step) {
if (arguments.length === 1) {
stop = start;
start = 0;
}
step = step || 1;
var arr = [];
for (var i = start; i < stop; i += step) {
arr.push(i);
}
return arr;
}
exports.isString = isString; exports.isString = isString;
exports.isArray = isArray; exports.isArray = isArray;
exports.isObject = isObject; exports.isObject = isObject;
exports.isError = isError;
exports.range = range;
exports.forOwn = forOwn; exports.forOwn = forOwn;
exports.assign = assign; exports.assign = assign;
@@ -1892,10 +1990,9 @@ module.exports = function (liquid) {
'use strict'; 'use strict';
var Liquid = require('..'); var Liquid = require('..');
var Promise = require('any-promise');
var lexical = Liquid.lexical; var lexical = Liquid.lexical;
var mapSeries = require('../src/util/promise.js').mapSeries; var mapSeries = require('../src/util/promise.js').mapSeries;
var RenderBreak = Liquid.Types.RenderBreak; var RenderBreakError = Liquid.Types.RenderBreakError;
var assert = require('../src/util/assert.js'); var assert = require('../src/util/assert.js');
var re = new RegExp('^(' + lexical.identifier.source + ')\\s+in\\s+' + ('(' + lexical.value.source + ')') + ('(?:\\s+' + lexical.hash.source + ')*') + '(?:\\s+(reversed))?$'); var re = new RegExp('^(' + lexical.identifier.source + ')\\s+in\\s+' + ('(' + lexical.value.source + ')') + ('(?:\\s+' + lexical.hash.source + ')*') + '(?:\\s+(reversed))?$');
@@ -1915,15 +2012,15 @@ module.exports = function (liquid) {
this.elseTemplates = []; this.elseTemplates = [];
var p, var p,
stream = liquid.parser.parseStream(remainTokens).on('start', function (x) { stream = liquid.parser.parseStream(remainTokens).on('start', function () {
return p = _this.templates; return p = _this.templates;
}).on('tag:else', function (token) { }).on('tag:else', function () {
return p = _this.elseTemplates; return p = _this.elseTemplates;
}).on('tag:endfor', function (token) { }).on('tag:endfor', function () {
return stream.stop(); return stream.stop();
}).on('template', function (tpl) { }).on('template', function (tpl) {
return p.push(tpl); return p.push(tpl);
}).on('end', function (x) { }).on('end', function () {
throw new Error('tag ' + tagToken.raw + ' not closed'); throw new Error('tag ' + tagToken.raw + ' not closed');
}); });
@@ -1968,7 +2065,7 @@ module.exports = function (liquid) {
return liquid.renderer.renderTemplates(_this2.templates, scope).then(function (partial) { return liquid.renderer.renderTemplates(_this2.templates, scope).then(function (partial) {
return html += partial; return html += partial;
}).catch(function (e) { }).catch(function (e) {
if (e instanceof RenderBreak) { if (e instanceof RenderBreakError) {
html += e.resolvedHTML; html += e.resolvedHTML;
if (e.message === 'continue') return; if (e.message === 'continue') return;
} }
@@ -1977,7 +2074,7 @@ module.exports = function (liquid) {
return scope.pop(); return scope.pop();
}); });
}).catch(function (e) { }).catch(function (e) {
if (e instanceof RenderBreak && e.message === 'break') { if (e instanceof RenderBreakError && e.message === 'break') {
return; return;
} }
throw e; throw e;
@@ -1988,7 +2085,7 @@ module.exports = function (liquid) {
}); });
}; };
},{"..":2,"../src/util/assert.js":16,"../src/util/promise.js":19,"any-promise":3}],29:[function(require,module,exports){ },{"..":2,"../src/util/assert.js":16,"../src/util/promise.js":19}],29:[function(require,module,exports){
'use strict'; 'use strict';
var Liquid = require('..'); var Liquid = require('..');
+3 -2
View File
File diff suppressed because one or more lines are too long
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "shopify-liquid", "name": "shopify-liquid",
"version": "1.3.2", "version": "1.3.3",
"description": "Liquid template engine for JavaScript, Node.js and Browser", "description": "Liquid template engine for JavaScript, Node.js and Browser",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {