From 268206cec55921d2d0309469ebd5d9533e4f79ee Mon Sep 17 00:00:00 2001 From: Vincent Petry Date: Tue, 28 Jan 2014 16:16:09 +0100 Subject: Fixed parseQueryString to handle empty values and plus signs - now correctly parse query strings with '+' signs - empty values are now parsed either as null or empty string - added unit test for parseQueryString() --- core/js/js.js | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) (limited to 'core/js/js.js') diff --git a/core/js/js.js b/core/js/js.js index 302b6b4d9fa..e907db2837e 100644 --- a/core/js/js.js +++ b/core/js/js.js @@ -371,6 +371,7 @@ var OC={ */ parseQueryString:function(queryString){ var parts, + pos, components, result = {}, key, @@ -378,12 +379,25 @@ var OC={ if (!queryString){ return null; } - if (queryString[0] === '?'){ - queryString = queryString.substr(1); + pos = queryString.indexOf('?'); + if (pos >= 0){ + queryString = queryString.substr(pos + 1); } - parts = queryString.split('&'); + parts = queryString.replace(/\+/g, '%20').split('&'); for (var i = 0; i < parts.length; i++){ - components = parts[i].split('='); + // split on first equal sign + var part = parts[i] + pos = part.indexOf('='); + if (pos >= 0) { + components = [ + part.substr(0, pos), + part.substr(pos + 1) + ] + } + else { + // key only + components = [part]; + } if (!components.length){ continue; } @@ -391,8 +405,14 @@ var OC={ if (!key){ continue; } - value = components[1]; - result[key] = value && decodeURIComponent(value); + // if equal sign was there, return string + if (components.length > 1) { + result[key] = decodeURIComponent(components[1]); + } + // no equal sign => null value + else { + result[key] = null; + } } return result; }, -- cgit v1.2.3