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

Commit c43003b

Browse files
committed
Merge branch 'christophedejonge-contribute'
2 parents bec4643 + e6ba809 commit c43003b

File tree

4 files changed

+90
-92
lines changed

4 files changed

+90
-92
lines changed

dist/picturefill.js

Lines changed: 44 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,12 @@ window.matchMedia || (window.matchMedia = function() {
9696
/**
9797
* Shortcut method for matchMedia ( for easy overriding in tests )
9898
*/
99+
99100
pf.matchesMedia = function( media ) {
100101
return w.matchMedia && w.matchMedia( media ).matches;
101102
};
102103

103-
/**
104-
* Shortcut method for `devicePixelRatio` ( for easy overriding in tests )
105-
*/
104+
// Shortcut method for `devicePixelRatio` ( for easy overriding in tests )
106105
pf.getDpr = function() {
107106
return ( w.devicePixelRatio || 1 );
108107
};
@@ -115,11 +114,11 @@ window.matchMedia || (window.matchMedia = function() {
115114
// 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.
116115
length = length && length.indexOf( "%" ) > -1 === false && ( parseFloat( length ) > 0 || length.indexOf( "calc(" ) > -1 ) ? length : "100vw";
117116
/**
118-
* If length is specified in `vw` units, use `%` instead since the div we’re measuring
119-
* is injected at the top of the document.
120-
*
121-
* TODO: maybe we should put this behind a feature test for `vw`?
122-
*/
117+
* If length is specified in `vw` units, use `%` instead since the div we’re measuring
118+
* is injected at the top of the document.
119+
*
120+
* TODO: maybe we should put this behind a feature test for `vw`?
121+
*/
123122
length = length.replace( "vw", "%" );
124123

125124
// Create a cached element for getting length value widths
@@ -152,10 +151,10 @@ window.matchMedia || (window.matchMedia = function() {
152151
// container of supported mime types that one might need to qualify before using
153152
pf.types = {};
154153

155-
// Add support for standard mime types.
156-
pf.types["image/jpeg"] = true;
157-
pf.types["image/gif"] = true;
158-
pf.types["image/png"] = true;
154+
// Add support for standard mime types
155+
pf.types[ "image/jpeg" ] = true;
156+
pf.types[ "image/gif" ] = true;
157+
pf.types[ "image/png" ] = true;
159158

160159
// test svg support
161160
pf.types[ "image/svg+xml" ] = doc.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#Image", "1.1");
@@ -199,9 +198,7 @@ window.matchMedia || (window.matchMedia = function() {
199198
}
200199
};
201200

202-
/**
203-
* Parses an individual `size` and returns the length, and optional media query
204-
*/
201+
// Parses an individual `size` and returns the length, and optional media query
205202
pf.parseSize = function( sourceSizeStr ) {
206203
var match = /(\([^)]+\))?\s*(.+)/g.exec( sourceSizeStr );
207204
return {
@@ -210,9 +207,7 @@ window.matchMedia || (window.matchMedia = function() {
210207
};
211208
};
212209

213-
/**
214-
* Takes a string of sizes and returns the width in pixels as a number
215-
*/
210+
// Takes a string of sizes and returns the width in pixels as a number
216211
pf.findWidthFromSourceSize = function( sourceSizeListStr ) {
217212
// Split up source size list, ie ( max-width: 30em ) 100%, ( max-width: 50em ) 50%, 33%
218213
// or (min-width:30em) calc(30% - 15px)
@@ -245,19 +240,19 @@ window.matchMedia || (window.matchMedia = function() {
245240

246241
pf.parseSrcset = function( srcset ) {
247242
/**
248-
* A lot of this was pulled from Boris Smus’ parser for the now-defunct WHATWG `srcset`
249-
* https://github.com/borismus/srcset-polyfill/blob/master/js/srcset-info.js
250-
*
251-
* 1. Let input (`srcset`) be the value passed to this algorithm.
252-
* 2. Let position be a pointer into input, initially pointing at the start of the string.
253-
* 3. Let raw candidates be an initially empty ordered list of URLs with associated
254-
* unparsed descriptors. The order of entries in the list is the order in which entries
255-
* are added to the list.
256-
*/
243+
* A lot of this was pulled from Boris Smus’ parser for the now-defunct WHATWG `srcset`
244+
* https://github.com/borismus/srcset-polyfill/blob/master/js/srcset-info.js
245+
*
246+
* 1. Let input (`srcset`) be the value passed to this algorithm.
247+
* 2. Let position be a pointer into input, initially pointing at the start of the string.
248+
* 3. Let raw candidates be an initially empty ordered list of URLs with associated
249+
* unparsed descriptors. The order of entries in the list is the order in which entries
250+
* are added to the list.
251+
*/
257252
var candidates = [];
258253

259254
while ( srcset !== "" ) {
260-
srcset = srcset.replace(/^\s+/g,"");
255+
srcset = srcset.replace( /^\s+/g, "" );
261256

262257
// 5. Collect a sequence of characters that are not space characters, and let that be url.
263258
var pos = srcset.search(/\s/g),
@@ -273,15 +268,15 @@ window.matchMedia || (window.matchMedia = function() {
273268
// 6.1. If url is empty, then jump to the step labeled descriptor parser.
274269

275270
if ( last === "," || url === "" ) {
276-
url = url.replace(/,+$/, "");
271+
url = url.replace( /,+$/, "" );
277272
descriptor = "";
278273
}
279274
srcset = srcset.slice( pos + 1 );
280275

281276
// 6.2. Collect a sequence of characters that are not U+002C COMMA characters (,), and
282277
// let that be descriptors.
283278
if ( descriptor === null ) {
284-
var descpos = srcset.indexOf(",");
279+
var descpos = srcset.indexOf( "," );
285280
if ( descpos !== -1 ) {
286281
descriptor = srcset.slice( 0, descpos );
287282
srcset = srcset.slice( descpos + 1 );
@@ -310,7 +305,7 @@ window.matchMedia || (window.matchMedia = function() {
310305
// 11. Descriptor parser: Let candidates be an initially empty source set. The order of entries in the list
311306
// is the order in which entries are added to the list.
312307
var sizes = sizesattr || "100vw",
313-
sizeDescriptor = descriptor && descriptor.replace(/(^\s+|\s+$)/g, ""),
308+
sizeDescriptor = descriptor && descriptor.replace( /(^\s+|\s+$)/g, "" ),
314309
widthInCssPixels = pf.findWidthFromSourceSize( sizes ),
315310
resCandidate;
316311

@@ -357,7 +352,7 @@ window.matchMedia || (window.matchMedia = function() {
357352
return formattedCandidates;
358353
};
359354

360-
/*
355+
/**
361356
* if it's an img element and it has a srcset property,
362357
* we need to remove the attribute so we can manipulate src
363358
* (the property's existence infers native srcset support, and a srcset-supporting browser will prioritize srcset's value over our winning picture candidate)
@@ -370,9 +365,7 @@ window.matchMedia || (window.matchMedia = function() {
370365
}
371366
};
372367

373-
/*
374-
* Accept a source or img element and process its srcset and sizes attrs
375-
*/
368+
// Accept a source or img element and process its srcset and sizes attrs
376369
pf.processSourceSet = function( el ) {
377370
var srcset = el.getAttribute( "srcset" ),
378371
sizes = el.getAttribute( "sizes" ),
@@ -437,7 +430,7 @@ window.matchMedia || (window.matchMedia = function() {
437430
return a.resolution - b.resolution;
438431
};
439432

440-
/*
433+
/**
441434
* In IE9, <source> elements get removed if they aren't children of
442435
* video elements. Thus, we conditionally wrap source elements
443436
* using <!--[if IE 9]><video style="display: none;"><![endif]-->
@@ -457,7 +450,7 @@ window.matchMedia || (window.matchMedia = function() {
457450
}
458451
};
459452

460-
/*
453+
/**
461454
* Find all `img` elements, and add them to the candidate list if they have
462455
* a `picture` parent, a `sizes` attribute in basic `srcset` supporting browsers,
463456
* a `srcset` attribute at all, and they haven’t been evaluated already.
@@ -470,8 +463,8 @@ window.matchMedia || (window.matchMedia = function() {
470463
var currImg = imgs[ h ];
471464

472465
if ( currImg.parentNode.nodeName.toUpperCase() === "PICTURE" ||
473-
( currImg.getAttribute( "srcset" ) !== null ) || currImg[ pf.ns ] && currImg[ pf.ns ].srcset !== null ) {
474-
elems.push( currImg );
466+
( currImg.getAttribute( "srcset" ) !== null ) || currImg[ pf.ns ] && currImg[ pf.ns ].srcset !== null ) {
467+
elems.push( currImg );
475468
}
476469
}
477470
return elems;
@@ -501,7 +494,7 @@ window.matchMedia || (window.matchMedia = function() {
501494
continue;
502495
}
503496
// if it's a source element that has the `src` property set, throw a warning in the console
504-
if ( source.getAttribute( "src" ) !== null && typeof console !== undefined ){
497+
if ( source.getAttribute( "src" ) !== null && typeof console !== undefined ) {
505498
console.warn("The `src` attribute is invalid on `picture` `source` element; instead, use `srcset`.");
506499
}
507500

@@ -534,8 +527,8 @@ window.matchMedia || (window.matchMedia = function() {
534527
parent,
535528
firstMatch,
536529
candidates,
530+
options = opt || {};
537531

538-
options = opt || {};
539532
elements = options.elements || pf.getAllElements();
540533

541534
// Loop through all elements
@@ -545,13 +538,18 @@ window.matchMedia || (window.matchMedia = function() {
545538
firstMatch = undefined;
546539
candidates = undefined;
547540

541+
// immediately skip non-`img` nodes
542+
if ( element.nodeName.toUpperCase() !== "IMG" ) {
543+
continue;
544+
}
545+
548546
// expando for caching data on the img
549547
if ( !element[ pf.ns ] ) {
550548
element[ pf.ns ] = {};
551549
}
552550

553-
// if the element has already been evaluated, skip it
554-
// unless `options.force` is set to true ( this, for example,
551+
// if the element has already been evaluated, skip it unless
552+
// `options.reevaluate` is set to true ( this, for example,
555553
// is set to true when running `picturefill` on `resize` ).
556554
if ( !options.reevaluate && element[ pf.ns ].evaluated ) {
557555
continue;
@@ -622,7 +620,8 @@ window.matchMedia || (window.matchMedia = function() {
622620

623621
function checkResize() {
624622
var resizeThrottle;
625-
if (!w._picturefillWorking) {
623+
624+
if ( !w._picturefillWorking ) {
626625
w._picturefillWorking = true;
627626
w.clearTimeout( resizeThrottle );
628627
resizeThrottle = w.setTimeout( function() {
@@ -648,7 +647,7 @@ window.matchMedia || (window.matchMedia = function() {
648647
if ( typeof module === "object" && typeof module.exports === "object" ) {
649648
// CommonJS, just export
650649
module.exports = picturefill;
651-
} else if ( typeof define === "function" && define.amd ){
650+
} else if ( typeof define === "function" && define.amd ) {
652651
// AMD support
653652
define( function() { return picturefill; } );
654653
} else if ( typeof w === "object" ) {

0 commit comments

Comments
 (0)