1
0

serialize.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. module("serialize", { teardown: moduleTeardown });
  2. test("jQuery.param()", function() {
  3. expect(22);
  4. var params, settings;
  5. equal( !( jQuery.ajaxSettings && jQuery.ajaxSettings.traditional ), true, "traditional flag, falsy by default" );
  6. params = {"foo":"bar", "baz":42, "quux":"All your base are belong to us"};
  7. equal( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
  8. params = {"string":"foo","null":null,"undefined":undefined};
  9. equal( jQuery.param(params), "string=foo&null=&undefined=", "handle nulls and undefineds properly" );
  10. params = {"someName": [1, 2, 3], "regularThing": "blah" };
  11. equal( jQuery.param(params), "someName%5B%5D=1&someName%5B%5D=2&someName%5B%5D=3&regularThing=blah", "with array" );
  12. params = {"foo": ["a", "b", "c"]};
  13. equal( jQuery.param(params), "foo%5B%5D=a&foo%5B%5D=b&foo%5B%5D=c", "with array of strings" );
  14. params = {"foo": ["baz", 42, "All your base are belong to us"] };
  15. equal( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
  16. params = {"foo": { "bar": "baz", "beep": 42, "quux": "All your base are belong to us" } };
  17. equal( jQuery.param(params), "foo%5Bbar%5D=baz&foo%5Bbeep%5D=42&foo%5Bquux%5D=All+your+base+are+belong+to+us", "even more arrays" );
  18. params = { a:[1,2], b:{ c:3, d:[4,5], e:{ x:[6], y:7, z:[8,9] }, f:true, g:false, h:undefined }, i:[10,11], j:true, k:false, l:[undefined,0], m:"cowboy hat?" };
  19. equal( decodeURIComponent( jQuery.param(params) ), "a[]=1&a[]=2&b[c]=3&b[d][]=4&b[d][]=5&b[e][x][]=6&b[e][y]=7&b[e][z][]=8&b[e][z][]=9&b[f]=true&b[g]=false&b[h]=&i[]=10&i[]=11&j=true&k=false&l[]=&l[]=0&m=cowboy+hat?", "huge structure" );
  20. params = { "a": [ 0, [ 1, 2 ], [ 3, [ 4, 5 ], [ 6 ] ], { "b": [ 7, [ 8, 9 ], [ { "c": 10, "d": 11 } ], [ [ 12 ] ], [ [ [ 13 ] ] ], { "e": { "f": { "g": [ 14, [ 15 ] ] } } }, 16 ] }, 17 ] };
  21. equal( decodeURIComponent( jQuery.param(params) ), "a[]=0&a[1][]=1&a[1][]=2&a[2][]=3&a[2][1][]=4&a[2][1][]=5&a[2][2][]=6&a[3][b][]=7&a[3][b][1][]=8&a[3][b][1][]=9&a[3][b][2][0][c]=10&a[3][b][2][0][d]=11&a[3][b][3][0][]=12&a[3][b][4][0][0][]=13&a[3][b][5][e][f][g][]=14&a[3][b][5][e][f][g][1][]=15&a[3][b][]=16&a[]=17", "nested arrays" );
  22. params = { "a":[1,2], "b":{ "c":3, "d":[4,5], "e":{ "x":[6], "y":7, "z":[8,9] }, "f":true, "g":false, "h":undefined }, "i":[10,11], "j":true, "k":false, "l":[undefined,0], "m":"cowboy hat?" };
  23. equal( jQuery.param(params,true), "a=1&a=2&b=%5Bobject+Object%5D&i=10&i=11&j=true&k=false&l=&l=0&m=cowboy+hat%3F", "huge structure, forced traditional" );
  24. equal( decodeURIComponent( jQuery.param({ "a": [1,2,3], "b[]": [4,5,6], "c[d]": [7,8,9], "e": { "f": [10], "g": [11,12], "h": 13 } }) ), "a[]=1&a[]=2&a[]=3&b[]=4&b[]=5&b[]=6&c[d][]=7&c[d][]=8&c[d][]=9&e[f][]=10&e[g][]=11&e[g][]=12&e[h]=13", "Make sure params are not double-encoded." );
  25. // #7945
  26. equal( jQuery.param({"jquery": "1.4.2"}), "jquery=1.4.2", "Check that object with a jQuery property get serialized correctly" );
  27. settings = { traditional: true };
  28. if ( jQuery.ajaxSettings ) {
  29. jQuery.ajaxSetup( settings );
  30. } else {
  31. jQuery.ajaxSettings = settings;
  32. }
  33. params = {"foo":"bar", "baz":42, "quux":"All your base are belong to us"};
  34. equal( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
  35. params = {"someName": [1, 2, 3], "regularThing": "blah" };
  36. equal( jQuery.param(params), "someName=1&someName=2&someName=3&regularThing=blah", "with array" );
  37. params = {"foo": ["a", "b", "c"]};
  38. equal( jQuery.param(params), "foo=a&foo=b&foo=c", "with array of strings" );
  39. params = {"foo[]":["baz", 42, "All your base are belong to us"]};
  40. equal( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
  41. params = {"foo[bar]":"baz", "foo[beep]":42, "foo[quux]":"All your base are belong to us"};
  42. equal( jQuery.param(params), "foo%5Bbar%5D=baz&foo%5Bbeep%5D=42&foo%5Bquux%5D=All+your+base+are+belong+to+us", "even more arrays" );
  43. params = { a:[1,2], b:{ c:3, d:[4,5], e:{ x:[6], y:7, z:[8,9] }, f:true, g:false, h:undefined }, i:[10,11], j:true, k:false, l:[undefined,0], m:"cowboy hat?" };
  44. equal( jQuery.param(params), "a=1&a=2&b=%5Bobject+Object%5D&i=10&i=11&j=true&k=false&l=&l=0&m=cowboy+hat%3F", "huge structure" );
  45. params = { "a": [ 0, [ 1, 2 ], [ 3, [ 4, 5 ], [ 6 ] ], { "b": [ 7, [ 8, 9 ], [ { "c": 10, d: 11 } ], [ [ 12 ] ], [ [ [ 13 ] ] ], { "e": { "f": { "g": [ 14, [ 15 ] ] } } }, 16 ] }, 17 ] };
  46. equal( jQuery.param(params), "a=0&a=1%2C2&a=3%2C4%2C5%2C6&a=%5Bobject+Object%5D&a=17", "nested arrays (not possible when jQuery.param.traditional == true)" );
  47. params = { a:[1,2], b:{ c:3, d:[4,5], e:{ x:[6], y:7, z:[8,9] }, f:true, g:false, h:undefined }, i:[10,11], j:true, k:false, l:[undefined,0], m:"cowboy hat?" };
  48. equal( decodeURIComponent( jQuery.param(params,false) ), "a[]=1&a[]=2&b[c]=3&b[d][]=4&b[d][]=5&b[e][x][]=6&b[e][y]=7&b[e][z][]=8&b[e][z][]=9&b[f]=true&b[g]=false&b[h]=&i[]=10&i[]=11&j=true&k=false&l[]=&l[]=0&m=cowboy+hat?", "huge structure, forced not traditional" );
  49. params = { "param1": null };
  50. equal( jQuery.param(params,false), "param1=", "Make sure that null params aren't traversed." );
  51. params = {"test": {"length": 3, "foo": "bar"} };
  52. equal( jQuery.param( params, false ), "test%5Blength%5D=3&test%5Bfoo%5D=bar", "Sub-object with a length property" );
  53. if ( jQuery.ajaxSettings === settings ) {
  54. delete jQuery.ajaxSettings;
  55. } else {
  56. jQuery.ajaxSetup({ traditional: false });
  57. }
  58. });
  59. test("jQuery.param() Constructed prop values", function() {
  60. expect( 4 );
  61. /** @constructor */
  62. function Record() {
  63. this["prop"] = "val";
  64. }
  65. var MyString = String,
  66. MyNumber = Number,
  67. params = { "test": new MyString("foo") };
  68. equal( jQuery.param( params, false ), "test=foo", "Do not mistake new String() for a plain object" );
  69. params = { "test": new MyNumber(5) };
  70. equal( jQuery.param( params, false ), "test=5", "Do not mistake new Number() for a plain object" );
  71. params = { "test": new Date() };
  72. ok( jQuery.param( params, false ), "(Non empty string returned) Do not mistake new Date() for a plain object" );
  73. // should allow non-native constructed objects
  74. params = { "test": new Record() };
  75. equal( jQuery.param( params, false ), jQuery.param({ "test": { "prop": "val" } }), "Allow non-native constructed objects" );
  76. });
  77. test("serialize()", function() {
  78. expect(5);
  79. // Add html5 elements only for serialize because selector can't yet find them on non-html5 browsers
  80. jQuery("#search").after(
  81. "<input type='email' id='html5email' name='email' value='dave@jquery.com' />" +
  82. "<input type='number' id='html5number' name='number' value='43' />" +
  83. "<input type='file' name='fileupload' />"
  84. );
  85. equal( jQuery("#form").serialize(),
  86. "action=Test&radio2=on&check=on&hidden=&foo%5Bbar%5D=&name=name&search=search&email=dave%40jquery.com&number=43&select1=&select2=3&select3=1&select3=2&select5=3",
  87. "Check form serialization as query string");
  88. equal( jQuery("input,select,textarea,button", "#form").serialize(),
  89. "action=Test&radio2=on&check=on&hidden=&foo%5Bbar%5D=&name=name&search=search&email=dave%40jquery.com&number=43&select1=&select2=3&select3=1&select3=2&select5=3",
  90. "Check input serialization as query string");
  91. equal( jQuery("#testForm").serialize(),
  92. "T3=%3F%0D%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=",
  93. "Check form serialization as query string");
  94. equal( jQuery("input,select,textarea,button", "#testForm").serialize(),
  95. "T3=%3F%0D%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=",
  96. "Check input serialization as query string");
  97. equal( jQuery("#form, #testForm").serialize(),
  98. "action=Test&radio2=on&check=on&hidden=&foo%5Bbar%5D=&name=name&search=search&email=dave%40jquery.com&number=43&select1=&select2=3&select3=1&select3=2&select5=3&T3=%3F%0D%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=",
  99. "Multiple form serialization as query string");
  100. /* Temporarily disabled. Opera 10 has problems with form serialization.
  101. equal( jQuery("#form, #testForm :input").serialize(),
  102. "action=Test&radio2=on&check=on&hidden=&foo%5Bbar%5D=&name=name&search=search&email=dave%40jquery.com&number=43&select1=&select2=3&select3=1&select3=2&T3=%3F%0D%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=",
  103. "Mixed form/input serialization as query string");
  104. */
  105. jQuery("#html5email, #html5number").remove();
  106. });