From 09937e39b039aff4884312fad1b0c85cb0d3f079 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20S=C3=A1nchez=20Ortega?= Date: Thu, 11 Aug 2016 09:10:46 +0200 Subject: [PATCH] Add a _leaflet_id to the map container to fix #4806 (#4810) --- spec/suites/map/MapSpec.js | 13 +++++++++++++ src/map/Map.js | 16 ++++++++++++---- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/spec/suites/map/MapSpec.js b/spec/suites/map/MapSpec.js index ef8bb641..31d25ac0 100644 --- a/spec/suites/map/MapSpec.js +++ b/spec/suites/map/MapSpec.js @@ -69,6 +69,19 @@ describe("Map", function () { expect(spy.called).to.not.be.ok(); }); + + it("throws error if container is reused by other instance", function () { + var container = document.createElement('div'), + map = L.map(container), + map2; + + map.remove(); + map2 = L.map(container); + + expect(function () { + map.remove(); + }).to.throwException(); + }); }); describe('#getCenter', function () { diff --git a/src/map/Map.js b/src/map/Map.js index 8188c00a..e2e459d8 100644 --- a/src/map/Map.js +++ b/src/map/Map.js @@ -405,11 +405,19 @@ L.Map = L.Evented.extend({ this._initEvents(true); + if (this._containerId !== this._container._leaflet_id) { + throw new Error('Map container is being reused by another instance'); + } + try { // throws error in IE6-8 - delete this._container._leaflet; + delete this._container._leaflet_id; + delete this._containerId; } catch (e) { - this._container._leaflet = undefined; + /*eslint-disable */ + this._container._leaflet_id = undefined; + /*eslint-enable */ + this._containerId = undefined; } L.DomUtil.remove(this._mapPane); @@ -712,12 +720,12 @@ L.Map = L.Evented.extend({ if (!container) { throw new Error('Map container not found.'); - } else if (container._leaflet) { + } else if (container._leaflet_id) { throw new Error('Map container is already initialized.'); } L.DomEvent.addListener(container, 'scroll', this._onScroll, this); - container._leaflet = true; + this._containerId = L.Util.stamp(container); }, _initLayout: function () {