From 70b0d5d1cc75adf658066c2d7131f46387e7d41d Mon Sep 17 00:00:00 2001 From: Frederic Junod Date: Mon, 29 Jan 2018 16:07:14 +0100 Subject: [PATCH] Check if the sketch feature is defined in finishDrawing Fixes #7035 --- src/ol/interaction/Draw.js | 3 ++ test/spec/ol/interaction/draw.test.js | 62 ++++++++++++++++++++++++--- 2 files changed, 58 insertions(+), 7 deletions(-) diff --git a/src/ol/interaction/Draw.js b/src/ol/interaction/Draw.js index 62ab20fba9..6ae34bad0d 100644 --- a/src/ol/interaction/Draw.js +++ b/src/ol/interaction/Draw.js @@ -714,6 +714,9 @@ Draw.prototype.removeLastPoint = function() { */ Draw.prototype.finishDrawing = function() { const sketchFeature = this.abortDrawing_(); + if (!sketchFeature) { + return; + } let coordinates = this.sketchCoords_; const geometry = /** @type {ol.geom.SimpleGeometry} */ (sketchFeature.getGeometry()); if (this.mode_ === Draw.Mode_.LINE_STRING) { diff --git a/test/spec/ol/interaction/draw.test.js b/test/spec/ol/interaction/draw.test.js index 578d4cac71..b213608a3a 100644 --- a/test/spec/ol/interaction/draw.test.js +++ b/test/spec/ol/interaction/draw.test.js @@ -240,15 +240,24 @@ describe('ol.interaction.Draw', function() { expect(receivedEvents.end).to.be(1); expect(receivedEvents.addfeature).to.be(1); }); + + it('works if finishDrawing is called when the sketch feature is not defined', function() { + expect(function() { + draw.finishDrawing(); + }).to.not.throwException(); + }); + }); describe('drawing multipoints', function() { + let draw; beforeEach(function() { - map.addInteraction(new Draw({ + draw = new Draw({ source: source, type: 'MultiPoint' - })); + }); + map.addInteraction(draw); }); it('draws multipoint on click', function() { @@ -262,6 +271,12 @@ describe('ol.interaction.Draw', function() { expect(geometry.getCoordinates()).to.eql([[30, -15]]); }); + it('works if finishDrawing is called when the sketch feature is not defined', function() { + expect(function() { + draw.finishDrawing(); + }).to.not.throwException(); + }); + }); describe('drawing linestrings', function() { @@ -463,6 +478,12 @@ describe('ol.interaction.Draw', function() { expect(de.callCount).to.be(1); }); + it('works if finishDrawing is called when the sketch feature is not defined', function() { + expect(function() { + draw.finishDrawing(); + }).to.not.throwException(); + }); + }); describe('drawing with a finishCondition', function() { @@ -515,12 +536,14 @@ describe('ol.interaction.Draw', function() { }); describe('drawing multi-linestrings', function() { + let draw; beforeEach(function() { - map.addInteraction(new Draw({ + draw = new Draw({ source: source, type: 'MultiLineString' - })); + }); + map.addInteraction(draw); }); it('draws multi with clicks, finishing on last point', function() { @@ -545,6 +568,12 @@ describe('ol.interaction.Draw', function() { expect(geometry.getCoordinates()).to.eql([[[10, -20], [30, -20]]]); }); + it('works if finishDrawing is called when the sketch feature is not defined', function() { + expect(function() { + draw.finishDrawing(); + }).to.not.throwException(); + }); + }); describe('drawing polygons', function() { @@ -645,7 +674,6 @@ describe('ol.interaction.Draw', function() { }); - it('draws polygon with clicks, finishing on last point', function() { // first point simulateEvent('pointermove', 10, 20); @@ -732,15 +760,23 @@ describe('ol.interaction.Draw', function() { expect(de.callCount).to.be(1); }); + it('works if finishDrawing is called when the sketch feature is not defined', function() { + expect(function() { + draw.finishDrawing(); + }).to.not.throwException(); + }); + }); describe('drawing multi-polygons', function() { + let draw; beforeEach(function() { - map.addInteraction(new Draw({ + draw = new Draw({ source: source, type: 'MultiPolygon' - })); + }); + map.addInteraction(draw); }); it('draws multi with clicks, finishing on first point', function() { @@ -808,6 +844,12 @@ describe('ol.interaction.Draw', function() { ]); }); + it('works if finishDrawing is called when the sketch feature is not defined', function() { + expect(function() { + draw.finishDrawing(); + }).to.not.throwException(); + }); + }); describe('drawing circles', function() { @@ -880,6 +922,12 @@ describe('ol.interaction.Draw', function() { expect(de.callCount).to.be(1); }); + it('works if finishDrawing is called when the sketch feature is not defined', function() { + expect(function() { + draw.finishDrawing(); + }).to.not.throwException(); + }); + }); describe('#setActive()', function() {