From 96a99adfcee12c0111adfc09fc46f3471185338a Mon Sep 17 00:00:00 2001 From: Rob Galanakis Date: Sun, 28 Aug 2016 21:22:28 -0700 Subject: [PATCH 1/2] Guard against missing currentGallery element When an image is loaded, it can preload the next/previous image with a callback. It's possible that between when the decision to preload is made and the `loadImage` call for preloading happens, the current gallery has been modified and the index to preload is missing, causing the preload callback to error. --- src/baguetteBox.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/baguetteBox.js b/src/baguetteBox.js index 4dcb8b6b..e4f4260c 100644 --- a/src/baguetteBox.js +++ b/src/baguetteBox.js @@ -484,8 +484,15 @@ return; } + var galleryItem = currentGallery[index]; + // It's possible that the decision to preload is made, + // but by the time the load happens, the element is gone. + if (typeof galleryItem === 'undefined') { + return; + } + // Get element reference, optional caption and source path - var imageElement = currentGallery[index].imageElement; + var imageElement = galleryItem.imageElement; var thumbnailElement = imageElement.getElementsByTagName('img')[0]; var imageCaption = typeof options.captions === 'function' ? options.captions.call(currentGallery, imageElement) : From 7674cbdff9b953d085c093d9aa12fe49889630a0 Mon Sep 17 00:00:00 2001 From: Rob Galanakis Date: Sun, 23 Oct 2016 21:05:36 -0700 Subject: [PATCH 2/2] Refactor loadImage early outs, add comment. --- src/baguetteBox.js | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/baguetteBox.js b/src/baguetteBox.js index e4f4260c..bded5926 100644 --- a/src/baguetteBox.js +++ b/src/baguetteBox.js @@ -472,7 +472,11 @@ function loadImage(index, callback) { var imageContainer = imagesElements[index]; - if (typeof imageContainer === 'undefined') { + var galleryItem = currentGallery[index]; + + // Return if the index exceeds prepared images in the overlay + // or if the current gallery has been changed / closed + if (imageContainer === undefined || galleryItem === undefined) { return; } @@ -484,13 +488,6 @@ return; } - var galleryItem = currentGallery[index]; - // It's possible that the decision to preload is made, - // but by the time the load happens, the element is gone. - if (typeof galleryItem === 'undefined') { - return; - } - // Get element reference, optional caption and source path var imageElement = galleryItem.imageElement; var thumbnailElement = imageElement.getElementsByTagName('img')[0];