millermedeiros / crossroads.js

JavaScript Routes
http://millermedeiros.github.com/crossroads.js/
1.44k stars 156 forks source link

decodeQueryString method: issue when ID contains '=' #134

Closed daverupp closed 9 years ago

daverupp commented 9 years ago

Hey I have a problem, when I use the query parameter option of crossroads. Because my ID contains of '=' the parsing of the id is failing.

Hey I have a problem, when I use the query parameter option of crossroads. Because my ID contains of '=' the parsing of the id is failing.

Following ID failed to parse with the existing method:

#photos?id=Storage::Photos::Asset::L3Bob3Rvcy9DYW1lcmEg==VXBsb2Fkcy9kYXZlMi5KUEc=

when I change it a little, the ID will be parsed as expected:

function decodeQueryString(str, shouldTypecast) {
    var queryArr = (str || '').replace('?', '').split('&'),
        n = queryArr.length,
        obj = {},
        item, val;
    while (n--) {
        item = queryArr[n].split('=');
        for (var i = 0; i < item.length; i++) {
            if (item[i] === '') {
                item[1] += '=';
            } else {
                if (i > 1) {
                    item[1] = item[1] + '=' + item[i];
                }
            }
        }             
        val = shouldTypecast ? typecastValue(item[1]) : item[1];
        obj[item[0]] = (typeof val === 'string') ? decodeURIComponent(val) : val;
    }
    return obj;
}

Would be cool, when this fix would find the way to your code :) or maybe someone else can use this helpful :) Kind regards Dave