From 888fb86887a9ea164c642ad55b1c9153a3a270f7 Mon Sep 17 00:00:00 2001 From: "zhenbo.zheng" Date: Wed, 13 Aug 2014 20:50:01 +0800 Subject: [PATCH 1/4] Check the undefined variables and fix it. --- lib/ejs.js | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/ejs.js b/lib/ejs.js index ca304a13..59e99f04 100644 --- a/lib/ejs.js +++ b/lib/ejs.js @@ -299,7 +299,9 @@ exports.render = function(str, options){ } else { fn = compile(str, options); } - + + fixUndef(fn, options); + options.__proto__ = options.locals; return fn.call(options.scope, options); }; @@ -350,6 +352,26 @@ function resolveInclude(name, filename) { return path; } +/** + * Check the undefined variables and fix it. + * + * @param {Function} fn + * @param {Object|Function} options or callback + */ + +function fixUndef (fn, options) { + try { + fn.call(options.scope, options); + } + catch (e) { + if (e.message.indexOf('is not defined')) { + var vars = e.message.replace('is not defined', '').trim(); + options[vars] = undefined; + checkUndef(fn, options); + } + } +} + // express support exports.__express = exports.renderFile; From 88b1bfd3fb0d882e8adecf977e457906c64df11a Mon Sep 17 00:00:00 2001 From: "zhenbo.zheng" Date: Wed, 13 Aug 2014 22:49:52 +0800 Subject: [PATCH 2/4] fix bug --- lib/ejs.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ejs.js b/lib/ejs.js index 59e99f04..8263d409 100644 --- a/lib/ejs.js +++ b/lib/ejs.js @@ -367,7 +367,7 @@ function fixUndef (fn, options) { if (e.message.indexOf('is not defined')) { var vars = e.message.replace('is not defined', '').trim(); options[vars] = undefined; - checkUndef(fn, options); + fixUndef(fn, options); } } } From a4f057f7c5b8aee17155cb8e642f7912fd0130c4 Mon Sep 17 00:00:00 2001 From: "zhenbo.zheng" Date: Wed, 13 Aug 2014 23:05:17 +0800 Subject: [PATCH 3/4] fix the scene like Obj.a --- lib/ejs.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ejs.js b/lib/ejs.js index 8263d409..cb1e9a55 100644 --- a/lib/ejs.js +++ b/lib/ejs.js @@ -366,7 +366,7 @@ function fixUndef (fn, options) { catch (e) { if (e.message.indexOf('is not defined')) { var vars = e.message.replace('is not defined', '').trim(); - options[vars] = undefined; + options[vars] = {toString:function () {return undefined}}; fixUndef(fn, options); } } From d1c004cf39a3f663109c8cfec15d30bc3828de32 Mon Sep 17 00:00:00 2001 From: "zhenbo.zheng" Date: Thu, 14 Aug 2014 00:14:48 +0800 Subject: [PATCH 4/4] a better way to display error. --- lib/ejs.js | 45 +++++++-------------------------------------- 1 file changed, 7 insertions(+), 38 deletions(-) diff --git a/lib/ejs.js b/lib/ejs.js index cb1e9a55..0f647fbb 100644 --- a/lib/ejs.js +++ b/lib/ejs.js @@ -1,3 +1,4 @@ + /*! * EJS * Copyright(c) 2012 TJ Holowaychuk @@ -126,8 +127,10 @@ var parse = exports.parse = function(str, options){ var prefix, postfix, line = (compileDebug ? '__stack.lineno=' : '') + lineno; switch (str[i]) { case '=': - prefix = "', escape((" + line + ', '; - postfix = ")), '"; + /*prefix = "', escape((" + line + ', '; + postfix = ")), '";*/ + prefix = "', (function () {try{return "; + postfix = "}catch(e){return e.message}})(), '"; ++i; break; case '-': @@ -167,19 +170,7 @@ var parse = exports.parse = function(str, options){ } while (~(n = js.indexOf("\n", n))) n++, lineno++; - - switch(js.substr(0, 1)) { - case ':': - js = filtered(js); - break; - case '%': - js = " buf.push('<%" + js.substring(1).replace(/'/g, "\\'") + "%>');"; - break; - case '#': - js = ""; - break; - } - + if (js.substr(0, 1) == ':') js = filtered(js); if (js) { if (js.lastIndexOf('//') > js.lastIndexOf('\n')) js += '\n'; buf += prefix; @@ -299,9 +290,7 @@ exports.render = function(str, options){ } else { fn = compile(str, options); } - - fixUndef(fn, options); - + options.__proto__ = options.locals; return fn.call(options.scope, options); }; @@ -352,26 +341,6 @@ function resolveInclude(name, filename) { return path; } -/** - * Check the undefined variables and fix it. - * - * @param {Function} fn - * @param {Object|Function} options or callback - */ - -function fixUndef (fn, options) { - try { - fn.call(options.scope, options); - } - catch (e) { - if (e.message.indexOf('is not defined')) { - var vars = e.message.replace('is not defined', '').trim(); - options[vars] = {toString:function () {return undefined}}; - fixUndef(fn, options); - } - } -} - // express support exports.__express = exports.renderFile;