зеркало из https://github.com/mozilla/ubiquity.git
Utils: Made urlToParams() more accurate. Added test for it.
This commit is contained in:
Родитель
252a71fe8f
Коммит
13ba6c5539
|
@ -519,14 +519,14 @@ function paramsToString(params, prefix) {
|
|||
// values retrieved from its query-part.
|
||||
|
||||
function urlToParams(url) {
|
||||
var params = {};
|
||||
for each (let param in url.slice(url.indexOf("?") + 1).split("&")) {
|
||||
var [key, val] = param.split("=");
|
||||
val = val ? val.replace(/\+/g, " ") : "";
|
||||
var params = {}, dict = {__proto__: null};
|
||||
for each (let param in /^(?:[^?]*\?)?([^#]*)/(url)[1].split("&")) {
|
||||
let [key, val] = /[^=]*(?==?(.*))/(param);
|
||||
val = val.replace(/\+/g, " ");
|
||||
try { key = decodeURIComponent(key) } catch (e) {};
|
||||
try { val = decodeURIComponent(val) } catch (e) {};
|
||||
params[key] = (key in params
|
||||
? [].concat(params[key], val)
|
||||
: val);
|
||||
params[key] = key in dict ? [].concat(params[key], val) : val;
|
||||
dict[key] = 1;
|
||||
}
|
||||
return params;
|
||||
}
|
||||
|
|
|
@ -387,8 +387,21 @@ function testUtilsParamsToString() {
|
|||
this.assertEquals(Utils.paramsToString(data, ""), expected.slice(1));
|
||||
}
|
||||
|
||||
function testUtilsUrlToParams() {
|
||||
this.assertEquals(
|
||||
uneval(Utils.urlToParams("foo=bar")),
|
||||
uneval({foo: "bar"}));
|
||||
this.assertEquals(
|
||||
uneval(Utils.urlToParams("http://foo.com/?bar=baz#quux")),
|
||||
uneval({bar: "baz"}));
|
||||
this.assertEquals(
|
||||
uneval(Utils.urlToParams("?dup=1&dup=2&%3F%26%3D%23=%E6%84%9B=+")),
|
||||
uneval({dup: ["1", "2"], "?&=#": "\u611B= "}));
|
||||
}
|
||||
|
||||
function testUtilsIsArray() {
|
||||
this.assert(Utils.isArray([]));
|
||||
this.assert(!Utils.isArray({}));
|
||||
}
|
||||
|
||||
// This tests bug #25.
|
||||
|
|
Загрузка…
Ссылка в новой задаче