Skip to content

Commit

Permalink
Releasing version 3.3.3: fixed a bug when using picturePolyfill toget…
Browse files Browse the repository at this point in the history
…her with the matchMedia polyfill
  • Loading branch information
verlok committed May 5, 2014
1 parent 074f769 commit aa1d965
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 41 deletions.
4 changes: 2 additions & 2 deletions dist/picturePolyfill.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 16 additions & 13 deletions src/picturePolyfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,27 +355,30 @@ var picturePolyfill = (function(w) {
var sourcesData,
pictureElement,
pictureElements,
srcAttribute;
srcAttribute,
mqSupport;

if (!this.isUseful) { return 0; }

pictureElements = (element || document).getElementsByTagName('picture');
mqSupport = this._mqSupport;

for (var i=0, len=pictureElements.length; i<len; i+=1) {
pictureElement = pictureElements[i];
if (!this._mqSupport) {
srcAttribute = pictureElement.getAttribute("data-default-src");
}
else {
sourcesData = _cacheArray[pictureElement.getAttribute('data-cache-index')];
if (!sourcesData) {
sourcesData = this._getSourcesData(pictureElement);
_cacheArray[_cacheIndex] = sourcesData;
pictureElement.setAttribute('data-cache-index', _cacheIndex);
_cacheIndex+=1;
}
srcAttribute = this._getSrcFromData(sourcesData);
// Try to read sources data from cache
sourcesData = _cacheArray[pictureElement.getAttribute('data-cache-index')];
// If empty, try to read sources data from the picture element, then cache them
if (!sourcesData) {
sourcesData = this._getSourcesData(pictureElement);
_cacheArray[_cacheIndex] = sourcesData;
pictureElement.setAttribute('data-cache-index', _cacheIndex);
_cacheIndex+=1;
}
// If no sourcesData retrieved or media queries are not supported, read from the default src
srcAttribute = (sourcesData.length === 0 || !mqSupport) ?
pictureElement.getAttribute('data-default-src') :
this._getSrcFromData(sourcesData);
// If there mustn't be any image, remove it, else set it (create/ update)
if (!srcAttribute) {
this._resetImg(pictureElement);
}
Expand Down
35 changes: 9 additions & 26 deletions test/picturePolyfill.qunit.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ if(!Array.prototype.indexOf) {

test("main object is declared and exposed", function() {
strictEqual(typeof window.picturePolyfill, 'object', "picturePolyfill should be an object");
strictEqual(typeof window.picturePolyfill.parse, 'function', "picturePolyfill.parse() should be a function")
strictEqual(typeof window.picturePolyfill.initialize, 'function', "picturePolyfill.initialize() should be a function")
strictEqual(typeof window.picturePolyfill.parse, 'function', "picturePolyfill.parse() should be a function");
strictEqual(typeof window.picturePolyfill.initialize, 'function', "picturePolyfill.initialize() should be a function");
});

test("_getSrcFromArray correct behaviour, correct data, correct calls", function() {
Expand Down Expand Up @@ -397,27 +397,10 @@ test("_setImg() should copy picture's width and height attributes to image", fun
images = testContainer.getElementsByTagName('img');
strictEqual(images.length, 1);

strictEqual(images[0].getAttribute('width'), '666');
strictEqual(images[0].getAttribute('height'), '69');
equal(images[0].getAttribute('width'), 666);
equal(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 Expand Up @@ -572,7 +555,7 @@ test("parse() resulting image sources - without MQ support", function(){
picturePolyfill._pxRatio = initial_pixelRatio;

});

/*
test("_getSourcesData, _getSrcFromData, _getSrcFromArray mustn't be called from non MQ browsers", function() {
$('body').append('<div id="testContainer">\
Expand All @@ -593,9 +576,9 @@ test("_getSourcesData, _getSrcFromData, _getSrcFromArray mustn't be called from
picturePolyfill._mqSupport = false;
picturePolyfill.parse();
ok(picturePolyfill._getSourcesData.notCalled);
ok(picturePolyfill._getSrcFromData.notCalled);
ok(picturePolyfill._getSrcFromArray.notCalled);
ok(picturePolyfill._getSourcesData.notCalled, "_getSourcesData was called on non mq browser, it shouldn't");
ok(picturePolyfill._getSrcFromData.notCalled, "_getSrcFromData was called on non mq browser, it shouldn't");
ok(picturePolyfill._getSrcFromArray.notCalled, "_getSrcFromArray was called on non mq browser, it shouldn't");
if (initial_mqSupport) {
picturePolyfill._mqSupport = true;
Expand All @@ -608,7 +591,7 @@ test("_getSourcesData, _getSrcFromData, _getSrcFromArray mustn't be called from
picturePolyfill._mqSupport = initial_mqSupport;
});

*/
test("parse() after a DOM injection (without MQ support)", function(){

var images, img1src, img2src, $ajaxResponse;
Expand Down

0 comments on commit aa1d965

Please sign in to comment.