Skip to content

Commit

Permalink
Improve special character handling in formDataUtils
Browse files Browse the repository at this point in the history
Correctly parse plus signs, spaces etc.
  • Loading branch information
tf committed Dec 28, 2016
1 parent 3ab9bc3 commit bbb4a18
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ pageflow.formDataUtils = {
}, },


fromObject: function(object) { fromObject: function(object) {
return _(decodeURIComponent($.param(object)).split('&')).reduce(function(result, param) { var queryString = $.param(object).replace(/\+/g, '%20');

return _(queryString.split('&')).reduce(function(result, param) {
var pair = param.split('='); var pair = param.split('=');
result[pair[0]] = pair[1]; result[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]);


return result; return result;
}, {}); }, {});
Expand Down
14 changes: 14 additions & 0 deletions spec/javascripts/utils/form_data_utils_spec.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@ describe('formDataUtils', function() {
'some[deeply][nested]': 'data' 'some[deeply][nested]': 'data'
}); });
}); });

it('handles spaces, +, = and & signs correctly', function() {
var object = {
some: {
value: '1 + 1 = 2 & a',
}
};

var result = pageflow.formDataUtils.fromObject(object);

expect(result).to.eql({
'some[value]': '1 + 1 = 2 & a',
});
});
}); });


describe('fromModel', function() { describe('fromModel', function() {
Expand Down

0 comments on commit bbb4a18

Please sign in to comment.