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

Prevent setting alt=String("undefined") when the original IMG has no alt #1049

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
6 changes: 5 additions & 1 deletion src/js/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,11 @@ $.magnificPopup.registerModule('image', {
var img = document.createElement('img');
img.className = 'mfp-img';
if(item.el && item.el.find('img').length) {
img.alt = item.el.find('img').attr('alt');
Copy link

@dpilafian dpilafian Dec 6, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than an if statement with mostly duplicate lines, cleaner to tack on || '':

img.alt = item.el.find('img').attr('alt') || '';

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure! I guess I'm always leery of depending on truthiness in loose-typed languages. Maybe because I'm too accustomed to PHP's terrible string finagling.

// Sometimes the img does not have an alt attribute. jQuery 1.6+ will
// then return /undefined/, setting img.alt=String("undefined").
if(item.el.find('img').attr('alt')) {
img.alt = item.el.find('img').attr('alt');
}
}
item.img = $(img).on('load.mfploader', onLoadComplete).on('error.mfploader', onLoadError);
img.src = item.src;
Expand Down