06.05
diff
saved
Bic2k
Note
Bic2k did not leave a note
Bic2k did not leave a note
- Index: libs/view/helpers/javascript.php
- ===================================================================
- --- libs/view/helpers/javascript.php (revision 7111)
- +++ libs/view/helpers/javascript.php (working copy)
- @@ -470,7 +470,7 @@
- * from an array
- *
- * @param array $data Data to be converted
- - * @param array $options Set of options: block, prefix, postfix, stringKeys, quoteKeys, q
- + * @param array $options Set of options: block, prefix, postfix, stringKeys, quoteKeys, q, strict
- * @param string $prefix DEPRECATED, use $options['prefix'] instead. Prepends the string to the returned data
- * @param string $postfix DEPRECATED, use $options['postfix'] instead. Appends the string to the returned data
- * @param array $stringKeys DEPRECATED, use $options['stringKeys'] instead. A list of array keys to be treated as a string
- @@ -485,7 +485,7 @@
- $options = array();
- }
- - $defaultOptions = array('block' => false, 'prefix' => '', 'postfix' => '', 'stringKeys' => array(), 'quoteKeys' => true, 'q' => '"');
- + $defaultOptions = array('block' => false, 'prefix' => '', 'postfix' => '', 'stringKeys' => array(), 'quoteKeys' => true, 'q' => '"', 'strict' => true );
- $options = array_merge($defaultOptions, $options, array_filter(compact(array_keys($defaultOptions))));
- if (is_object($data)) {
- @@ -507,13 +507,14 @@
- }
- foreach ($data as $key => $val) {
- + //if ( $key === 'StockHistory-open_price' ) { debug($val);debug(is_numeric($val) == true ? 'true' : 'false');debug(is_float($val) == true ? 'true' : 'false');die(); }
- if (is_array($val) || is_object($val)) {
- $val = $this->object($val, am($options, array('block' => false)));
- } else {
- - $val = $this->value($val, (!count($options['stringKeys']) || ($options['quoteKeys'] && in_array($key, $options['stringKeys'], true)) || (!$options['quoteKeys'] && !in_array($key, $options['stringKeys'], true))));
- + $val = $this->value($val, (!count($options['stringKeys']) || ($options['quoteKeys'] && in_array($key, $options['stringKeys'], true)) || (!$options['quoteKeys'] && !in_array($key, $options['stringKeys'], true))),$options['strict']);
- }
- if (!$numeric) {
- - $val = $options['q'] . $this->value($key, false) . $options['q'] . ':' . $val;
- + $val = $options['q'] . $this->value($key, false,$options['strict']) . $options['q'] . ':' . $val;
- }
- $out[] = $val;
- }
- @@ -537,33 +538,63 @@
- *
- * @param mixed $val A PHP variable to be converted to JSON
- * @param boolean $quoteStrings If false, leaves string values unquoted
- + * @param boolean $strict If true, uses strict typing on values for quoting/formatting
- * @return string a JavaScript-safe/JSON representation of $val
- */
- - function value($val, $quoteStrings = true) {
- - switch (true) {
- - case (is_array($val) || is_object($val)):
- - $val = $this->object($val);
- - break;
- - case ($val === null):
- - $val = 'null';
- - break;
- - case (is_bool($val)):
- - $val = ife($val, 'true', 'false');
- - break;
- - case (is_int($val)):
- - $val = $val;
- - break;
- - case (is_float($val)):
- - $val = sprintf("%.11f", $val);
- - break;
- - default:
- - $val = $this->escapeString($val);
- - if ($quoteStrings) {
- - $val = '"' . $val . '"';
- - }
- - break;
- - }
- - return $val;
- + function value($val, $quoteStrings = true, $strict = true) {
- + if ($strict) {
- + switch (true) {
- + case (is_array($val) || is_object($val)):
- + $val = $this->object($val);
- + break;
- + case ($val === null):
- + $val = 'null';
- + break;
- + case (is_bool($val)):
- + $val = ife($val, 'true', 'false');
- + break;
- + //case (is_numeric($val) && (integer)($val) == $val):
- + case (is_int($val)):
- + $val = $val;
- + break;
- + //case (is_numeric($val) && (float)$val == $val ):
- + case (is_float($val)):
- + $val = sprintf("%.11f", $val);
- + break;
- + default:
- + $val = $this->escapeString($val);
- + if ($quoteStrings) {
- + $val = '"' . $val . '"';
- + }
- + break;
- + }
- + return $val;
- + } else {
- + switch (true) {
- + case (is_array($val) || is_object($val)):
- + $val = $this->object($val);
- + break;
- + case ($val === null):
- + $val = 'null';
- + break;
- + case (is_bool($val)):
- + $val = ife($val, 'true', 'false');
- + break;
- + case (is_numeric($val) && (integer)($val) == $val):
- + $val = $val;
- + break;
- + case (is_numeric($val) && (float)$val == $val ):
- + $val = sprintf("%.11f", $val);
- + break;
- + default:
- + $val = $this->escapeString($val);
- + if ($quoteStrings) {
- + $val = '"' . $val . '"';
- + }
- + break;
- + }
- + return $val;
- + }
- }
- /**
- * AfterRender callback. Writes any cached events to the view, or to a temp file.
- Index: tests/cases/libs/view/helpers/javascript.test.php
- ===================================================================
- --- tests/cases/libs/view/helpers/javascript.test.php (revision 7111)
- +++ tests/cases/libs/view/helpers/javascript.test.php (working copy)
- @@ -332,6 +332,11 @@
- $result = $this->Javascript->object(array('Object' => array(true => true, false, -3.141592653589, -10)));
- $expected = '{"Object":{"1":true,"2":false,"3":' . (-1 * $number) . ',"4":-10}}';
- $this->assertEqual($result, $expected);
- +
- + $result = $this->Javascript->object(array('Object' => array(1,-1,"1","-1",$number,"$number")),array('strict' => false));
- +
- + $expected = '{"Object":[1,-1,1,-1,3.14159265359,3.14159265359]}';
- + $this->assertEqual($result, $expected);
- }
- $result = $this->Javascript->object(new TestJavascriptObject());
Parsed in 0.050 seconds, using GeSHi 1.0.7.14