dimsemenov / Magnific-Popup

Light and responsive lightbox script with focus on performance.
http://dimsemenov.com/plugins/magnific-popup/
MIT License
11.39k stars 3.48k forks source link

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

Open jaydansand opened 6 years ago

jaydansand commented 6 years ago

As mentioned in #871, when using the Image type and the <img> has no alt attribute, Magnific Popup's image.js on line 193 will erroneously replace the image with a new img where alt=String("undefined").

Problem line in Magnific Popup code (image.js:193): img.alt = item.el.find('img').attr('alt');

Problem description: As of jQuery version 1.6 (and Magnific Popup requires > 1.6) jQuery.attr() returns undefined if the attribute does not exist. When setting img.alt = undefined, undefined is converted to a String and the actual text "undefined" gets assigned.

Proof of Underlying Problem (in jQuery):

var img = document.createElement('img'), und = jQuery(img).attr('alt');
img.alt = und;
console.log(und, typeof und); // undefined "undefined"
console.log(img.alt, typeof img.alt); // "undefined" "string"

Solution/Fix: Test item.el.find('img').attr('alt') before assigning img.alt, as done in this PR.