From dd6acb0f90c600f7f4314839b454c3b107d1d041 Mon Sep 17 00:00:00 2001 From: Christopher Pruijsen Date: Sun, 13 Sep 2026 10:44:58 +0000 Subject: [PATCH] Fix null dereference in onRemove when container is detached When a map is removed before the geocoder control is removed (e.g. map.remove() followed by map.removeControl(geocoder)), the geocoder's container is already detached from the DOM and parentNode is null, causing a TypeError on removeChild. Guard the removal so onRemove is safe to call after the container has been detached. Fixes #480 --- lib/index.js | 4 +++- test/test.geocoder.js | 11 +++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) 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');