PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/serialize.js

http://github.com/jquery/jquery
JavaScript | 129 lines | 86 code | 28 blank | 15 comment | 25 complexity | 928bee4a7bf49b35fd95ac76997346f2 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. import jQuery from "./core.js";
  2. import toType from "./core/toType.js";
  3. import rcheckableType from "./var/rcheckableType.js";
  4. import "./core/init.js";
  5. import "./traversing.js"; // filter
  6. import "./attributes/prop.js";
  7. var
  8. rbracket = /\[\]$/,
  9. rCRLF = /\r?\n/g,
  10. rsubmitterTypes = /^(?:submit|button|image|reset|file)$/i,
  11. rsubmittable = /^(?:input|select|textarea|keygen)/i;
  12. function buildParams( prefix, obj, traditional, add ) {
  13. var name;
  14. if ( Array.isArray( obj ) ) {
  15. // Serialize array item.
  16. jQuery.each( obj, function( i, v ) {
  17. if ( traditional || rbracket.test( prefix ) ) {
  18. // Treat each array item as a scalar.
  19. add( prefix, v );
  20. } else {
  21. // Item is non-scalar (array or object), encode its numeric index.
  22. buildParams(
  23. prefix + "[" + ( typeof v === "object" && v != null ? i : "" ) + "]",
  24. v,
  25. traditional,
  26. add
  27. );
  28. }
  29. } );
  30. } else if ( !traditional && toType( obj ) === "object" ) {
  31. // Serialize object item.
  32. for ( name in obj ) {
  33. buildParams( prefix + "[" + name + "]", obj[ name ], traditional, add );
  34. }
  35. } else {
  36. // Serialize scalar item.
  37. add( prefix, obj );
  38. }
  39. }
  40. // Serialize an array of form elements or a set of
  41. // key/values into a query string
  42. jQuery.param = function( a, traditional ) {
  43. var prefix,
  44. s = [],
  45. add = function( key, valueOrFunction ) {
  46. // If value is a function, invoke it and use its return value
  47. var value = typeof valueOrFunction === "function" ?
  48. valueOrFunction() :
  49. valueOrFunction;
  50. s[ s.length ] = encodeURIComponent( key ) + "=" +
  51. encodeURIComponent( value == null ? "" : value );
  52. };
  53. if ( a == null ) {
  54. return "";
  55. }
  56. // If an array was passed in, assume that it is an array of form elements.
  57. if ( Array.isArray( a ) || ( a.jquery && !jQuery.isPlainObject( a ) ) ) {
  58. // Serialize the form elements
  59. jQuery.each( a, function() {
  60. add( this.name, this.value );
  61. } );
  62. } else {
  63. // If traditional, encode the "old" way (the way 1.3.2 or older
  64. // did it), otherwise encode params recursively.
  65. for ( prefix in a ) {
  66. buildParams( prefix, a[ prefix ], traditional, add );
  67. }
  68. }
  69. // Return the resulting serialization
  70. return s.join( "&" );
  71. };
  72. jQuery.fn.extend( {
  73. serialize: function() {
  74. return jQuery.param( this.serializeArray() );
  75. },
  76. serializeArray: function() {
  77. return this.map( function() {
  78. // Can add propHook for "elements" to filter or add form elements
  79. var elements = jQuery.prop( this, "elements" );
  80. return elements ? jQuery.makeArray( elements ) : this;
  81. } ).filter( function() {
  82. var type = this.type;
  83. // Use .is( ":disabled" ) so that fieldset[disabled] works
  84. return this.name && !jQuery( this ).is( ":disabled" ) &&
  85. rsubmittable.test( this.nodeName ) && !rsubmitterTypes.test( type ) &&
  86. ( this.checked || !rcheckableType.test( type ) );
  87. } ).map( function( _i, elem ) {
  88. var val = jQuery( this ).val();
  89. if ( val == null ) {
  90. return null;
  91. }
  92. if ( Array.isArray( val ) ) {
  93. return jQuery.map( val, function( val ) {
  94. return { name: elem.name, value: val.replace( rCRLF, "\r\n" ) };
  95. } );
  96. }
  97. return { name: elem.name, value: val.replace( rCRLF, "\r\n" ) };
  98. } ).get();
  99. }
  100. } );
  101. export default jQuery;