Задача: qForms, библиотека типичного функционала валидации/построения/связки html-форм
Исходник: wddx.js :: qForms api-139, язык: javascript [code #159, hits: 9720]
автор: - [добавлен: 28.05.2006]
  1. /******************************************************************************
  2. Required Functions
  3. ******************************************************************************/
  4. function __serializeStruct(struct){
  5. // open packet
  6. var aWDDX = new Array("<wddxPacket version='1.0'><header/><data><struct>");
  7. for( var key in struct ) aWDDX[aWDDX.length] = "<var name='" + key.toLowerCase() + "'><string>" + __wddxValue(struct[key]) + "</string></var>";
  8. // close packet
  9. aWDDX[aWDDX.length] = "</struct></data></wddxPacket>";
  10. return aWDDX.join("");
  11. }
  12.  
  13.  
  14. function __wddxValue(str){
  15. var aValue = new Array();
  16. for( var i=0; i < str.length; ++i) aValue[aValue.length] = _encoding.table[str.charAt(i)];
  17. return aValue.join("");
  18. }
  19.  
  20. function _wddx_Encoding(){
  21. // Encoding table for strings (CDATA)
  22. var et = new Array();
  23.  
  24. // numbers to characters table
  25. var n2c = new Array();
  26.  
  27. for( var i=0; i < 256; ++i ){
  28. // build a character from octal code
  29. var d1 = Math.floor(i/64);
  30. var d2 = Math.floor((i%64)/8);
  31. var d3 = i%8;
  32. var c = eval("\"\\" + d1.toString(10) + d2.toString(10) + d3.toString(10) + "\"");
  33.  
  34. // modify character-code conversion tables
  35. n2c[i] = c;
  36.  
  37. // modify encoding table
  38. if( i < 32 && i != 9 && i != 10 && i != 13 ){
  39. // control characters that are not tabs, newlines, and carriage returns
  40.  
  41. // create a two-character hex code representation
  42. var hex = i.toString(16);
  43. if( hex.length == 1 ) hex = "0" + hex;
  44.  
  45. et[n2c[i]] = "<char code='" + hex + "'/>";
  46.  
  47. } else if( i < 128 ){
  48. // low characters that are not special control characters
  49. et[n2c[i]] = n2c[i];
  50. } else {
  51. // high characters
  52. et[n2c[i]] = "&#x" + i.toString(16) + ";";
  53. }
  54. }
  55.  
  56. // special escapes for CDATA encoding
  57. et["<"] = "&lt;";
  58. et[">"] = "&gt;";
  59. et["&"] = "&amp;";
  60.  
  61. this.table = et;
  62. }
  63. _encoding = new _wddx_Encoding();
  64.  
  65.  
  66. /******************************************************************************
  67. qForm Methods
  68. ******************************************************************************/
  69. function _a_serialize(exclude){
  70. // if you need to reset the default values of the fields
  71. var lstExclude = (arguments.length > 0) ? "," + _removeSpaces(arguments[0]) + "," : "";
  72. struct = new Object();
  73. stcAllFields = qFormAPI.getFields();
  74. // loop through form elements
  75. for( key in stcAllFields ){
  76. if( lstExclude.indexOf("," + key + ",") == -1 ) struct[key] = stcAllFields[key];
  77. }
  78. // create & return the serialized object
  79. return __serializeStruct(struct);
  80. }
  81. _a.prototype.serialize = _a_serialize;
  82.  
  83. // define qForm serialize(); prototype
  84. function _q_serialize(exclude){
  85. // if you need to reset the default values of the fields
  86. var lstExclude = (arguments.length > 0) ? "," + _removeSpaces(arguments[0]) + "," : "";
  87. struct = new Object();
  88. // loop through form elements
  89. for( var j=0; j < this._fields.length; j++ ){
  90. if( lstExclude.indexOf("," + this._fields[j] + ",") == -1 ) struct[this._fields[j]] = this[this._fields[j]].getValue();
  91. }
  92. // create & return the serialized object
  93. return __serializeStruct(struct);
  94. }
  95. qForm.prototype.serialize = _q_serialize;
/******************************************************************************
qForm JSAPI: WDDX Mod Library

Author: Dan G. Switzer, II
Build: 101
******************************************************************************/
Тестировалось на: IE 6.0 SP2, Mozilla FF 1.5, Opera 8.5

+добавить реализацию