diff --git a/lib/index.js b/lib/index.js index 23eabdb..15ce514 100644 --- a/lib/index.js +++ b/lib/index.js @@ -528,7 +528,9 @@ MapboxGeocoder.prototype = { }, onRemove: function() { - this.container.parentNode.removeChild(this.container); + if (this.container.parentNode) { + this.container.parentNode.removeChild(this.container); + } if (this.options.trackProximity && this._map) { this._map.off('moveend', this._updateProximity); diff --git a/test/test.geocoder.js b/test/test.geocoder.js index d2c5611..fcc4164 100644 --- a/test/test.geocoder.js +++ b/test/test.geocoder.js @@ -1015,6 +1015,17 @@ test('geocoder', function(tt) { t.end(); }) + + tt.test('geocode#onRemove after map removal', function(t){ + setup({marker: true}); + + map.remove(); + + t.doesNotThrow(function() { + geocoder.onRemove(); + }, 'onRemove does not throw when the geocoder container is no longer in the DOM'); + t.end(); + }) tt.test('geocoder#setLanguage', function(t){ setup({language: 'de-DE'}); t.equals(geocoder.options.language, 'de-DE', 'the correct language is set on initialization');