Skip to content

Commit 1f0d688

Browse files
JeroenDeDauwclaude
andcommitted
Fix LeafletEditor.remove() crash when no edits were made
self.saveButton is only created after the user makes a change (via _showSaveButton). Calling remove() before any edits crashes with TypeError because self.saveButton is undefined. Add null guard and QUnit tests for remove() in both scenarios. Closes #888 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent e8adf53 commit 1f0d688

3 files changed

Lines changed: 51 additions & 1 deletion

File tree

extension.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@
348348
"leaflet/GeoJsonTest.js",
349349
"leaflet/FeatureBuilderTest.js",
350350
"leaflet/JQueryLeafletTest.js",
351+
"leaflet/LeafletEditorTest.js",
351352
"MapSaverTest.js"
352353
],
353354
"dependencies": [

resources/leaflet/LeafletEditor.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,9 @@
231231

232232
self.remove = function() {
233233
self.drawControl.remove();
234-
self.saveButton.remove();
234+
if (self.saveButton) {
235+
self.saveButton.remove();
236+
}
235237
self.geoJsonLayer.remove();
236238
};
237239

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
( function () {
2+
'use strict';
3+
4+
QUnit.module( 'Maps.LeafletEditor', {
5+
beforeEach: function () {
6+
this.$container = $( '<div>' ).css( { width: '400px', height: '300px' } ).appendTo( '#qunit-fixture' );
7+
this.map = L.map( this.$container[ 0 ], { center: [ 52, 5 ], zoom: 10 } );
8+
this.mapSaver = new window.maps.MapSaver( 'TestPage' );
9+
},
10+
afterEach: function () {
11+
this.map.remove();
12+
}
13+
} );
14+
15+
QUnit.test( 'remove() does not crash when no edits were made', function ( assert ) {
16+
var editor = window.maps.leaflet.LeafletEditor(
17+
this.map,
18+
this.mapSaver
19+
);
20+
21+
editor.initialize( { type: 'FeatureCollection', features: [] } );
22+
23+
// remove() should work even though saveButton was never created
24+
editor.remove();
25+
26+
assert.true( true, 'remove() completed without error' );
27+
} );
28+
29+
QUnit.test( 'remove() works after edits were made', function ( assert ) {
30+
var editor = window.maps.leaflet.LeafletEditor(
31+
this.map,
32+
this.mapSaver
33+
);
34+
35+
editor.initialize( { type: 'FeatureCollection', features: [] } );
36+
37+
// Simulate a created feature to trigger _showSaveButton
38+
this.map.fire( L.Draw.Event.CREATED, {
39+
layer: L.marker( [ 52, 5 ] )
40+
} );
41+
42+
editor.remove();
43+
44+
assert.true( true, 'remove() completed without error after edits' );
45+
} );
46+
47+
}() );

0 commit comments

Comments
 (0)