diff --git a/resources/multilightbox/lightboximage.js b/resources/multilightbox/lightboximage.js index 9ea200f26..27754af2a 100644 --- a/resources/multilightbox/lightboximage.js +++ b/resources/multilightbox/lightboximage.js @@ -27,18 +27,21 @@ */ LIP.initialSrc = null; - LIP.getImageElement = function ( loadcb ) { - var ele; + LIP.getImageElement = function () { + var ele, + $deferred = $.Deferred(), + image = this; lightboxHooks.callAll( 'beforeFetchImage', this ); ele = new Image(); - ele.addEventListener( 'load', loadcb ); + ele.addEventListener( 'error', $deferred.reject ); + ele.addEventListener( 'load', function() { $deferred.resolve( image, ele ); } ); ele.src = this.src || this.initialSrc; lightboxHooks.callAll( 'modifyImageElement', ele ); - return ele; + return $deferred; }; // Assumes that the parent element's size is the maximum size. diff --git a/resources/multilightbox/lightboxinterface.js b/resources/multilightbox/lightboxinterface.js index 524e73452..cf48a8aad 100644 --- a/resources/multilightbox/lightboxinterface.js +++ b/resources/multilightbox/lightboxinterface.js @@ -165,12 +165,13 @@ * @param {LightboxImage} image */ LIP.load = function ( image ) { - var iface = this, - ele = image.getImageElement( function () { - iface.loadCallback( image, ele ); - } ); + var iface = this; this.currentImage = image; + + image.getImageElement().done( function( image, ele ) { + iface.loadCallback.call( iface, image, ele ); + } ); }; LIP.autoResizeImage = function () { diff --git a/tests/qunit/lightboximage.test.js b/tests/qunit/lightboximage.test.js index 0a21dd73f..bb42b158f 100644 --- a/tests/qunit/lightboximage.test.js +++ b/tests/qunit/lightboximage.test.js @@ -10,7 +10,20 @@ QUnit.start(); } - lightboxImage.getImageElement( loadCallback ); + lightboxImage.getImageElement() + .done( loadCallback ); + } ); + + QUnit.asyncTest( 'Image failing', 1, function ( assert ) { + var lightboxImage = new window.LightboxImage( 'http://thisdoesntexist/fail.jpg' ); + + function errorCallback() { + assert.ok( true, 'Image failed !' ); + QUnit.start(); + } + + lightboxImage.getImageElement() + .fail( errorCallback ); } ); }( mediaWiki, jQuery ) );