Skip to content

Commit

Permalink
$.serialize: avoiding encoding "%20" as "+", for better alignment wit…
Browse files Browse the repository at this point in the history
…h jQuery
  • Loading branch information
vovayatsyuk authored Mar 8, 2023
1 parent e7f9460 commit 504bec9
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
3 changes: 1 addition & 2 deletions src/forms/helpers/query_encode.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@

const queryEncodeSpaceRe = /%20/g;
const queryEncodeCRLFRe = /\r?\n/g;

function queryEncode ( prop: string, value: string ): string {

return `&${encodeURIComponent ( prop )}=${encodeURIComponent ( value.replace ( queryEncodeCRLFRe, '\r\n' ) ).replace ( queryEncodeSpaceRe, '+' )}`;
return `&${encodeURIComponent ( prop )}=${encodeURIComponent ( value.replace ( queryEncodeCRLFRe, '\r\n' ) )}`;

}
10 changes: 5 additions & 5 deletions test/modules/forms.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
var fixture = '\
<form class="form">\
<input type="hidden" value="5" name="hidden"/>\
<input type="text" value="text" name="text"/>\
<input type="text" value="text with spaces" name="text"/>\
<input type="text" value="disabled" name="disabled-check" disabled />\
<input type="checkbox" value="yes" checked="checked" name="checkbox-yes" />\
<input type="checkbox" value="no" name="checkbox-no" />\
Expand Down Expand Up @@ -32,23 +32,23 @@ describe ( 'Forms', { beforeEach: getFixtureInit ( fixture ) }, function () {

var val = $('.form').serialize ();

t.is ( val, 'hidden=5&text=text&checkbox-yes=yes&radio=yes&select=selected&select-multiple=option-1&select-multiple=option-2' );
t.is ( val, 'hidden=5&text=text%20with%20spaces&checkbox-yes=yes&radio=yes&select=selected&select-multiple=option-1&select-multiple=option-2' );

});

it ( 'serializes an element', function ( t ) {

var val = $('.form input[type=text]').serialize ();

t.is ( val, 'text=text' );
t.is ( val, 'text=text%20with%20spaces' );

});

it ( 'serializes multiple form elements', function ( t ) {

var val = $('.form input, .form textarea, .form select').serialize ();

t.is ( val, 'hidden=5&text=text&checkbox-yes=yes&radio=yes&select=selected&select-multiple=option-1&select-multiple=option-2' );
t.is ( val, 'hidden=5&text=text%20with%20spaces&checkbox-yes=yes&radio=yes&select=selected&select-multiple=option-1&select-multiple=option-2' );

});

Expand All @@ -68,7 +68,7 @@ describe ( 'Forms', { beforeEach: getFixtureInit ( fixture ) }, function () {

var val = $('.form input[type=text]').val ();

t.is ( val, 'text' );
t.is ( val, 'text with spaces' );

});

Expand Down

0 comments on commit 504bec9

Please sign in to comment.