@@ -96,13 +96,12 @@ window.matchMedia || (window.matchMedia = function() {
96
96
/**
97
97
* Shortcut method for matchMedia ( for easy overriding in tests )
98
98
*/
99
+
99
100
pf . matchesMedia = function ( media ) {
100
101
return w . matchMedia && w . matchMedia ( media ) . matches ;
101
102
} ;
102
103
103
- /**
104
- * Shortcut method for `devicePixelRatio` ( for easy overriding in tests )
105
- */
104
+ // Shortcut method for `devicePixelRatio` ( for easy overriding in tests )
106
105
pf . getDpr = function ( ) {
107
106
return ( w . devicePixelRatio || 1 ) ;
108
107
} ;
@@ -115,11 +114,11 @@ window.matchMedia || (window.matchMedia = function() {
115
114
// 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.
116
115
length = length && length . indexOf ( "%" ) > - 1 === false && ( parseFloat ( length ) > 0 || length . indexOf ( "calc(" ) > - 1 ) ? length : "100vw" ;
117
116
/**
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
+ */
123
122
length = length . replace ( "vw" , "%" ) ;
124
123
125
124
// Create a cached element for getting length value widths
@@ -152,10 +151,10 @@ window.matchMedia || (window.matchMedia = function() {
152
151
// container of supported mime types that one might need to qualify before using
153
152
pf . types = { } ;
154
153
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 ;
159
158
160
159
// test svg support
161
160
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() {
199
198
}
200
199
} ;
201
200
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
205
202
pf . parseSize = function ( sourceSizeStr ) {
206
203
var match = / ( \( [ ^ ) ] + \) ) ? \s * ( .+ ) / g. exec ( sourceSizeStr ) ;
207
204
return {
@@ -210,9 +207,7 @@ window.matchMedia || (window.matchMedia = function() {
210
207
} ;
211
208
} ;
212
209
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
216
211
pf . findWidthFromSourceSize = function ( sourceSizeListStr ) {
217
212
// Split up source size list, ie ( max-width: 30em ) 100%, ( max-width: 50em ) 50%, 33%
218
213
// or (min-width:30em) calc(30% - 15px)
@@ -245,19 +240,19 @@ window.matchMedia || (window.matchMedia = function() {
245
240
246
241
pf . parseSrcset = function ( srcset ) {
247
242
/**
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
+ */
257
252
var candidates = [ ] ;
258
253
259
254
while ( srcset !== "" ) {
260
- srcset = srcset . replace ( / ^ \s + / g, "" ) ;
255
+ srcset = srcset . replace ( / ^ \s + / g, "" ) ;
261
256
262
257
// 5. Collect a sequence of characters that are not space characters, and let that be url.
263
258
var pos = srcset . search ( / \s / g) ,
@@ -273,15 +268,15 @@ window.matchMedia || (window.matchMedia = function() {
273
268
// 6.1. If url is empty, then jump to the step labeled descriptor parser.
274
269
275
270
if ( last === "," || url === "" ) {
276
- url = url . replace ( / , + $ / , "" ) ;
271
+ url = url . replace ( / , + $ / , "" ) ;
277
272
descriptor = "" ;
278
273
}
279
274
srcset = srcset . slice ( pos + 1 ) ;
280
275
281
276
// 6.2. Collect a sequence of characters that are not U+002C COMMA characters (,), and
282
277
// let that be descriptors.
283
278
if ( descriptor === null ) {
284
- var descpos = srcset . indexOf ( "," ) ;
279
+ var descpos = srcset . indexOf ( "," ) ;
285
280
if ( descpos !== - 1 ) {
286
281
descriptor = srcset . slice ( 0 , descpos ) ;
287
282
srcset = srcset . slice ( descpos + 1 ) ;
@@ -310,7 +305,7 @@ window.matchMedia || (window.matchMedia = function() {
310
305
// 11. Descriptor parser: Let candidates be an initially empty source set. The order of entries in the list
311
306
// is the order in which entries are added to the list.
312
307
var sizes = sizesattr || "100vw" ,
313
- sizeDescriptor = descriptor && descriptor . replace ( / ( ^ \s + | \s + $ ) / g, "" ) ,
308
+ sizeDescriptor = descriptor && descriptor . replace ( / ( ^ \s + | \s + $ ) / g, "" ) ,
314
309
widthInCssPixels = pf . findWidthFromSourceSize ( sizes ) ,
315
310
resCandidate ;
316
311
@@ -357,7 +352,7 @@ window.matchMedia || (window.matchMedia = function() {
357
352
return formattedCandidates ;
358
353
} ;
359
354
360
- /*
355
+ /**
361
356
* if it's an img element and it has a srcset property,
362
357
* we need to remove the attribute so we can manipulate src
363
358
* (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() {
370
365
}
371
366
} ;
372
367
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
376
369
pf . processSourceSet = function ( el ) {
377
370
var srcset = el . getAttribute ( "srcset" ) ,
378
371
sizes = el . getAttribute ( "sizes" ) ,
@@ -437,7 +430,7 @@ window.matchMedia || (window.matchMedia = function() {
437
430
return a . resolution - b . resolution ;
438
431
} ;
439
432
440
- /*
433
+ /**
441
434
* In IE9, <source> elements get removed if they aren't children of
442
435
* video elements. Thus, we conditionally wrap source elements
443
436
* using <!--[if IE 9]><video style="display: none;"><![endif]-->
@@ -457,7 +450,7 @@ window.matchMedia || (window.matchMedia = function() {
457
450
}
458
451
} ;
459
452
460
- /*
453
+ /**
461
454
* Find all `img` elements, and add them to the candidate list if they have
462
455
* a `picture` parent, a `sizes` attribute in basic `srcset` supporting browsers,
463
456
* a `srcset` attribute at all, and they haven’t been evaluated already.
@@ -470,8 +463,8 @@ window.matchMedia || (window.matchMedia = function() {
470
463
var currImg = imgs [ h ] ;
471
464
472
465
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 ) ;
475
468
}
476
469
}
477
470
return elems ;
@@ -501,7 +494,7 @@ window.matchMedia || (window.matchMedia = function() {
501
494
continue ;
502
495
}
503
496
// 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 ) {
505
498
console . warn ( "The `src` attribute is invalid on `picture` `source` element; instead, use `srcset`." ) ;
506
499
}
507
500
@@ -534,8 +527,8 @@ window.matchMedia || (window.matchMedia = function() {
534
527
parent ,
535
528
firstMatch ,
536
529
candidates ,
530
+ options = opt || { } ;
537
531
538
- options = opt || { } ;
539
532
elements = options . elements || pf . getAllElements ( ) ;
540
533
541
534
// Loop through all elements
@@ -545,13 +538,18 @@ window.matchMedia || (window.matchMedia = function() {
545
538
firstMatch = undefined ;
546
539
candidates = undefined ;
547
540
541
+ // immediately skip non-`img` nodes
542
+ if ( element . nodeName . toUpperCase ( ) !== "IMG" ) {
543
+ continue ;
544
+ }
545
+
548
546
// expando for caching data on the img
549
547
if ( ! element [ pf . ns ] ) {
550
548
element [ pf . ns ] = { } ;
551
549
}
552
550
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,
555
553
// is set to true when running `picturefill` on `resize` ).
556
554
if ( ! options . reevaluate && element [ pf . ns ] . evaluated ) {
557
555
continue ;
@@ -622,7 +620,8 @@ window.matchMedia || (window.matchMedia = function() {
622
620
623
621
function checkResize ( ) {
624
622
var resizeThrottle ;
625
- if ( ! w . _picturefillWorking ) {
623
+
624
+ if ( ! w . _picturefillWorking ) {
626
625
w . _picturefillWorking = true ;
627
626
w . clearTimeout ( resizeThrottle ) ;
628
627
resizeThrottle = w . setTimeout ( function ( ) {
@@ -648,7 +647,7 @@ window.matchMedia || (window.matchMedia = function() {
648
647
if ( typeof module === "object" && typeof module . exports === "object" ) {
649
648
// CommonJS, just export
650
649
module . exports = picturefill ;
651
- } else if ( typeof define === "function" && define . amd ) {
650
+ } else if ( typeof define === "function" && define . amd ) {
652
651
// AMD support
653
652
define ( function ( ) { return picturefill ; } ) ;
654
653
} else if ( typeof w === "object" ) {
0 commit comments