From 67c4e57453120935180c45c6820e7d3fbd2ea1f9 Mon Sep 17 00:00:00 2001 From: David DeSandro Date: Tue, 2 Jan 2018 11:51:45 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=9B=A0=20check=20for=20non-zero=20natural?= =?UTF-8?q?Width?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes bug with Safari + InfiniteScroll + Masonry, metafizzy/infinite-scroll#671 👷 build v4.1.4 --- bower.json | 2 +- imagesloaded.js | 6 +++-- imagesloaded.pkgd.js | 58 +++++++++++++++++++++------------------- imagesloaded.pkgd.min.js | 4 +-- package.json | 2 +- 5 files changed, 39 insertions(+), 33 deletions(-) diff --git a/bower.json b/bower.json index ce7515c..795c69f 100644 --- a/bower.json +++ b/bower.json @@ -7,7 +7,7 @@ }, "devDependencies": { "jquery": ">=1.9 <4.0", - "qunit": "~1.20.0" + "qunit": "^2.0.0" }, "ignore": [ "**/.*", diff --git a/imagesloaded.js b/imagesloaded.js index 24e9a3a..f527a71 100644 --- a/imagesloaded.js +++ b/imagesloaded.js @@ -1,5 +1,5 @@ /*! - * imagesLoaded v4.1.3 + * imagesLoaded v4.1.4 * JavaScript is all like "You images are done yet or what?" * MIT License */ @@ -282,7 +282,9 @@ LoadingImage.prototype.check = function() { }; LoadingImage.prototype.getIsImageComplete = function() { - return this.img.complete && this.img.naturalWidth !== undefined; + // check for non-zero, non-undefined naturalWidth + // fixes Safari+InfiniteScroll+Masonry bug infinite-scroll#671 + return this.img.complete && this.img.naturalWidth; }; LoadingImage.prototype.confirm = function( isLoaded, message ) { diff --git a/imagesloaded.pkgd.js b/imagesloaded.pkgd.js index 802330a..a230750 100644 --- a/imagesloaded.pkgd.js +++ b/imagesloaded.pkgd.js @@ -1,5 +1,5 @@ /*! - * imagesLoaded PACKAGED v4.1.3 + * imagesLoaded PACKAGED v4.1.4 * JavaScript is all like "You images are done yet or what?" * MIT License */ @@ -85,13 +85,14 @@ proto.emitEvent = function( eventName, args ) { if ( !listeners || !listeners.length ) { return; } - var i = 0; - var listener = listeners[i]; + // copy over to avoid interference if .off() in listener + listeners = listeners.slice(0); args = args || []; // once stuff var onceListeners = this._onceEvents && this._onceEvents[ eventName ]; - while ( listener ) { + for ( var i=0; i < listeners.length; i++ ) { + var listener = listeners[i] var isOnce = onceListeners && onceListeners[ listener ]; if ( isOnce ) { // remove listener @@ -102,16 +103,12 @@ proto.emitEvent = function( eventName, args ) { } // trigger listener listener.apply( this, args ); - // get next listener - i += isOnce ? 0 : 1; - listener = listeners[i]; } return this; }; -proto.allOff = -proto.removeAllListeners = function() { +proto.allOff = function() { delete this._events; delete this._onceEvents; }; @@ -121,7 +118,7 @@ return EvEmitter; })); /*! - * imagesLoaded v4.1.3 + * imagesLoaded v4.1.4 * JavaScript is all like "You images are done yet or what?" * MIT License */ @@ -173,22 +170,23 @@ function extend( a, b ) { return a; } +var arraySlice = Array.prototype.slice; + // turn element or nodeList into an array function makeArray( obj ) { - var ary = []; if ( Array.isArray( obj ) ) { // use object if already an array - ary = obj; - } else if ( typeof obj.length == 'number' ) { + return obj; + } + + var isArrayLike = typeof obj == 'object' && typeof obj.length == 'number'; + if ( isArrayLike ) { // convert nodeList to array - for ( var i=0; i < obj.length; i++ ) { - ary.push( obj[i] ); - } - } else { - // array of single index - ary.push( obj ); + return arraySlice.call( obj ); } - return ary; + + // array of single index + return [ obj ]; } // -------------------------- imagesLoaded -------------------------- // @@ -204,13 +202,19 @@ function ImagesLoaded( elem, options, onAlways ) { return new ImagesLoaded( elem, options, onAlways ); } // use elem as selector string + var queryElem = elem; if ( typeof elem == 'string' ) { - elem = document.querySelectorAll( elem ); + queryElem = document.querySelectorAll( elem ); + } + // bail if bad element + if ( !queryElem ) { + console.error( 'Bad element for imagesLoaded ' + ( queryElem || elem ) ); + return; } - this.elements = makeArray( elem ); + this.elements = makeArray( queryElem ); this.options = extend( {}, this.options ); - + // shift arguments if no options set if ( typeof options == 'function' ) { onAlways = options; } else { @@ -229,9 +233,7 @@ function ImagesLoaded( elem, options, onAlways ) { } // HACK check async to allow time to bind listeners - setTimeout( function() { - this.check(); - }.bind( this )); + setTimeout( this.check.bind( this ) ); } ImagesLoaded.prototype = Object.create( EvEmitter.prototype ); @@ -399,7 +401,9 @@ LoadingImage.prototype.check = function() { }; LoadingImage.prototype.getIsImageComplete = function() { - return this.img.complete && this.img.naturalWidth !== undefined; + // check for non-zero, non-undefined naturalWidth + // fixes Safari+InfiniteScroll+Masonry bug infinite-scroll#671 + return this.img.complete && this.img.naturalWidth; }; LoadingImage.prototype.confirm = function( isLoaded, message ) { diff --git a/imagesloaded.pkgd.min.js b/imagesloaded.pkgd.min.js index 0854d63..e443a77 100644 --- a/imagesloaded.pkgd.min.js +++ b/imagesloaded.pkgd.min.js @@ -1,7 +1,7 @@ /*! - * imagesLoaded PACKAGED v4.1.3 + * imagesLoaded PACKAGED v4.1.4 * JavaScript is all like "You images are done yet or what?" * MIT License */ -!function(e,t){"function"==typeof define&&define.amd?define("ev-emitter/ev-emitter",t):"object"==typeof module&&module.exports?module.exports=t():e.EvEmitter=t()}("undefined"!=typeof window?window:this,function(){function e(){}var t=e.prototype;return t.on=function(e,t){if(e&&t){var i=this._events=this._events||{},n=i[e]=i[e]||[];return-1==n.indexOf(t)&&n.push(t),this}},t.once=function(e,t){if(e&&t){this.on(e,t);var i=this._onceEvents=this._onceEvents||{},n=i[e]=i[e]||{};return n[t]=!0,this}},t.off=function(e,t){var i=this._events&&this._events[e];if(i&&i.length){var n=i.indexOf(t);return-1!=n&&i.splice(n,1),this}},t.emitEvent=function(e,t){var i=this._events&&this._events[e];if(i&&i.length){var n=0,o=i[n];t=t||[];for(var r=this._onceEvents&&this._onceEvents[e];o;){var s=r&&r[o];s&&(this.off(e,o),delete r[o]),o.apply(this,t),n+=s?0:1,o=i[n]}return this}},t.allOff=t.removeAllListeners=function(){delete this._events,delete this._onceEvents},e}),function(e,t){"use strict";"function"==typeof define&&define.amd?define(["ev-emitter/ev-emitter"],function(i){return t(e,i)}):"object"==typeof module&&module.exports?module.exports=t(e,require("ev-emitter")):e.imagesLoaded=t(e,e.EvEmitter)}("undefined"!=typeof window?window:this,function(e,t){function i(e,t){for(var i in t)e[i]=t[i];return e}function n(e){var t=[];if(Array.isArray(e))t=e;else if("number"==typeof e.length)for(var i=0;i