diff --git a/lib/OpenLayers/Tile/Image.js b/lib/OpenLayers/Tile/Image.js index 9fef2dcfde..3772fe387a 100644 --- a/lib/OpenLayers/Tile/Image.js +++ b/lib/OpenLayers/Tile/Image.js @@ -109,10 +109,14 @@ OpenLayers.Tile.Image = OpenLayers.Class(OpenLayers.Tile, { destroy: function() { if (this.imgDiv != null) { if (this.layerAlphaHack) { + // unregister the "load" handler OpenLayers.Event.stopObservingElement(this.imgDiv.childNodes[0].id); - } else { - OpenLayers.Event.stopObservingElement(this.imgDiv.id); } + + // unregister the "load" and "error" handlers. Only the "error" handler if + // this.layerAlphaHack is true. + OpenLayers.Event.stopObservingElement(this.imgDiv.id); + if (this.imgDiv.parentNode == this.frame) { this.frame.removeChild(this.imgDiv); this.imgDiv.map = null; diff --git a/tests/Tile/Image.html b/tests/Tile/Image.html index c12abc0f75..f5f327b928 100644 --- a/tests/Tile/Image.html +++ b/tests/Tile/Image.html @@ -24,7 +24,44 @@ t.eq( tile.url, url, "tile.url is set correctly"); t.ok( tile.size.equals(size), "tile.size is set correctly"); } - + + function test_destroy_observers(t) { + t.plan(2); + + var map = new OpenLayers.Map('map'); + var layer = new OpenLayers.Layer.WMS( "OpenLayers WMS", + "http://labs.metacarta.com/wms/vmap0?", {layers: 'basic'}); + map.addLayer(layer); + + var position = new OpenLayers.Pixel(20,30); + var bounds = new OpenLayers.Bounds(1,2,3,4); + var size = new OpenLayers.Size(5,6); + + // with alpha hack + var withAlpha = new OpenLayers.Tile.Image(layer, position, bounds, null, size); + withAlpha.layerAlphaHack = true; + + withAlpha.draw(); + var cacheID = withAlpha.imgDiv._eventCacheID; + withAlpha.destroy(); + + t.eq(OpenLayers.Event.observers[cacheID], undefined, + "With alpha hack: imgDiv observers are cleared in destroy"); + + // without alpha hack + var withoutAlpha = new OpenLayers.Tile.Image(layer, position, bounds, null, size); + withoutAlpha.layerAlphaHack = false; + + withoutAlpha.draw(); + var cacheID = withoutAlpha.imgDiv._eventCacheID; + withoutAlpha.destroy(); + + t.eq(OpenLayers.Event.observers[cacheID], undefined, + "Without alpha hack: imgDiv observers are cleared in destroy"); + + map.destroy(); + } + function test_Tile_Image_clone (t) { t.plan( 9 );