Skip to content

Commit

Permalink
Merge branch 'pr/9'
Browse files Browse the repository at this point in the history
  • Loading branch information
verlok committed Apr 27, 2014
2 parents 089963f + 2a07500 commit f3a0c42
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/picturePolyfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,16 @@ var picturePolyfill = (function(w) {
* @param attributes
*/
_setImg: function(pictureElement, attributes) {
var pictureAttributesToCopy, attributeName, attributeValue,
var pictureAttributesToCopy, attributeName, attributeValue, imgEl, imgSrc,
imageElements = pictureElement.getElementsByTagName('img');

// If image already exists, use it
if (imageElements.length) {
imageElements[0].setAttribute('src', attributes.src);
imgSrc = attributes.src;
imgEl = imageElements[0];
if (imgEl.getAttribute('src') !== imgSrc) {
imgEl.setAttribute('src', imgSrc);
}
}
// Else create the image
else {
Expand Down
17 changes: 17 additions & 0 deletions test/picturePolyfill.qunit.js
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,23 @@ test("_setImg() should copy picture's width and height attributes to image", fun
strictEqual(images[0].getAttribute('height'), '69');
});

test("_setImg() should not load the same image twice", function() {
var testContainer, image;

$('body').append('<div id="testContainer"></div>');
testContainer = document.getElementById('testContainer');

picturePolyfill._setImg(testContainer, {src: 'http://placehold.it/1x1', alt: 'An image'});
testContainer = document.getElementById('testContainer');

image = testContainer.getElementsByTagName('img')[0];
this.spy(image, 'setAttribute');

picturePolyfill._setImg(testContainer, {src: 'http://placehold.it/1x1', alt: 'An image'});

ok(image.setAttribute.notCalled);
})

test("parse() is called at DOM ready", function() {
if (!document.createEvent) {
ok(true);
Expand Down

0 comments on commit f3a0c42

Please sign in to comment.