Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make carousel more robust against missing data #20101

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: bugfix

Carousel: fix crashes when opening images with missing data.
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
[data-carousel-extra]:not(.jp-carousel-wrap) {
cursor: pointer;
}

.jp-carousel-wrap * {
line-height:inherit; /* prevent declarations of line-height in the universal selector */
}
Expand Down
20 changes: 13 additions & 7 deletions projects/plugins/jetpack/modules/carousel/jetpack-carousel.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,9 @@
function fadeOut( el, callback ) {
callback = callback || util.noop;
fade( el, '1', '0', function () {
el.style.display = 'none';
if ( el ) {
el.style.display = 'none';
}
callback();
} );
}
Expand Down Expand Up @@ -1488,19 +1490,22 @@
var galleryItem = domUtil.closest( item, '.gallery-item' );
var captionEl = galleryItem && galleryItem.querySelector( '.gallery-caption' );
var permalinkEl = domUtil.closest( item, 'a' );
var origFile = item.getAttribute( 'data-orig-file' ) || item.getAttribute( 'src-orig' );

var attrs = {
attachmentId: item.getAttribute( 'data-attachment-id' ) || '0',
attachmentId:
item.getAttribute( 'data-attachment-id' ) || item.getAttribute( 'data-id' ) || '0',
commentsOpened: item.getAttribute( 'data-comments-opened' ) || '0',
imageMeta: domUtil.getJSONAttribute( item, 'data-image-meta' ) || {},
title: item.getAttribute( 'data-image-title' ) || '',
desc: item.getAttribute( 'data-image-description' ) || '',
mediumFile: item.getAttribute( 'data-medium-file' ) || '',
largeFile: item.getAttribute( 'data-large-file' ) || '',
origFile: item.getAttribute( 'data-orig-file' ) || '',
origFile: origFile || '',
thumbSize: { width: item.naturalWidth, height: item.naturalHeight },
caption: ( captionEl && captionEl.innerHTML ) || '',
permalink: permalinkEl && permalinkEl.getAttribute( 'href' ),
src: origFile || item.getAttribute( 'src' ) || '',
};

var tiledGalleryItem = domUtil.closest( item, '.tiled-gallery-item' );
Expand All @@ -1519,8 +1524,6 @@
if ( typeof wpcom !== 'undefined' && wpcom.carousel && wpcom.carousel.generateImgSrc ) {
attrs.src = wpcom.carousel.generateImgSrc( item, max );
} else {
attrs.src = item.getAttribute( 'data-orig-file' );

attrs.src = selectBestImageUrl( {
origFile: attrs.src,
origWidth: attrs.origWidth,
Expand Down Expand Up @@ -1581,7 +1584,7 @@
function openCarousel( gallery, options ) {
var settings = {
imgSelector:
'.gallery-item [data-attachment-id], .tiled-gallery-item [data-attachment-id], img[data-attachment-id]',
'.gallery-item [data-attachment-id], .tiled-gallery-item [data-attachment-id], img[data-attachment-id], img[data-id]',
startIndex: 0,
};

Expand Down Expand Up @@ -1731,7 +1734,10 @@

var images = gallery.querySelectorAll( 'img' );
for ( var j = 0; j < images.length; j++ ) {
if ( parseInt( images[ j ].getAttribute( 'data-attachment-id' ), 10 ) === attachmentId ) {
if (
parseInt( images[ j ].getAttribute( 'data-attachment-id' ), 10 ) === attachmentId ||
parseInt( images[ j ].getAttribute( 'data-id' ), 10 ) === attachmentId
) {
selected = j;
break;
}
Expand Down