summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--itjs.class23
-rwxr-xr-xtest/itjs.t32
2 files changed, 20 insertions, 35 deletions
diff --git a/itjs.class b/itjs.class
index 72cf64d..7922cdc 100644
--- a/itjs.class
+++ b/itjs.class
@@ -39,30 +39,13 @@ function send_headers($charset = null)
}
/**
- * Serialize the result into a javascript script
+ * json_encode the result with options suitable for using as javascript source code
* @param $values Array with values to be serialized
- * @param $envelope Encapsulate the data when callback function is provided (iframe solution)
* @return String with javascript code to be sent to client
*/
-static function serialize($values, $envelope = false)
+static function serialize($values)
{
- if (($envelope || isset($values['eof'])) && ($callback = it::replace(array('[^\w.]' => ""), $_REQUEST['itjs_call'])))
- {
- $target = $_REQUEST['itjs_iframe'] ? "parent" : "window";
- list($itclass) = explode('.', $callback);
- $header = "$target.$itclass && $target.$callback && $target.$callback.dataReady(";
- $footer = "," . intval($_REQUEST['itjs_callid']) . ");";
-
- if ($_REQUEST['itjs_iframe']) # iframe-based loading required by Opera 7
- {
- $header = '<script type="text/javascript">' . $header;
- $footer .= "</script>";
- }
- }
-
- $result = $header . itjs::encode($values) . $footer;
-
- return it_untaint($result);
+ return json_encode($values, JSON_UNESCAPED_UNICODE | (it::is_devel() ? JSON_PRETTY_PRINT : 0));
}
/*
diff --git a/test/itjs.t b/test/itjs.t
index 695ccd8..777b1ec 100755
--- a/test/itjs.t
+++ b/test/itjs.t
@@ -3,6 +3,8 @@
# Tests for itjs.class, currently only itjs::serialize()
+$GLOBALS['ULTRASERVERTYPE'] = "live"; # don't prettyprint json
+
$_GET['aa'] = 1;
is(
@@ -19,48 +21,48 @@ is(
is(
itjs::serialize(array(0, "0")),
- '[0, 0]',
- 'number 0'
+ '[0,"0"]',
+ 'number strings stay strings'
);
is(
itjs::serialize(array(null, "", false)),
- '["", "", false]',
+ '[null,"",false]',
'null, empty string, false'
);
is(
- itjs::serialize(array(42, "42", 42.5, "042")),
- '[42, 42, "42.5", "042"]',
- 'numbers'
-);
-
-is(
itjs::serialize(array('foo' => "bar", "qux", 42 => "quux")),
- "{foo:\"bar\",\n'0':\"qux\",\n'42':\"quux\"}",
+ '{"foo":"bar","0":"qux","42":"quux"}',
'key-value pairs'
);
is(
itjs::serialize(array('foo' => array('bar' => array("qux", 42)))),
- '{foo:{bar:["qux", 42]}}',
+ '{"foo":{"bar":["qux",42]}}',
'nested arrays'
);
is(
itjs::serialize(array('import' => array('function' => array(true, 42)))),
- '{\'import\':{\'function\':[true, 42]}}',
+ '{"import":{"function":[true,42]}}',
'keywords'
);
is(
- itjs::serialize(array("foo'foo" => "bar")),
- "{'foo\'foo':\"bar\"}",
+ itjs::serialize(array('foo"foo' => "bar")),
+ '{"foo\"foo":"bar"}',
"quote ' in keys"
);
is(
+ itjs::serialize("</script>"),
+ '"<\\/script>"',
+ "quote slashes"
+);
+
+is(
itjs::serialize("hellö"),
'"hellö"',
- "string"
+ "don't encode utf8"
);