Hybrid View
-
29 Mar 2012 12:14 PM #1
Ext.urlDecode does not decode "+" (plus sign) as space
Ext.urlDecode does not decode "+" (plus sign) as space
Sencha Touch version tested:
- 1.1.1
Platform tested against:- iOS 5
- Chrome 17 on XP
Description:- Ext.urlDecode does not decode the + (plus sign) as a space.
Let me back up and say I was a little torn on whether to file this as a bug or a feature request. If the purpose of urlDecode is to simply decode URLs (or more specifically, URIs), then there really is no "bug" because the URI standards don't say much on topic.
However, if the purpose of urlDecode is to decode query parameters that are originally passed in via a URL, I'd consider this very lacking behavior. While it is also completely harmless to encode a space as %20, both the html4 and html5 specs for application/x-www-form-urlencoded data say that the "right" way to do it is to encode space as "+".
Since that's the only defined way of building query params, that's the way a whole lot of applications are going to pass those params. Hence when interacting with other applications, you might get your parameters with +s instead of %20s.
Now, if you're only EVER using Ext.urlDecode() to decode something that Ext.urlEncode() encoding (meaning you're not interacting with anything), you're completely safe.
There's a pretty good discussion of all the issues here:
http://unixpapa.com/js/querystring.html
He basically came to the same conclusions I did in the sections "2. Query String Standards (or the Lack Thereof)" and "3.1. The Built-In Functions Are Not Meant for Query Strings". He also suggested the same workaround I have. Basically, you just replace the +s with spaces before calling the underlying decodeURIComponent() function.
Test Case:
Steps to reproduce the problem:Code:console.log(Ext.urlDecode("param=one+two+three").param)- Run the above code on the console
The result that was expected:- one two three
The result that occurs instead:- one+two+three
Possible fix:
Code:urlDecode : function(string, overwrite) { if (Ext.isEmpty(string)) { return {}; } var obj = {}, pairs = string.replace(/\+/g, ' ').split('&'), d = decodeURIComponent, name, value; Ext.each(pairs, function(pair) { pair = pair.split('='); name = d(pair[0]); value = d(pair[1]); obj[name] = overwrite || !obj[name] ? value : [].concat(obj[name]).concat(value); }); return obj; }
-
29 Mar 2012 12:30 PM #2Sencha - Senior Forum Manager
- Join Date
- Mar 2007
- Location
- St. Louis, MO
- Posts
- 33,656
- Vote Rating
- 435
Thank you for the report.
Mitchell Simoens @SenchaMitch
Sencha Inc, Senior Forum Manager
________________
http://www.JSONPLint.com - Source to lint your JSONP!
Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
https://github.com/mitchellsimoens
Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/
Need more help with your app? Hire Sencha Services services@sencha.com
Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is almost in print!
When posting code, please use BBCode's CODE tags.
You found a bug! We've classified it as
TOUCH-2629
.
We encourage you to continue the discussion and to find an acceptable workaround while we work on a permanent fix.


Reply With Quote