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

Commit

Permalink
fix 100vw fallback, if all provided sizes are invalid/unknown to the …
Browse files Browse the repository at this point in the history
…browser
  • Loading branch information
aFarkas committed Mar 19, 2015
1 parent 742c66c commit f379528
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/picturefill.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,10 @@
*/
pf.getWidthFromLength = function( length ) {
var cssValue;
// If a length is specified and doesn’t contain a percentage, and it is greater than 0 or using `calc`, use it. Else, use the `100vw` default.
length = length && length.indexOf( "%" ) > -1 === false && ( parseFloat( length ) > 0 || length.indexOf( "calc(" ) > -1 ) ? length : "100vw";
// If a length is specified and doesn’t contain a percentage, and it is greater than 0 or using `calc`, use it. Else, abort.
if ( !(length && length.indexOf( "%" ) > -1 === false && ( parseFloat( length ) > 0 || length.indexOf( "calc(" ) > -1 )) ) {
return false;
}

/**
* If length is specified in `vw` units, use `%` instead since the div we’re measuring
Expand Down Expand Up @@ -210,7 +212,7 @@
}

//if we have no winningLength fallback to 100vw
return winningLength || Math.max(w.innerWidth || 0, doc.document.clientWidth);
return winningLength || Math.max(w.innerWidth || 0, doc.documentElement.clientWidth);
};

pf.parseSrcset = function( srcset ) {
Expand Down
5 changes: 5 additions & 0 deletions tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@
sizes = "100foo, 200px";
width = pf.findWidthFromSourceSize(sizes);
equal(width, 200, "returns 200 when there was an unknown css length");

sizes = "100foo, sd2300bar";
width = pf.findWidthFromSourceSize(sizes);

equal(width, Math.max(window.innerWidth || 0, document.documentElement.clientWidth), "returns 100vw when all sizes are an unknown css length");
});

asyncTest("setIntrinsicSize", function() {
Expand Down

0 comments on commit f379528

Please sign in to comment.