From c9370b937d369c125d64243b095c1a9bc2b5e75c Mon Sep 17 00:00:00 2001 From: ryanhamley Date: Thu, 2 Jan 2020 15:03:41 -0800 Subject: [PATCH 1/2] Prevent changing bearing via URL hash when rotation is disabled --- src/ui/hash.js | 3 ++- test/unit/ui/hash.test.js | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/ui/hash.js b/src/ui/hash.js index fccfea62d83..4a6e17a7ac7 100644 --- a/src/ui/hash.js +++ b/src/ui/hash.js @@ -119,10 +119,11 @@ class Hash { _onHashChange() { const loc = this._getCurrentHash(); if (loc.length >= 3 && !loc.some(v => isNaN(v))) { + const bearing = this._map.dragRotate.isEnabled() && this._map.touchZoomRotate.isEnabled() ? +loc[3] : this._map.getBearing() || 0; this._map.jumpTo({ center: [+loc[2], +loc[1]], zoom: +loc[0], - bearing: +(loc[3] || 0), + bearing, pitch: +(loc[4] || 0) }); return true; diff --git a/test/unit/ui/hash.test.js b/test/unit/ui/hash.test.js index e81c1d14d33..e1be22ab4ef 100644 --- a/test/unit/ui/hash.test.js +++ b/test/unit/ui/hash.test.js @@ -57,6 +57,11 @@ test('hash', (t) => { t.equal(map.getBearing(), 0); t.equal(map.getPitch(), 0); + // map is created with `interactive: false` + // so explicitly enable rotation for this test + map.dragRotate.enable(); + map.touchZoomRotate.enable(); + window.location.hash = '#5/1.00/0.50/30/60'; hash._onHashChange(); @@ -67,6 +72,33 @@ test('hash', (t) => { t.equal(map.getBearing(), 30); t.equal(map.getPitch(), 60); + // disable rotation to test that updating + // the hash's bearing won't change the map + map.dragRotate.disable(); + map.touchZoomRotate.disable(); + + window.location.hash = '#5/1.00/0.50/-45/60'; + + hash._onHashChange(); + + t.equal(map.getCenter().lng, 0.5); + t.equal(map.getCenter().lat, 1); + t.equal(map.getZoom(), 5); + t.equal(map.getBearing(), 30); + t.equal(map.getPitch(), 60); + + // test that a hash with no bearing resets + // to the previous bearing when rotation is disabled + window.location.hash = '#5/1.00/0.50/'; + + hash._onHashChange(); + + t.equal(map.getCenter().lng, 0.5); + t.equal(map.getCenter().lat, 1); + t.equal(map.getZoom(), 5); + t.equal(map.getBearing(), 30); + t.equal(window.location.hash, '#5/1/0.5/30'); + window.location.hash = '#4/wrongly/formed/hash'; t.false(hash._onHashChange()); From 15189fbfa4aa9d6a2605edcdd36c6fc1089acbd9 Mon Sep 17 00:00:00 2001 From: ryanhamley Date: Fri, 3 Jan 2020 11:29:05 -0800 Subject: [PATCH 2/2] Remove superfluous null guard --- src/ui/hash.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ui/hash.js b/src/ui/hash.js index 4a6e17a7ac7..81935c01e64 100644 --- a/src/ui/hash.js +++ b/src/ui/hash.js @@ -119,7 +119,7 @@ class Hash { _onHashChange() { const loc = this._getCurrentHash(); if (loc.length >= 3 && !loc.some(v => isNaN(v))) { - const bearing = this._map.dragRotate.isEnabled() && this._map.touchZoomRotate.isEnabled() ? +loc[3] : this._map.getBearing() || 0; + const bearing = this._map.dragRotate.isEnabled() && this._map.touchZoomRotate.isEnabled() ? +loc[3] : this._map.getBearing(); this._map.jumpTo({ center: [+loc[2], +loc[1]], zoom: +loc[0],