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

Commit

Permalink
test width with a connected image, if width with an unconnected image…
Browse files Browse the repository at this point in the history
… returns 0
  • Loading branch information
aFarkas committed Mar 19, 2015
1 parent a5859fb commit 742c66c
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/picturefill.js
Original file line number Diff line number Diff line change
Expand Up @@ -376,24 +376,36 @@
pf.setIntrinsicSize = (function() {
var urlCache = {};
var setSize = function( picImg, width, res ) {
picImg.setAttribute( "width", parseInt(width / res, 10) );
if ( width ) {
picImg.setAttribute( "width", parseInt(width / res, 10) );
}
};
return function( picImg, bestCandidate ) {
var img;
if ( !picImg[ pf.ns ] ) {
if ( !picImg[ pf.ns ] || w.pfStopIntrinsicSize ) {
return;
}
if ( picImg[ pf.ns ].dims === undefined ) {
picImg[ pf.ns].dims = picImg.getAttribute("width") || picImg.getAttribute("height");
}
if ( picImg[ pf.ns].dims ) { return; }

if ( urlCache[bestCandidate.url] ) {
if ( bestCandidate.url in urlCache ) {
setSize( picImg, urlCache[bestCandidate.url], bestCandidate.resolution );
} else {
img = doc.createElement( "img" );
img.onload = function() {
urlCache[bestCandidate.url] = img.width;

//IE 10/11 don't calculate width for svg outside document
if ( !urlCache[bestCandidate.url] ) {
try {
doc.body.appendChild( img );
urlCache[bestCandidate.url] = img.width || img.offsetWidth;
doc.body.removeChild( img );
} catch(e){}
}

if ( picImg.src === bestCandidate.url ) {
setSize( picImg, urlCache[bestCandidate.url], bestCandidate.resolution );
}
Expand Down

0 comments on commit 742c66c

Please sign in to comment.