10
10
* Use an IntersectionObserver polyfill in case of IE11 support necessary.
11
11
*/
12
12
13
- 'use strict' ;
14
-
15
13
import './loading-attribute-polyfill.css' ;
16
14
17
- var config = {
15
+ const config = {
18
16
intersectionObserver : {
19
17
// Start download if the item gets within 256px in the Y axis
20
18
rootMargin : '0px 0px 256px 0px' ,
@@ -25,7 +23,7 @@ var config = {
25
23
} ;
26
24
27
25
// Device/browser capabilities object
28
- var capabilities = {
26
+ const capabilities = {
29
27
loading : {
30
28
image : 'loading' in HTMLImageElement . prototype ,
31
29
iframe : 'loading' in HTMLIFrameElement . prototype ,
@@ -39,7 +37,7 @@ if (window.NodeList && !NodeList.prototype.forEach) {
39
37
}
40
38
41
39
// Define according to browsers support of the IntersectionObserver feature (missing e.g. on IE11 or Safari 11)
42
- var intersectionObserver ;
40
+ let intersectionObserver ;
43
41
44
42
if ( 'IntersectionObserver' in window ) {
45
43
intersectionObserver = new IntersectionObserver (
@@ -53,7 +51,7 @@ if ('IntersectionObserver' in window) {
53
51
* @param {Object } lazyItem Current item to be restored after lazy loading.
54
52
*/
55
53
function restoreSource ( lazyItem ) {
56
- var srcsetItems = [ ] ;
54
+ let srcsetItems = [ ] ;
57
55
58
56
// Just in case the img is the decendent of a picture element, check for source tags
59
57
if ( lazyItem . parentNode . tagName . toLowerCase ( ) === 'picture' ) {
@@ -67,7 +65,7 @@ function restoreSource(lazyItem) {
67
65
srcsetItems . push ( lazyItem ) ;
68
66
69
67
// Not using .dataset within those upfollowing lines of code for polyfill independent compatibility down to IE9
70
- srcsetItems . forEach ( function ( item ) {
68
+ srcsetItems . forEach ( ( item ) => {
71
69
if ( item . hasAttribute ( 'data-lazy-srcset' ) ) {
72
70
item . setAttribute ( 'srcset' , item . getAttribute ( 'data-lazy-srcset' ) ) ;
73
71
item . removeAttribute ( 'data-lazy-srcset' ) ; // Not using delete .dataset here for compatibility down to IE9
@@ -83,7 +81,7 @@ function restoreSource(lazyItem) {
83
81
* @param {Object } lazyItemPicture Current <picture> item to be restored after lazy loading.
84
82
*/
85
83
function removePlaceholderSource ( lazyItemPicture ) {
86
- var placeholderSource = lazyItemPicture . querySelector (
84
+ const placeholderSource = lazyItemPicture . querySelector (
87
85
'source[data-lazy-remove]'
88
86
) ;
89
87
@@ -99,14 +97,14 @@ function removePlaceholderSource(lazyItemPicture) {
99
97
* @param {Object } observer IntersectionObserver instance reference
100
98
*/
101
99
function onIntersection ( entries , observer ) {
102
- entries . forEach ( function ( entry ) {
100
+ entries . forEach ( ( entry ) => {
103
101
// Mitigation for EDGE lacking support of .isIntersecting until v15, compare to e.g. https://github.com/w3c/IntersectionObserver/issues/211#issuecomment-309144669
104
102
if ( entry . intersectionRatio === 0 ) {
105
103
return ;
106
104
}
107
105
108
106
// If the item is visible now, load it and stop watching it
109
- var lazyItem = entry . target ;
107
+ const lazyItem = entry . target ;
110
108
111
109
observer . unobserve ( lazyItem ) ;
112
110
@@ -122,9 +120,9 @@ function onPrinting() {
122
120
return ;
123
121
}
124
122
125
- var mediaQueryList = window . matchMedia ( 'print' ) ;
123
+ const mediaQueryList = window . matchMedia ( 'print' ) ;
126
124
127
- mediaQueryList . addListener ( function ( mql ) {
125
+ mediaQueryList . addListener ( ( mql ) => {
128
126
if ( mql . matches ) {
129
127
document
130
128
. querySelectorAll (
@@ -133,7 +131,7 @@ function onPrinting() {
133
131
config . lazyIframe +
134
132
'[data-lazy-src]'
135
133
)
136
- . forEach ( function ( lazyItem ) {
134
+ . forEach ( ( lazyItem ) => {
137
135
restoreSource ( lazyItem ) ;
138
136
} ) ;
139
137
}
@@ -147,14 +145,14 @@ function onPrinting() {
147
145
*/
148
146
function getAndPrepareHTMLCode ( noScriptTag ) {
149
147
// The contents of a <noscript> tag are treated as text to JavaScript
150
- var lazyAreaHtml = noScriptTag . textContent || noScriptTag . innerHTML ;
148
+ let lazyAreaHtml = noScriptTag . textContent || noScriptTag . innerHTML ;
151
149
152
- var getImageWidth = lazyAreaHtml . match ( / w i d t h = [ ' " ] ( \d + ) [ ' " ] / ) || false ;
153
- var temporaryImageWidth = getImageWidth [ 1 ] || 1 ;
154
- var getImageHeight = lazyAreaHtml . match ( / h e i g h t = [ ' " ] ( \d + ) [ ' " ] / ) || false ;
155
- var temporaryImageHeight = getImageHeight [ 1 ] || 1 ;
150
+ const getImageWidth = lazyAreaHtml . match ( / w i d t h = [ ' " ] ( \d + ) [ ' " ] / ) || false ;
151
+ const temporaryImageWidth = getImageWidth [ 1 ] || 1 ;
152
+ const getImageHeight = lazyAreaHtml . match ( / h e i g h t = [ ' " ] ( \d + ) [ ' " ] / ) || false ;
153
+ const temporaryImageHeight = getImageHeight [ 1 ] || 1 ;
156
154
157
- var temporaryImage =
155
+ const temporaryImage =
158
156
'data:image/svg+xml,%3Csvg xmlns=%27http://www.w3.org/2000/svg%27 viewBox=%270 0 ' +
159
157
temporaryImageWidth +
160
158
' ' +
@@ -202,13 +200,13 @@ function getAndPrepareHTMLCode(noScriptTag) {
202
200
*/
203
201
function prepareElement ( noScriptTag ) {
204
202
// Sticking the noscript HTML code in the innerHTML of a new <div> tag to 'load' it after creating that <div>
205
- var lazyArea = document . createElement ( 'div' ) ;
203
+ const lazyArea = document . createElement ( 'div' ) ;
206
204
207
205
lazyArea . innerHTML = getAndPrepareHTMLCode ( noScriptTag ) ;
208
206
209
207
// Move all children out of the element
210
208
while ( lazyArea . firstChild ) {
211
- var actualChild = lazyArea . firstChild ;
209
+ const actualChild = lazyArea . firstChild ;
212
210
213
211
if (
214
212
capabilities . scrolling &&
@@ -220,7 +218,7 @@ function prepareElement(noScriptTag) {
220
218
( actualChild . tagName . toLowerCase ( ) === 'iframe' &&
221
219
! capabilities . loading . iframe ) )
222
220
) {
223
- var observedElement =
221
+ const observedElement =
224
222
actualChild . tagName . toLowerCase ( ) === 'picture'
225
223
? lazyArea . querySelector ( 'img' )
226
224
: actualChild ;
@@ -238,8 +236,8 @@ function prepareElement(noScriptTag) {
238
236
/**
239
237
* Get all the <noscript> tags on the page, prepare each and any one of them and setup the printing
240
238
*/
241
- let prepareElements = ( ) => {
242
- var lazyLoadAreas = document . querySelectorAll ( 'noscript.loading-lazy' ) ;
239
+ const prepareElements = ( ) => {
240
+ const lazyLoadAreas = document . querySelectorAll ( 'noscript.loading-lazy' ) ;
243
241
244
242
lazyLoadAreas . forEach ( ( element ) => prepareElement ( element ) ) ;
245
243
@@ -252,19 +250,19 @@ let prepareElements = () => {
252
250
if ( / c o m p | i n t e r / . test ( document . readyState ) ) {
253
251
prepareElements ( ) ;
254
252
} else if ( 'addEventListener' in document ) {
255
- document . addEventListener ( 'DOMContentLoaded' , function ( ) {
253
+ document . addEventListener ( 'DOMContentLoaded' , ( ) => {
256
254
prepareElements ( ) ;
257
255
} ) ;
258
256
} else {
259
- document . attachEvent ( 'onreadystatechange' , function ( ) {
257
+ document . attachEvent ( 'onreadystatechange' , ( ) => {
260
258
if ( document . readyState === 'complete' ) {
261
259
prepareElements ( ) ;
262
260
}
263
261
} ) ;
264
262
}
265
263
266
264
const loadingAttributePolyfill = {
267
- prepareElement : prepareElement ,
265
+ prepareElement,
268
266
} ;
269
267
270
268
export default loadingAttributePolyfill ;
0 commit comments