Skip to content

Commit

Permalink
🛠 check for non-zero naturalWidth
Browse files Browse the repository at this point in the history
Fixes bug with Safari + InfiniteScroll + Masonry, metafizzy/infinite-scroll#671

👷 build v4.1.4
  • Loading branch information
desandro committed Jan 2, 2018
1 parent 2eafc07 commit 67c4e57
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 33 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
},
"devDependencies": {
"jquery": ">=1.9 <4.0",
"qunit": "~1.20.0"
"qunit": "^2.0.0"
},
"ignore": [
"**/.*",
Expand Down
6 changes: 4 additions & 2 deletions imagesloaded.js
Original file line number Diff line number Diff line change
@@ -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
*/
Expand Down Expand Up @@ -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 ) {
Expand Down
58 changes: 31 additions & 27 deletions imagesloaded.pkgd.js
Original file line number Diff line number Diff line change
@@ -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
*/
Expand Down Expand Up @@ -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
Expand All @@ -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;
};
Expand All @@ -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
*/
Expand Down Expand Up @@ -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 -------------------------- //
Expand All @@ -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 {
Expand All @@ -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 );
Expand Down Expand Up @@ -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 ) {
Expand Down
4 changes: 2 additions & 2 deletions imagesloaded.pkgd.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "imagesloaded",
"version": "4.1.3",
"version": "4.1.4",
"description": "JavaScript is all like _You images done yet or what?_",
"main": "imagesloaded.js",
"dependencies": {
Expand Down

0 comments on commit 67c4e57

Please sign in to comment.