Router parsing of fragment hash causes information to be lost or misinterpreted
The general code setup:
Backbone.Router.extend({ routes: { '*query': 'onQueryRoute' }, initialize: function() { Backbone.history.start(); }, onQueryRoute: function(query) { // "query" is impossible to reliably parse as a query string // due to full-string url-decode on line 1306 of backbone.js // inside of _extractParameters(). } });
Here's the scenario. Imagine I have a single query parameter with an arbitrary value that happens to contain "1&bar=2". So I construct my fragment string like so:
var value = encodeURIComponent("1&bar=2"); router.navigate("foo=" + value);
When I refresh the page and my onQueryRoute event handler gets called, I will receive this string: "foo=1&bar=2", which is obviously impossible to parse now because it can be interpreted as two query parameters when it should actually only be one. This is happening because on line 1306 inside of _extractParamters(), you are url-decoding the whole fragment hash without splitting it first.
In my particular use-case, the query parameters are not fixed; there can be an arbitrary number of them so I can't easily set a route regex that automatically parses the individual values. The structure of my URL is like this: key1[0]=value1&key2[0]=value2&key1[1]=...&foo=bar&baz=blah
The execute() preprocessor method is useless here because it gets called after _extractParameters() has mangled the hash string by url-decoding it whole without splitting it first by default. My current workaround is to override the method with this: _extractParameters: function(route, fragment) { return Backbone.Router.prototype._extractParameters .call(this, route, encodeURIComponent(fragment)); }
I basically url-encode it again before passing it back to the original implementation so that when I get it in my callback, I get the original version that I can proceed to parse normally. This is not very efficient and I'd like to see this fixed properly in the source.
Thoughts?
Hi @enunez! What version of Backbone are you using? The treatment of query strings in routing has been updated recently (#2890). Using the latest version, the query string will be passed as the last argument to the route handler.
Hi @braddunbar, I'm using the latest release code, 1.1.2. The issue occurs at the end of _extractParameters() by calling decodeURIComponent() on the entire fragment instead of looping through the individual parameters by splitting on &.
_extractParameters: function(route, fragment) { var params = route.exec(fragment).slice(1); return _.map(params, function(param, i) { // Don't decode the search params. if (i === params.length - 1) return param || null; return param ? decodeURIComponent(param) : null; }); }
Ok, so I misunderstood because I missed that you're navigating directly to foo=1. You're gonna want to put that into a query string instead, as in router.navigate('path?foo=1'). The query string will be the last argument to the router and won't be decoded.
@braddunbar I think you're misunderstanding. I am not navigating to "foo=1" in my contrived example. I am navigating to foo=1%26bar%3D2. Please read what I explained. Just imagine anyone tries to set a query parameter value to something that contains an "&" character. Backbone's current behavior will make it impossible to parse.
Right, I've got that. Using router.navigate('path?foo=1&bar=2') is the same issue. Either way, you'll need to use a query string prefixed with ?.
@braddunbar Hmm. Ok, I'm a bit confused. What does "path?" correspond to there? Is it just an arbitrary string that will act as a "hack" to get Backbone to not url-decode the query string? If so, aren't we just polluting the url with unnecessary cruft? Surely we can come up with a better solution. I tested your solution and it does indeed work, but now I have a pointless/bogus pathname in my url.
Right, it's just an example. You can just use router.navigate('?foo=1&bar=2') if you like.
I'll still have a superfluous "?" in my url, but I can live with that :) Thanks for your help.
Sure thing, glad I could help!
@braddunbar This is not working for me with Backbone 1.3.3.
I'm using Backbone.history.navigate("?q=" + encodeURIComponent(searchQuery)) to change the URL when submitting a search form.
In my case, searchQuery could be something like "max müller", so I encode it with encodeURIComponent into "max+m%C3%BCller". But within Backbones navigate function, my searchQuery is beeing decoded back to "max müller" and returned like that.
So at the end the URL gets changed into "myroot.com/?q=max müller", which is not a valid URL. Instead it should be like this: "myroot.com/?q=max+m%C3%BCller".
Am I missing something? This should be the standard behavior or not? Is there a way to achieve it beside modifying Backbones core?