[ Index ]
 

Code source de Seagull 0.6.1

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/js/html_ajax/serializer/ -> UrlSerializer.js (source)

   1  // {{{ HTML_AJAX_Serialize_Urlencoded
   2  /**
   3   * URL-encoding serializer
   4   *
   5   * This class can be used to serialize and unserialize data in a
   6   * format compatible with PHP's handling of HTTP query strings.
   7   * Due to limitations of the format, all input is serialized as an
   8   * array or a string. See examples/serialize.url.examples.php
   9   *
  10   * @version    0.0.1
  11   * @copyright    2005 Arpad Ray <arpad@php.net>
  12   * @license    http://www.opensource.org/licenses/lgpl-license.php  LGPL
  13   *
  14   * See Main.js for Author/license details
  15   */
  16  function HTML_AJAX_Serialize_Urlencoded() {}
  17  HTML_AJAX_Serialize_Urlencoded.prototype = {
  18      contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
  19      base: '_HTML_AJAX',
  20      _keys: [],
  21      error: false,
  22      message: "",
  23      cont: "",
  24      // {{{ serialize
  25      /**
  26       *  Serializes a variable
  27       *
  28       *  @param    mixed  inp the variable to serialize
  29       *  @return    string   a string representation of the input, 
  30       *                      which can be reconstructed by unserialize()
  31       */
  32      serialize: function(input, _internal) {
  33          if (typeof input == 'undefined') {
  34              return '';
  35          }
  36          if (!_internal) {
  37              this._keys = [];
  38          }
  39          var ret = '', first = true;
  40          for (i = 0; i < this._keys.length; i++) {
  41              ret += (first ? HTML_AJAX_Util.encodeUrl(this._keys[i]) : '[' + HTML_AJAX_Util.encodeUrl(this._keys[i]) + ']');
  42              first = false;
  43          }
  44          ret += '=';
  45          switch (HTML_AJAX_Util.getType(input)) {
  46              case 'string': 
  47              case 'number':
  48                  ret += HTML_AJAX_Util.encodeUrl(input.toString());
  49                  break;
  50              case 'boolean':
  51                  ret += (input ? '1' : '0');
  52                  break;
  53              case 'array':
  54              case 'object':
  55                  ret = '';
  56                  for (i in input) {
  57                      this._keys.push(i);
  58                      ret += this.serialize(input[i], true) + '&';
  59                      this._keys.pop();
  60                  }
  61                  ret = ret.substr(0, ret.length - 1);
  62          }
  63          return ret;
  64      },
  65      // }}}
  66      // {{{ unserialize
  67      /**
  68       *  Reconstructs a serialized variable
  69       *
  70       *  @param    string inp the string to reconstruct
  71       *  @return    array an array containing the variable represented by the input string, or void on failure
  72       */
  73      unserialize: function(input) {
  74          if (!input.length || input.length == 0) {
  75              // null
  76              return;
  77          }
  78          if (!/^(\w+(\[[^\[\]]*\])*=[^&]*(&|$))+$/.test(input)) {
  79              this.raiseError("invalidly formed input", input);
  80              return;
  81          }
  82          input = input.split("&");
  83          var pos, key, keys, val, _HTML_AJAX = [];
  84          if (input.length == 1) {
  85              return HTML_AJAX_Util.decodeUrl(input[0].substr(this.base.length + 1));
  86          }
  87          for (var i in input) {
  88              pos = input[i].indexOf("=");
  89              if (pos < 1 || input[i].length - pos - 1 < 1) {
  90                  this.raiseError("input is too short", input[i]);
  91                  return;
  92              }
  93              key = HTML_AJAX_Util.decodeUrl(input[i].substr(0, pos));
  94              val = HTML_AJAX_Util.decodeUrl(input[i].substr(pos + 1));
  95              key = key.replace(/\[((\d*\D+)+)\]/g, '["$1"]');
  96              keys = key.split(']');
  97              for (j in keys) {
  98                  if (!keys[j].length || keys[j].length == 0) {
  99                      continue;
 100                  }
 101                  try {
 102                      if (eval('typeof ' + keys[j] + ']') == 'undefined') {
 103                          var ev = keys[j] + ']=[];';
 104                          eval(ev);
 105                      }
 106                  } catch (e) {
 107                      this.raiseError("error evaluating key", ev);
 108                      return; 
 109                  }
 110              }
 111              try {
 112                  eval(key + '="' + val + '";');
 113              } catch (e) {
 114                  this.raiseError("error evaluating value", input);
 115                  return; 
 116              }
 117          }
 118          return _HTML_AJAX;
 119      },
 120      // }}}
 121      // {{{ getError
 122      /**
 123      *  Gets the last error message
 124      *
 125      *  @return    string   the last error message from unserialize()
 126      */    
 127      getError: function() {
 128          return this.message + "\n" + this.cont;
 129      },
 130      // }}}
 131      // {{{ raiseError
 132      /**
 133      *  Raises an eror (called by unserialize().)
 134      *
 135      *  @param    string    message    the error message
 136      *  @param    string    cont       the remaining unserialized content
 137      */    
 138      raiseError: function(message, cont) {
 139          this.error = 1;
 140          this.message = message;
 141          this.cont = cont;
 142      }
 143      // }}}
 144  }
 145  // }}}


Généré le : Fri Mar 30 01:27:52 2007 par Balluche grâce à PHPXref 0.7