From 2eb73c4757b73493455b66b6d031fb51f992acf4 Mon Sep 17 00:00:00 2001 From: Niels Holt Date: Fri, 29 Oct 2021 16:36:57 +0200 Subject: [PATCH] Fix Path.setStyle bug (replacing #6671) (#6941) * Fix issue #6662 and #6664 * Add unit test for Path weight set error * Simplify Polyline weight set test case * Alternative fix for setting weight after empty Polyline is added to the map * Add unit test for setting weight after empty Polygon is added to the map * Return when empty Polyline is being rendered * More general formulation for test * Minor refactoring to make tests more uniform Co-authored-by: fodor0205 Co-authored-by: johndoe --- spec/suites/layer/vector/PolygonSpec.js | 34 ++++++++++++++++++------ spec/suites/layer/vector/PolylineSpec.js | 22 ++++++++++++++- src/layer/vector/Polyline.js | 5 ++++ 3 files changed, 52 insertions(+), 9 deletions(-) diff --git a/spec/suites/layer/vector/PolygonSpec.js b/spec/suites/layer/vector/PolygonSpec.js index 5be116aff1c..5f6ca64dfb5 100644 --- a/spec/suites/layer/vector/PolygonSpec.js +++ b/spec/suites/layer/vector/PolygonSpec.js @@ -1,4 +1,14 @@ describe('Polygon', function () { + var map; + + before(function () { + map = new L.Map(document.createElement('div'), {center: [55.8, 37.6], zoom: 6}); + }); + + after(function () { + map.remove(); + }); + describe("#initialize", function () { it("should never be flat", function () { var latLngs = [[1, 2], [3, 4]]; @@ -59,10 +69,8 @@ describe('Polygon', function () { }); it("can be added to the map when empty", function () { - var map = new L.Map(document.createElement('div')); var polygon = new L.Polygon([]).addTo(map); var isAdded = map.hasLayer(polygon); - map.remove(); // clean up expect(isAdded).to.be(true); }); @@ -140,12 +148,6 @@ describe('Polygon', function () { }); describe('#getCenter', function () { - var map = new L.Map(document.createElement('div'), {center: [55.8, 37.6], zoom: 6}); - - after(function () { - map.remove(); - }); - it('should compute center of a big simple polygon around equator', function () { var latlngs = [ [[0, 0], [10, 0], [10, 10], [0, 10]] @@ -322,4 +324,20 @@ describe('Polygon', function () { expect(polygon._latlngs[1][1]).to.eql([L.latLng([2, 3]), L.latLng([2, 4]), L.latLng([3, 4]), L.latLng([2, 2])]); }); }); + + describe("#setStyle", function () { + it("succeeds for empty Polygon already added to the map", function () { + var style = { + weight: 3 + }; + var polygon = L.polygon([]); + + polygon.addTo(map); + polygon.setStyle(style); + + for (var prop in style) { + expect(polygon.options[prop]).to.be(style[prop]); + } + }); + }); }); diff --git a/spec/suites/layer/vector/PolylineSpec.js b/spec/suites/layer/vector/PolylineSpec.js index 00c9bd7b1a7..b5e4e867200 100644 --- a/spec/suites/layer/vector/PolylineSpec.js +++ b/spec/suites/layer/vector/PolylineSpec.js @@ -1,5 +1,9 @@ describe('Polyline', function () { - var map = new L.Map(document.createElement('div'), {center: [55.8, 37.6], zoom: 6}); + var map; + + before(function () { + map = new L.Map(document.createElement('div'), {center: [55.8, 37.6], zoom: 6}); + }); after(function () { map.remove(); @@ -212,4 +216,20 @@ describe('Polyline', function () { expect(polyline._latlngs).to.eql([L.latLng([1, 2])]); }); }); + + describe("#setStyle", function () { + it("succeeds for empty Polyline already added to the map", function () { + var style = { + weight: 3 + }; + var polyline = L.polyline([]); + + polyline.addTo(map); + polyline.setStyle(style); + + for (var prop in style) { + expect(polyline.options[prop]).to.be(style[prop]); + } + }); + }); }); diff --git a/src/layer/vector/Polyline.js b/src/layer/vector/Polyline.js index 6f2d90c5bc6..003eaf82643 100644 --- a/src/layer/vector/Polyline.js +++ b/src/layer/vector/Polyline.js @@ -212,6 +212,11 @@ export var Polyline = Path.extend({ _updateBounds: function () { var w = this._clickTolerance(), p = new Point(w, w); + + if (!this._rawPxBounds) { + return; + } + this._pxBounds = new Bounds([ this._rawPxBounds.min.subtract(p), this._rawPxBounds.max.add(p)