From 2a07500d08b9a30bf02d535826417dc89e38084c Mon Sep 17 00:00:00 2001 From: Andrea Verlicchi Date: Sun, 27 Apr 2014 17:38:41 +0200 Subject: [PATCH] Fixed image reloading --- src/picturePolyfill.js | 8 ++++++-- test/picturePolyfill.qunit.js | 17 +++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/picturePolyfill.js b/src/picturePolyfill.js index e6e417f..e333b85 100644 --- a/src/picturePolyfill.js +++ b/src/picturePolyfill.js @@ -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 { diff --git a/test/picturePolyfill.qunit.js b/test/picturePolyfill.qunit.js index 6266cec..d1e537b 100644 --- a/test/picturePolyfill.qunit.js +++ b/test/picturePolyfill.qunit.js @@ -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('
'); + 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);