From efc173cd2c1c56500bc909bd4c6c7de39dd1a832 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20St=C3=B6ckel?= Date: Wed, 7 Sep 2011 16:31:32 +0000 Subject: [PATCH] Fixed problem with escaping and added a few test cases to the PHP->JS compiler --- .../js/et2_core_phpExpressionCompiler.js | 33 +++++++++++++++++-- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/etemplate/js/et2_core_phpExpressionCompiler.js b/etemplate/js/et2_core_phpExpressionCompiler.js index c7bed8df0a..97ff223fb2 100644 --- a/etemplate/js/et2_core_phpExpressionCompiler.js +++ b/etemplate/js/et2_core_phpExpressionCompiler.js @@ -352,8 +352,8 @@ if (typeof part == "string") { - // Escape all "'" chars and add the string to the parts array - parts.push("'" + part.replace(/'/g, "\\'") + "'"); + // Escape all "'" and "\" chars and add the string to the parts array + parts.push("'" + part.replace(/'/g, "\\'").replace(/\\/g, "\\\\") + "'"); } else { @@ -361,6 +361,11 @@ } } + if (parts.length == 0) + { + parts.push('""'); + } + return parts.join(" + "); } @@ -399,7 +404,7 @@ var js = _php_compileJSCode(_vars, syntaxTree); // Log the successfull compiling - et2_debug("log", "Compiled PHP '" + _expr + "' --> '" + js + "'"); + et2_debug("log", "Compiled PHP " + _expr + " --> " + js); // Prepate the attributes for the function constuctor var attrs = []; @@ -415,3 +420,25 @@ }).call(window); + + +// Include this code in in order to test the above code +/*(function () { + var row = 10; + var row_cont = {"title": "Hello World!"}; + var cont = {10: row_cont}; + + function test(_php, _res) + { + console.log( + et2_compilePHPExpression(_php, ["row", "row_cont", "cont"]) + (row, row_cont, cont) === _res); + } + + test("${row}[title]", "10[title]"); + test("{$row_cont[title]}", "Hello World!"); + test('{$cont["$row"][\'title\']}', "Hello World!"); + test("\\\\", "\\"); + test("", ""); +})();*/ +