Fixed problem with escaping and added a few test cases to the PHP->JS compiler

This commit is contained in:
Andreas Stöckel 2011-09-07 16:31:32 +00:00
parent db074aa39c
commit efc173cd2c

View File

@ -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("", "");
})();*/