06.05 diff saved
bic2k
Tags add more
 
Note
removed debug code
  1. Index: libs/view/helpers/javascript.php
  2.  
  3. ===================================================================
  4.  
  5. --- libs/view/helpers/javascript.php    (revision 7111)
  6.  
  7. +++ libs/view/helpers/javascript.php    (working copy)
  8.  
  9. @@ -470,7 +470,7 @@
  10.  
  11.   * from an array
  12.   *
  13.   * @param array $data Data to be converted
  14. - * @param array $options Set of options: block, prefix, postfix, stringKeys, quoteKeys, q
  15. + * @param array $options Set of options: block, prefix, postfix, stringKeys, quoteKeys, q, strict
  16.   * @param string $prefix DEPRECATED, use $options['prefix'] instead. Prepends the string to the returned data
  17.   * @param string $postfix DEPRECATED, use $options['postfix'] instead. Appends the string to the returned data
  18.   * @param array $stringKeys DEPRECATED, use $options['stringKeys'] instead. A list of array keys to be treated as a string
  19. @@ -485,7 +485,7 @@
  20.  
  21.           $options = array();
  22.        }
  23.  
  24. -      $defaultOptions = array('block' => false, 'prefix' => '', 'postfix' => '', 'stringKeys' => array(), 'quoteKeys' => true, 'q' => '"');
  25. +      $defaultOptions = array('block' => false, 'prefix' => '', 'postfix' => '', 'stringKeys' => array(), 'quoteKeys' => true, 'q' => '"', 'strict' => true );
  26.        $options = array_merge($defaultOptions, $options, array_filter(compact(array_keys($defaultOptions))));
  27.  
  28.        if (is_object($data)) {
  29. @@ -507,13 +507,13 @@
  30.  
  31.           }
  32.  
  33.           foreach ($data as $key => $val) {
  34.              if (is_array($val) || is_object($val)) {
  35.                 $val = $this->object($val, am($options, array('block' => false)));
  36.              } else {
  37. -               $val = $this->value($val, (!count($options['stringKeys']) || ($options['quoteKeys'] && in_array($key, $options['stringKeys'], true)) || (!$options['quoteKeys'] && !in_array($key, $options['stringKeys'], true))));
  38. +               $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']);
  39.              }
  40.              if (!$numeric) {
  41. -               $val = $options['q'] . $this->value($key, false) . $options['q'] . ':' . $val;
  42. +               $val = $options['q'] . $this->value($key, false,$options['strict']) . $options['q'] . ':' . $val;
  43.              }
  44.              $out[] = $val;
  45.           }
  46. @@ -537,33 +538,63 @@
  47.  
  48.   *
  49.   * @param mixed $val A PHP variable to be converted to JSON
  50.   * @param boolean $quoteStrings If false, leaves string values unquoted
  51. + * @param boolean $strict If true, uses strict typing on values for quoting/formatting
  52.   * @return string a JavaScript-safe/JSON representation of $val
  53.   */
  54. -   function value($val, $quoteStrings = true) {
  55. -      switch (true) {
  56. -         case (is_array($val) || is_object($val)):
  57. -            $val = $this->object($val);
  58. -         break;
  59. -         case ($val === null):
  60. -            $val = 'null';
  61. -         break;
  62. -         case (is_bool($val)):
  63. -            $val = ife($val, 'true', 'false');
  64. -         break;
  65. -         case (is_int($val)):
  66. -            $val = $val;
  67. -         break;
  68. -         case (is_float($val)):
  69. -            $val = sprintf("%.11f", $val);
  70. -         break;
  71. -         default:
  72. -            $val = $this->escapeString($val);
  73. -            if ($quoteStrings) {
  74. -               $val = '"' . $val . '"';
  75. -            }
  76. -         break;
  77. -      }
  78. -      return $val;
  79. +   function value($val, $quoteStrings = true, $strict = true) {
  80. +        if ($strict) {
  81. +            switch (true) {
  82. +                case (is_array($val) || is_object($val)):
  83. +                   $val = $this->object($val);
  84. +                   break;
  85. +                case ($val === null):
  86. +                    $val = 'null';
  87. +                    break;
  88. +                case (is_bool($val)):
  89. +                    $val = ife($val, 'true', 'false');
  90. +                    break;
  91. +            //case (is_numeric($val) && (integer)($val) == $val):
  92. +                case (is_int($val)):               
  93. +                    $val = $val;
  94. +                    break;
  95. +            //case (is_numeric($val) && (float)$val == $val ):
  96. +                case (is_float($val)):
  97. +                    $val = sprintf("%.11f", $val);
  98. +                    break;
  99. +                default:
  100. +                    $val = $this->escapeString($val);
  101. +                    if ($quoteStrings) {
  102. +                        $val = '"' . $val . '"';
  103. +                    }
  104. +                    break;
  105. +            }
  106. +            return $val;
  107. +        } else {
  108. +            switch (true) {
  109. +                case (is_array($val) || is_object($val)):
  110. +                   $val = $this->object($val);
  111. +                   break;
  112. +                case ($val === null):
  113. +                    $val = 'null';
  114. +                    break;
  115. +                case (is_bool($val)):
  116. +                    $val = ife($val, 'true', 'false');
  117. +                    break;
  118. +                case (is_numeric($val) && (integer)($val) == $val):
  119. +                    $val = $val;
  120. +                    break;
  121. +                case (is_numeric($val) && (float)$val == $val ):
  122. +                    $val = sprintf("%.11f", $val);
  123. +                    break;
  124. +                default:
  125. +                    $val = $this->escapeString($val);
  126. +                    if ($quoteStrings) {
  127. +                        $val = '"' . $val . '"';
  128. +                    }
  129. +                    break;
  130. +            }
  131. +            return $val;
  132. +        }
  133.     }
  134.  /**
  135.   * AfterRender callback.  Writes any cached events to the view, or to a temp file.
  136. Index: tests/cases/libs/view/helpers/javascript.test.php
  137.  
  138. ===================================================================
  139.  
  140. --- tests/cases/libs/view/helpers/javascript.test.php   (revision 7111)
  141.  
  142. +++ tests/cases/libs/view/helpers/javascript.test.php   (working copy)
  143.  
  144. @@ -332,6 +332,11 @@
  145.  
  146.           $result = $this->Javascript->object(array('Object' => array(true => true, false, -3.141592653589, -10)));
  147.           $expected = '{"Object":{"1":true,"2":false,"3":' . (-1 * $number) . ',"4":-10}}';
  148.           $this->assertEqual($result, $expected);
  149. +           
  150. +            $result = $this->Javascript->object(array('Object' => array(1,-1,"1","-1",$number,"$number")),array('strict' => false));
  151. +           
  152. +            $expected = '{"Object":[1,-1,1,-1,3.14159265359,3.14159265359]}';
  153. +            $this->assertEqual($result, $expected);
  154.        }
  155.  
  156.        $result = $this->Javascript->object(new TestJavascriptObject());
  157.  
Parsed in 0.048 seconds, using GeSHi 1.0.7.14

Modify this Paste