JSON: Properly handles unicode escape sequences (\u####) now
authorDan
Sun, 22 Mar 2009 00:44:09 -0400
changeset 880 218b6d4de908
parent 879 9788f2b7e08a
child 881 01810d32a0d6
JSON: Properly handles unicode escape sequences (\u####) now
ajax.php
includes/clientside/static/editor.js
includes/json2.php
--- a/ajax.php	Sat Mar 21 19:11:30 2009 -0400
+++ b/ajax.php	Sun Mar 22 00:44:09 2009 -0400
@@ -187,9 +187,16 @@
       if ( !isset($_POST['r']) )
         die('Invalid request');
       
-      $request = enano_json_decode($_POST['r']);
-      if ( !isset($request['src']) || !isset($request['summary']) || !isset($request['minor_edit']) || !isset($request['time']) || !isset($request['draft']) )
-        die('Invalid request');
+      try
+      {
+        $request = enano_json_decode($_POST['r']);
+        if ( !isset($request['src']) || !isset($request['summary']) || !isset($request['minor_edit']) || !isset($request['time']) || !isset($request['draft']) )
+          die('Invalid request');
+      }
+      catch(Zend_Json_Exception $e)
+      {
+        die("JSON parsing failed. View as HTML to see full report.\n<br /><br />\n<pre>" . htmlspecialchars(strval($e)) . "</pre><br />Request: <pre>" . htmlspecialchars($_POST['r']) . "</pre>");
+      }
       
       $time = intval($request['time']);
       
--- a/includes/clientside/static/editor.js	Sat Mar 21 19:11:30 2009 -0400
+++ b/includes/clientside/static/editor.js	Sun Mar 22 00:44:09 2009 -0400
@@ -13,7 +13,7 @@
     return true;
   if ( editor_open )
     return true;
-  load_component(['l10n', 'template-compiler', 'messagebox', 'fadefilter', 'flyin']);
+  load_component(['l10n', 'template-compiler', 'messagebox', 'fadefilter', 'flyin', 'toolbar']);
   selectButtonMinor('edit');
   selectButtonMajor('article');
   setAjaxLoading();
--- a/includes/json2.php	Sat Mar 21 19:11:30 2009 -0400
+++ b/includes/json2.php	Sun Mar 22 00:44:09 2009 -0400
@@ -773,6 +773,9 @@
                             case '\'' :
                                 $result .= '\'';
                                 break;
+                            case 'u':
+                              $result .= self::decode_unicode_byte(substr($str, $i + 1, 4));
+                              break;
                             default:
                                 throw new Zend_Json_Exception("Illegal escape "
                                     .  "sequence '" . $chr . "'");
@@ -904,6 +907,41 @@
 
         return($this->_token);
     }
+    
+    /**
+     * Handle a Unicode byte; local to Enano.
+     * @param string 4 character byte sequence
+     * @return string
+     */
+    
+    protected function decode_unicode_byte($byte)
+    {
+      if ( strlen($byte) != 4 )
+        throw new Zend_Json_Exception("Invalid Unicode sequence \\u$byte");
+        
+      $value = hexdec($byte);
+
+      if ($value < 0x0080)
+      {
+        // 1 byte: 0xxxxxxx
+        $character = chr($value);
+      }
+      else if ($value < 0x0800)
+      {
+        // 2 bytes: 110xxxxx 10xxxxxx
+        $character =
+            chr((($value & 0x07c0) >> 6) | 0xc0)
+          . chr(($value & 0x3f) | 0x80);
+      }
+      else
+      {
+        // 3 bytes: 1110xxxx 10xxxxxx 10xxxxxx
+        $character =
+            chr((($value & 0xf000) >> 12) | 0xe0)
+          . chr((($value & 0x0fc0) >> 6) | 0x80)
+          . chr(($value & 0x3f) | 0x80);
+      }
+    }
 }
 
 /**