Comparing version 1.2.5 to 1.3.0
{ | ||
"throwOnError": false, | ||
"throwOnCompileError": false, | ||
"throwOnRuntimeError": false, | ||
"outputPrecompiledSourceToConsole": false, | ||
@@ -4,0 +5,0 @@ "outputErrorToConsole": false, |
@@ -8,3 +8,3 @@ 'use strict'; | ||
function Precompiled(code, options, customBuiltins) { | ||
function Precompiled(code, options) { | ||
var self = this; | ||
@@ -17,4 +17,2 @@ | ||
self.customBuiltins = customBuiltins; | ||
self.fn = null; | ||
@@ -30,3 +28,3 @@ try { | ||
if(self.options.throwOnError) { throw e; } | ||
if(self.options.throwOnCompileError) { throw e; } | ||
else { | ||
@@ -45,7 +43,2 @@ self.fn = function() { return ''; }; | ||
var builtins = new Builtins(context, locals); | ||
// add/update custom builtins | ||
_.each(self.customBuiltins, function(builtin, name) { | ||
builtins[name] = builtin; | ||
}); | ||
try { | ||
@@ -59,3 +52,3 @@ return self.fn(context, builtins, locals); | ||
if(self.options.throwOnError) { throw e; } | ||
if(self.options.throwOnRuntimeError) { throw e; } | ||
else { return ''; } | ||
@@ -62,0 +55,0 @@ } |
@@ -88,3 +88,3 @@ 'use strict'; | ||
function Wash(source, options, customBuiltins) { | ||
function Wash(source, options) { | ||
var self = this; | ||
@@ -101,6 +101,2 @@ | ||
if(customBuiltins) { | ||
self.customBuiltins = customBuiltins; | ||
} | ||
self._code = ''; | ||
@@ -111,11 +107,2 @@ self._forLoopVars = []; | ||
Wash.prototype._isBuiltin = function(token) { | ||
var self = this; | ||
if(Builtins.isBuiltin(token)) { return true; } | ||
if(self.customBuiltins) { | ||
return !!utils.getValueByPath(self.customBuiltins, '.' + token); | ||
} | ||
}; | ||
Wash.prototype._evalTokens = function(tokens) { | ||
@@ -142,3 +129,3 @@ var self = this; | ||
} | ||
} else if(self._isBuiltin(token)) { | ||
} else if(Builtins.isBuiltin(token)) { | ||
outs += '__builtins.' + token; | ||
@@ -151,3 +138,3 @@ } else { | ||
} else { | ||
outs += _safeEval(token, self.options.throwOnError); | ||
outs += _safeEval(token, self.options.throwOnCompileError); | ||
} | ||
@@ -189,2 +176,3 @@ } | ||
var predIf = self._evalTokens(params); | ||
if(!self.options.throwOnRuntimeError) { self._code += 'try{\n'; } | ||
self._code += 'if(' + predIf + '){\n'; | ||
@@ -201,4 +189,6 @@ break; | ||
self._code += '}\n'; | ||
if(!self.options.throwOnRuntimeError) { self._code += '}catch(__ignored){}\n'; } | ||
break; | ||
case 'for': | ||
if(!self.options.throwOnRuntimeError) { self._code += 'try{\n'; } | ||
self._parseForTag(params); | ||
@@ -209,2 +199,3 @@ break; | ||
self._code += '}\n'; | ||
if(!self.options.throwOnRuntimeError) { self._code += '}catch(__ignored){}\n'; } | ||
break; | ||
@@ -231,3 +222,3 @@ } | ||
// {% actions %} | ||
// {% statements %} | ||
match = actionTagRegex.exec(block); | ||
@@ -253,4 +244,10 @@ if(match) { | ||
// just run | ||
self._code += '(' + self._evalTokens(_tokenize(expr)) + ');'; | ||
output = self._evalTokens(_tokenize(expr)); | ||
if(output) { | ||
if(self.options.throwOnRuntimeError) { | ||
self._code += '(' + output + ');\n'; | ||
} else { | ||
self._code += 'try{\n' + output + '\n}catch(__ignored){}\n'; | ||
} | ||
} | ||
} | ||
@@ -275,3 +272,7 @@ | ||
if(output) { | ||
self._code += '__out+=(' + output + ');\n'; | ||
if(self.options.throwOnRuntimeError) { | ||
self._code += '__out+=(' + output + ');\n'; | ||
} else { | ||
self._code += 'try{\n__out+=(' + output + ');\n}catch(__ignored){}\n'; | ||
} | ||
} | ||
@@ -290,3 +291,3 @@ | ||
if(self.options.throwOnError) { throw err; } | ||
if(self.options.throwOnCompileError) { throw err; } | ||
} | ||
@@ -301,3 +302,3 @@ }); | ||
console.assert(!self.options.throwOnError, 'No closing tag: ' + tagName); | ||
console.assert(!self.options.throwOnCompileError, 'No closing tag: ' + tagName); | ||
self._code = 'var __out = "";'; | ||
@@ -309,3 +310,3 @@ } else if(level < 0) { | ||
console.assert(!self.options.throwOnError, 'Redundant closing tag: ' + tagName); | ||
console.assert(!self.options.throwOnCompileError, 'Redundant closing tag: ' + tagName); | ||
self._code = 'var __out = "";'; | ||
@@ -322,3 +323,3 @@ } | ||
return new Precompiled(self._code, self.options, self.customBuiltins); | ||
return new Precompiled(self._code, self.options); | ||
}; | ||
@@ -325,0 +326,0 @@ |
{ | ||
"name": "wash", | ||
"description": "a safe template rendering engine", | ||
"version": "1.2.5", | ||
"version": "1.3.0", | ||
"main": "index", | ||
@@ -6,0 +6,0 @@ "author": { |
@@ -45,3 +45,2 @@ # Wash | ||
My name is John. I am 30 years old. | ||
(Yes I am old enough.) | ||
I have 3 kids. They are Daniel, Paul, Mark. | ||
@@ -58,3 +57,3 @@ THEY ARE DANIEL PAUL MARK | ||
- Wash doew not allow the template code to access external variables, modules, or functions. | ||
- _(work in progress)_ Wash can restrict the number of iterations or the number of elements in collection. | ||
- Wash can restrict the number of iterations or the number of elements in collection. | ||
@@ -61,0 +60,0 @@ ## References |
@@ -50,2 +50,4 @@ 'use strict'; | ||
opts = opts || {}; | ||
opts.throwOnCompileError = true; | ||
opts.throwOnRuntimeError = true; | ||
opts.outputPrecompiledSourceToConsole = false; | ||
@@ -61,3 +63,24 @@ opts.outputErrorToConsole = true; | ||
function expectError(source, opts, expected) { | ||
function expectCompileError(source, opts) { | ||
opts = opts || {}; | ||
opts.throwOnRuntimeError = true; | ||
it('Wash.render("' + esc(source) + '") expect compile error [throwOnError=true]', function() { | ||
opts.throwOnCompileError = true; | ||
assert.throws(function() { | ||
var wash = new Wash(source, opts); | ||
wash.precompile(); | ||
}); | ||
}); | ||
it('Wash.render("' + esc(source) + '") expect compile error [throwOnError=false]', function() { | ||
opts.throwOnCompileError = false; | ||
var wash = new Wash(source, opts); | ||
var precompiled = wash.precompile(); | ||
var actual = precompiled.render(ctx); | ||
assert.strictEqual(actual, ''); | ||
}); | ||
} | ||
function expectRuntimeError(source, opts, expected) { | ||
if(arguments.length === 2) { | ||
@@ -68,14 +91,19 @@ expected = opts; | ||
it('Wash.render("' + esc(source) + '") should throw', function() { | ||
opts = opts || {}; | ||
opts = opts || {}; | ||
opts.throwOnCompileError = true; | ||
it('Wash.render("' + esc(source) + '") expect runtime error [throwOnError=true]', function() { | ||
opts.throwOnRuntimeError = true; | ||
var wash = new Wash(source, opts); | ||
var precompiled = wash.precompile(); | ||
assert.throws(function() { | ||
opts.throwOnError = true; | ||
var wash = new Wash(source, opts); | ||
wash.render(ctx); | ||
precompiled.render(ctx); | ||
}); | ||
}); | ||
opts.throwOnError = false; | ||
it('Wash.render("' + esc(source) + '") expect runtime error [throwOnError=false]', function() { | ||
opts.throwOnRuntimeError = false; | ||
var wash = new Wash(source, opts); | ||
var actual = wash.render(ctx); | ||
var precompiled = wash.precompile(); | ||
var actual = precompiled.render(ctx); | ||
assert.strictEqual(actual, expected); | ||
@@ -87,2 +115,3 @@ }); | ||
global.expect = expect; | ||
global.expectError = expectError; | ||
global.expectCompileError = expectCompileError; | ||
global.expectRuntimeError = expectRuntimeError; |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
60371
30
1313
66