summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Schneider2008-07-28 15:20:54 +0000
committerChristian Schneider2008-07-28 15:20:54 +0000
commit03443412d7038a8a68da1137ea7e6b38fdbc728c (patch)
tree3c0172544631b3175f8f9b8d9cdf026de82346ce
parent92e07b4f66121d0509acf2931e084b7a62dc0ee2 (diff)
downloaditools-03443412d7038a8a68da1137ea7e6b38fdbc728c.tar.gz
itools-03443412d7038a8a68da1137ea7e6b38fdbc728c.tar.bz2
itools-03443412d7038a8a68da1137ea7e6b38fdbc728c.zip
Quote identifier if it is a javascript reserved word
-rw-r--r--itjs.class3
-rwxr-xr-xtests/itjs.t6
2 files changed, 8 insertions, 1 deletions
diff --git a/itjs.class b/itjs.class
index add3914..6c98c27 100644
--- a/itjs.class
+++ b/itjs.class
@@ -65,6 +65,7 @@ function serialize($values, $envelope = false)
function encode($values)
{
$texts = ($values === array_values($values)) ? "[]0 " : "{}1\n"; # Numerical or associative array
+ static $jskeyword = array("abstract" => 1, "boolean" => 1, "break" => 1, "byte" => 1, "case" => 1, "catch" => 1, "char" => 1, "class" => 1, "const" => 1, "continue" => 1, "debugger" => 1, "default" => 1, "delete" => 1, "do" => 1, "double" => 1, "each" => 1, "else" => 1, "enum" => 1, "export" => 1, "extends" => 1, "false" => 1, "final" => 1, "finally" => 1, "float" => 1, "for" => 1, "function" => 1, "goto" => 1, "if" => 1, "implements" => 1, "import" => 1, "in" => 1, "instanceof" => 1, "int" => 1, "interface" => 1, "long" => 1, "namespace" => 1, "native" => 1, "new" => 1, "null" => 1, "package" => 1, "private" => 1, "protected" => 1, "public" => 1, "return" => 1, "short" => 1, "static" => 1, "super" => 1, "switch" => 1, "synchronized" => 1, "this" => 1, "throw" => 1, "throws" => 1, "transient" => 1, "true" => 1, "try" => 1, "typeof" => 1, "var" => 1, "void" => 1, "volatile" => 1, "while" => 1, "with" => 1, "xml" => 1);
$result = $texts{0};
@@ -74,7 +75,7 @@ function encode($values)
if ($texts{2})
{
- if (!preg_match('/^[a-z_]\w*$/i', $key))
+ if ($jskeyword[$key] || !preg_match('/^[a-z_]\w*$/i', $key))
$key = "'$key'";
$result .= "$key:";
diff --git a/tests/itjs.t b/tests/itjs.t
index 4b4b0c8..ef3437a 100755
--- a/tests/itjs.t
+++ b/tests/itjs.t
@@ -41,4 +41,10 @@ is(
'nested arrays'
);
+is(
+ itjs::serialize(array('import' => array('function' => array(true, 42)))),
+ '{\'import\':{\'function\':[true, 42]}}',
+ 'keywords'
+);
+
?>