Do not encode slashes in "dir" URL param in files JS

This commit is contained in:
Vincent Petry 2016-05-06 16:13:39 +02:00
parent caefe23bb6
commit 254576e1f7
No known key found for this signature in database
GPG Key ID: AF8F9EFC56562186
3 changed files with 39 additions and 17 deletions

View File

@ -270,6 +270,19 @@
this.navigation.getActiveContainer().trigger(new $.Event('urlChanged', params)); this.navigation.getActiveContainer().trigger(new $.Event('urlChanged', params));
}, },
/**
* Encode URL params into a string, except for the "dir" attribute
* that gets encoded as path where "/" is not encoded
*
* @param {Object.<string>} params
* @return {string} encoded params
*/
_makeUrlParams: function(params) {
var dir = params.dir;
delete params.dir;
return 'dir=' + OC.encodePath(dir) + '&' + OC.buildQueryString(params);
},
/** /**
* Change the URL to point to the given dir and view * Change the URL to point to the given dir and view
*/ */
@ -283,9 +296,9 @@
var currentParams = OC.Util.History.parseUrlQuery(); var currentParams = OC.Util.History.parseUrlQuery();
if (currentParams.dir === params.dir && currentParams.view === params.view && currentParams.fileid !== params.fileid) { if (currentParams.dir === params.dir && currentParams.view === params.view && currentParams.fileid !== params.fileid) {
// if only fileid changed or was added, replace instead of push // if only fileid changed or was added, replace instead of push
OC.Util.History.replaceState(params); OC.Util.History.replaceState(this._makeUrlParams(params));
} else { } else {
OC.Util.History.pushState(params); OC.Util.History.pushState(this._makeUrlParams(params));
} }
} }
}; };

View File

@ -122,35 +122,39 @@ describe('OCA.Files.App tests', function() {
describe('URL handling', function() { describe('URL handling', function() {
it('pushes the state to the URL when current app changed directory', function() { it('pushes the state to the URL when current app changed directory', function() {
$('#app-content-files').trigger(new $.Event('changeDirectory', {dir: 'subdir'})); $('#app-content-files').trigger(new $.Event('changeDirectory', {dir: 'sub dir'}));
expect(pushStateStub.calledOnce).toEqual(true); expect(pushStateStub.calledOnce).toEqual(true);
expect(pushStateStub.getCall(0).args[0].dir).toEqual('subdir'); var params = OC.parseQueryString(pushStateStub.getCall(0).args[0]);
expect(pushStateStub.getCall(0).args[0].view).not.toBeDefined(); expect(params.dir).toEqual('sub dir');
expect(params.view).not.toBeDefined();
$('li[data-id=other]>a').click(); $('li[data-id=other]>a').click();
pushStateStub.reset(); pushStateStub.reset();
$('#app-content-other').trigger(new $.Event('changeDirectory', {dir: 'subdir'})); $('#app-content-other').trigger(new $.Event('changeDirectory', {dir: 'sub dir'}));
expect(pushStateStub.calledOnce).toEqual(true); expect(pushStateStub.calledOnce).toEqual(true);
expect(pushStateStub.getCall(0).args[0].dir).toEqual('subdir'); params = OC.parseQueryString(pushStateStub.getCall(0).args[0]);
expect(pushStateStub.getCall(0).args[0].view).toEqual('other'); expect(params.dir).toEqual('sub dir');
expect(params.view).toEqual('other');
}); });
it('replaces the state to the URL when fileid is known', function() { it('replaces the state to the URL when fileid is known', function() {
$('#app-content-files').trigger(new $.Event('changeDirectory', {dir: 'subdir'})); $('#app-content-files').trigger(new $.Event('changeDirectory', {dir: 'sub dir'}));
expect(pushStateStub.calledOnce).toEqual(true); expect(pushStateStub.calledOnce).toEqual(true);
expect(pushStateStub.getCall(0).args[0].dir).toEqual('subdir'); var params = OC.parseQueryString(pushStateStub.getCall(0).args[0]);
expect(pushStateStub.getCall(0).args[0].view).not.toBeDefined(); expect(params.dir).toEqual('sub dir');
expect(params.view).not.toBeDefined();
expect(replaceStateStub.notCalled).toEqual(true); expect(replaceStateStub.notCalled).toEqual(true);
parseUrlQueryStub.returns({dir: 'subdir'}); parseUrlQueryStub.returns({dir: 'sub dir'});
$('#app-content-files').trigger(new $.Event('afterChangeDirectory', {dir: 'subdir', fileId: 123})); $('#app-content-files').trigger(new $.Event('afterChangeDirectory', {dir: 'sub dir', fileId: 123}));
expect(pushStateStub.calledOnce).toEqual(true); expect(pushStateStub.calledOnce).toEqual(true);
expect(replaceStateStub.calledOnce).toEqual(true); expect(replaceStateStub.calledOnce).toEqual(true);
expect(replaceStateStub.getCall(0).args[0].dir).toEqual('subdir'); params = OC.parseQueryString(replaceStateStub.getCall(0).args[0]);
expect(replaceStateStub.getCall(0).args[0].view).not.toBeDefined(); expect(params.dir).toEqual('sub dir');
expect(replaceStateStub.getCall(0).args[0].fileid).toEqual(123); expect(params.view).not.toBeDefined();
expect(params.fileid).toEqual('123');
}); });
describe('onpopstate', function() { describe('onpopstate', function() {
it('sends "urlChanged" event to current app', function() { it('sends "urlChanged" event to current app', function() {

View File

@ -2160,7 +2160,12 @@ OC.Util.History = {
if (!this._handlers.length) { if (!this._handlers.length) {
return; return;
} }
params = (e && e.state) || this.parseUrlQuery() || {}; params = (e && e.state);
if (_.isString(params)) {
params = OC.parseQueryString(params);
} else if (!params) {
params = this.parseUrlQuery() || {};
}
for (var i = 0; i < this._handlers.length; i++) { for (var i = 0; i < this._handlers.length; i++) {
this._handlers[i](params); this._handlers[i](params);
} }