Skip to content
This repository has been archived by the owner on Sep 6, 2022. It is now read-only.

Commit

Permalink
add currentSrc support detection (fixes #478) (for 2.3 branch)
Browse files Browse the repository at this point in the history
  • Loading branch information
aFarkas committed Apr 7, 2015
1 parent 5a4b512 commit 42fe060
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 18 deletions.
23 changes: 11 additions & 12 deletions dist/picturefill.js
@@ -1,4 +1,4 @@
/*! Picturefill - v2.3.0 - 2015-03-23
/*! Picturefill - v2.3.0 - 2015-04-07
* http://scottjehl.github.io/picturefill
* Copyright (c) 2015 https://github.com/scottjehl/picturefill/blob/master/Authors.txt; Licensed MIT */
/*! matchMedia() polyfill - Test a CSS media type/query in JS. Authors & copyright (c) 2012: Scott Jehl, Paul Irish, Nicholas Zakas, David Knight. Dual MIT/BSD license */
Expand Down Expand Up @@ -92,6 +92,7 @@ window.matchMedia || (window.matchMedia = function() {
(function() {
pf.srcsetSupported = "srcset" in image;
pf.sizesSupported = "sizes" in image;
pf.curSrcSupported = "currentSrc" in image;
})();

// just a string trim workaround
Expand Down Expand Up @@ -504,7 +505,9 @@ window.matchMedia || (window.matchMedia = function() {
picImg.src = bestCandidate.url;
// currentSrc attribute and property to match
// http://picture.responsiveimages.org/#the-img-element
picImg.currentSrc = picImg.src;
if ( !pf.curSrcSupported ) {
picImg.currentSrc = picImg.src;
}

pf.backfaceVisibilityFix( picImg );
}
Expand Down Expand Up @@ -707,17 +710,13 @@ window.matchMedia || (window.matchMedia = function() {
}
}, 250 );

var resizeTimer;
var handleResize = function() {
picturefill({ reevaluate: true });
};
function checkResize() {
var resizeThrottle;

if ( !w._picturefillWorking ) {
w._picturefillWorking = true;
w.clearTimeout( resizeThrottle );
resizeThrottle = w.setTimeout( function() {
picturefill({ reevaluate: true });
w._picturefillWorking = false;
}, 60 );
}
clearTimeout(resizeTimer);
resizeTimer = setTimeout( handleResize, 60 );
}

if ( w.addEventListener ) {
Expand Down
4 changes: 2 additions & 2 deletions dist/picturefill.min.js

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

5 changes: 4 additions & 1 deletion src/picturefill.js
Expand Up @@ -43,6 +43,7 @@
(function() {
pf.srcsetSupported = "srcset" in image;
pf.sizesSupported = "sizes" in image;
pf.curSrcSupported = "currentSrc" in image;
})();

// just a string trim workaround
Expand Down Expand Up @@ -455,7 +456,9 @@
picImg.src = bestCandidate.url;
// currentSrc attribute and property to match
// http://picture.responsiveimages.org/#the-img-element
picImg.currentSrc = picImg.src;
if ( !pf.curSrcSupported ) {
picImg.currentSrc = picImg.src;
}

pf.backfaceVisibilityFix( picImg );
}
Expand Down
10 changes: 7 additions & 3 deletions tests/tests.js
Expand Up @@ -461,15 +461,19 @@
pf.applyBestCandidate( candidates, image );

deepEqual(image.src, candidates[2].url, "uses the url from the best px fit" );
deepEqual(image.currentSrc, candidates[2].url, "uses the url from the best px fit" );
if ( !pf.curSrcSupported ) {
deepEqual(image.currentSrc, candidates[2].url, "uses the url from the best px fit" );
}

image.src = "data:300";
image.currentSrc = "data:300";

pf.applyBestCandidate( candidates, image );

deepEqual(image.src, "data:300", "src left alone when matched" );
deepEqual(image.currentSrc, "data:300", "currentSrc left alone when matched" );
if ( !pf.curSrcSupported ) {
deepEqual(image.currentSrc, "data:300", "currentSrc left alone when matched" );
}
});

test( "removeVideoShim", function() {
Expand Down Expand Up @@ -612,7 +616,7 @@

picturefill( { elements: [ img ] } );

assert.equal( img.currentSrc || img.src, "data:img" );
assert.equal( img.src || img.currentSrc, "data:img" );
});

})( window, jQuery );

0 comments on commit 42fe060

Please sign in to comment.