Skip to content

Commit

Permalink
Fix attribution text not removed when Layer is removed from map
Browse files Browse the repository at this point in the history
Symptom: Switching between layers with different attributions does
not remove the attribution from the previously selected layer.

Control.Attribution keeps track of its attributions with a counter for
each text. The problem described in Leaflet#4285 is that each time the layer
is added to the map, the counter is increased by two. It's because
Layer calls Control.Attribution.addAttribution() twice, once from
addLayer() and the second time via the whenReady() callback _layerAdd().
This was not caught by the tests since the callback was never fired
(missing map.setView() call).

Fixes Leaflet#4285
  • Loading branch information
dr-itz committed Mar 1, 2016
1 parent f7d7ad2 commit 0af11b0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
20 changes: 14 additions & 6 deletions spec/suites/control/Control.AttributionSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,17 @@ describe("Control.Attribution", function () {
control = new L.Control.Attribution({
prefix: 'prefix'
}).addTo(map);
map.setView([0, 0], 1);
container = control.getContainer();
});

function dummyLayer() {
var layer = new L.Layer();
layer.onAdd = function () { };
layer.onRemove = function () { };
return layer;
}

it("contains just prefix if no attributions added", function () {
expect(container.innerHTML).to.eql('prefix');
});
Expand Down Expand Up @@ -67,9 +75,9 @@ describe("Control.Attribution", function () {

describe('on layer add/remove', function () {
it('changes text', function () {
var fooLayer = new L.Layer();
var barLayer = new L.Layer();
var bazLayer = new L.Layer();
var fooLayer = dummyLayer();
var barLayer = dummyLayer();
var bazLayer = dummyLayer();
fooLayer.getAttribution = function () { return 'foo'; };
barLayer.getAttribution = function () { return 'bar'; };
bazLayer.getAttribution = function () { return 'baz'; };
Expand All @@ -91,9 +99,9 @@ describe("Control.Attribution", function () {
});

it('keeps count of duplicated attributions', function () {
var fooLayer = new L.Layer();
var fo2Layer = new L.Layer();
var fo3Layer = new L.Layer();
var fooLayer = dummyLayer();
var fo2Layer = dummyLayer();
var fo3Layer = dummyLayer();
fooLayer.getAttribution = function () { return 'foo'; };
fo2Layer.getAttribution = function () { return 'foo'; };
fo3Layer.getAttribution = function () { return 'foo'; };
Expand Down
4 changes: 0 additions & 4 deletions src/layer/Layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,6 @@ L.Map.include({
layer.beforeAdd(this);
}

if (layer.getAttribution && this.attributionControl) {
this.attributionControl.addAttribution(layer.getAttribution());
}

this.whenReady(layer._layerAdd, layer);

return this;
Expand Down

0 comments on commit 0af11b0

Please sign in to comment.