Fix history api URI encoding

The history API encoded components multiple times, e.g. causing a [ to
be converted to %5B in the first link, then to %255B on the second link,
%25255B on the third, etc.

refs #4408
This commit is contained in:
Jannis Moßhammer 2013-07-12 11:51:59 +02:00 committed by Marius Hein
parent 4592e7bd65
commit e0f0e1fc13
3 changed files with 67 additions and 9 deletions

View File

@ -10,14 +10,18 @@
var pending = {
};
var encodeForURL = function(param) {
return encodeURIComponent(param);
};
var getCurrentGETParameters = function() {
var currentGET = window.location.search.substring(1).split("&");
var params = {};
if(currentGET.length > 0) {
$.each(currentGET, function(idx, elem) {
var keyVal = elem.split("=");
params[encodeURIComponent(keyVal[0])] = encodeURIComponent(keyVal[1]);
params[keyVal[0]] = encodeForURL(keyVal[1]);
});
}
return params;
@ -26,14 +30,17 @@
var pushGet = function(param, value, url) {
url = url || (window.location.origin+window.location.pathname);
var params = getCurrentGETParameters();
params[encodeURIComponent(param)] = encodeURIComponent(value);
params[param] = encodeForURL(value);
var search = "?";
for (var name in params) {
if(search != "?")
if (name === "" || typeof params[name] == "undefined") {
continue;
}
if (search != "?")
search += "&";
search += name+"="+params[name];
}
return url+search+"#"+window.location.hash;
};
@ -143,12 +150,11 @@
req.destination = destination;
}
if (destination == "icinga-main") {
History.pushState(data, document.title, url);
history.pushState(data, document.title, url);
} else {
url = pushGet("c["+destination+"]", url);
History.pushState(data, document.title, url);
history.pushState(data, document.title, url);
}
console.log("New url: ", url);
return req;
};

View File

@ -10,13 +10,14 @@ requirejs.config({
"raphael.vml": 'vendor/raphael/raphael.vml',
'ace' : 'vendor/ace/ace',
"Holder": 'vendor/holder',
"History": 'vendor/history',
logging: 'icinga/util/logging'
}
});
define(['jquery','Holder'], function ($) {
define(['jquery','Holder', 'History'], function ($) {
requirejs(['bootstrap']);
requirejs(['icinga/icinga'], function (Icinga) {
window.$ = $;

View File

@ -0,0 +1,51 @@
/**
*
* Regression test for #4408
# History api double encodes and causes messy behaviour
*
**/
var i2w = require('./i2w-config');
var casper = i2w.getTestEnv();
var URL = "http://localhost:12999";
var firstLink = "/fragments/testFragment1.html?c[test]=test_test";
var secondLink = "/fragments/testFragment3.html?this=is_a_param";
casper.start(URL+"/generic.html");
casper.then(function() {
casper.page.evaluate(i2w.setupRequireJs, {icinga: true});
});
casper.then(function() {
casper.page.evaluate(function() {
requirejs(["icinga/icinga"], function(icinga) {
icinga.loadUrl("/fragments/testFragment1.html?c[test]=test_test");
});
});
casper.waitForSelector("div#icinga-main a", onFirstCall);
});
/**
* First call of the loadUrl
**/
var onFirstCall = function() {
this.test.assertUrlMatch(URL+firstLink);
casper.page.evaluate(function() {
requirejs(["icinga/icinga"], function(icinga) {
icinga.loadUrl("/fragments/testFragment3.html?this=is_a_param", "icinga-detail");
});
});
this.wait(400, function() {
var expected =
URL +
firstLink+"&c[icinga-detail]=" +
secondLink;
this.test.assertUrlMatch(expected);
});
};
casper.run(function() {
this.test.done();
});