1010 * Use an IntersectionObserver polyfill in case of IE11 support necessary.
1111 */
1212
13- 'use strict' ;
14-
1513import './loading-attribute-polyfill.css' ;
1614
17- var config = {
15+ const config = {
1816 intersectionObserver : {
1917 // Start download if the item gets within 256px in the Y axis
2018 rootMargin : '0px 0px 256px 0px' ,
@@ -25,7 +23,7 @@ var config = {
2523} ;
2624
2725// Device/browser capabilities object
28- var capabilities = {
26+ const capabilities = {
2927 loading : {
3028 image : 'loading' in HTMLImageElement . prototype ,
3129 iframe : 'loading' in HTMLIFrameElement . prototype ,
@@ -39,7 +37,7 @@ if (window.NodeList && !NodeList.prototype.forEach) {
3937}
4038
4139// Define according to browsers support of the IntersectionObserver feature (missing e.g. on IE11 or Safari 11)
42- var intersectionObserver ;
40+ let intersectionObserver ;
4341
4442if ( 'IntersectionObserver' in window ) {
4543 intersectionObserver = new IntersectionObserver (
@@ -53,7 +51,7 @@ if ('IntersectionObserver' in window) {
5351 * @param {Object } lazyItem Current item to be restored after lazy loading.
5452 */
5553function restoreSource ( lazyItem ) {
56- var srcsetItems = [ ] ;
54+ let srcsetItems = [ ] ;
5755
5856 // Just in case the img is the decendent of a picture element, check for source tags
5957 if ( lazyItem . parentNode . tagName . toLowerCase ( ) === 'picture' ) {
@@ -67,7 +65,7 @@ function restoreSource(lazyItem) {
6765 srcsetItems . push ( lazyItem ) ;
6866
6967 // 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 ) => {
7169 if ( item . hasAttribute ( 'data-lazy-srcset' ) ) {
7270 item . setAttribute ( 'srcset' , item . getAttribute ( 'data-lazy-srcset' ) ) ;
7371 item . removeAttribute ( 'data-lazy-srcset' ) ; // Not using delete .dataset here for compatibility down to IE9
@@ -83,7 +81,7 @@ function restoreSource(lazyItem) {
8381 * @param {Object } lazyItemPicture Current <picture> item to be restored after lazy loading.
8482 */
8583function removePlaceholderSource ( lazyItemPicture ) {
86- var placeholderSource = lazyItemPicture . querySelector (
84+ const placeholderSource = lazyItemPicture . querySelector (
8785 'source[data-lazy-remove]'
8886 ) ;
8987
@@ -99,14 +97,14 @@ function removePlaceholderSource(lazyItemPicture) {
9997 * @param {Object } observer IntersectionObserver instance reference
10098 */
10199function onIntersection ( entries , observer ) {
102- entries . forEach ( function ( entry ) {
100+ entries . forEach ( ( entry ) => {
103101 // Mitigation for EDGE lacking support of .isIntersecting until v15, compare to e.g. https://github.com/w3c/IntersectionObserver/issues/211#issuecomment-309144669
104102 if ( entry . intersectionRatio === 0 ) {
105103 return ;
106104 }
107105
108106 // If the item is visible now, load it and stop watching it
109- var lazyItem = entry . target ;
107+ const lazyItem = entry . target ;
110108
111109 observer . unobserve ( lazyItem ) ;
112110
@@ -122,9 +120,9 @@ function onPrinting() {
122120 return ;
123121 }
124122
125- var mediaQueryList = window . matchMedia ( 'print' ) ;
123+ const mediaQueryList = window . matchMedia ( 'print' ) ;
126124
127- mediaQueryList . addListener ( function ( mql ) {
125+ mediaQueryList . addListener ( ( mql ) => {
128126 if ( mql . matches ) {
129127 document
130128 . querySelectorAll (
@@ -133,7 +131,7 @@ function onPrinting() {
133131 config . lazyIframe +
134132 '[data-lazy-src]'
135133 )
136- . forEach ( function ( lazyItem ) {
134+ . forEach ( ( lazyItem ) => {
137135 restoreSource ( lazyItem ) ;
138136 } ) ;
139137 }
@@ -147,14 +145,14 @@ function onPrinting() {
147145 */
148146function getAndPrepareHTMLCode ( noScriptTag ) {
149147 // 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 ;
151149
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 ;
156154
157- var temporaryImage =
155+ const temporaryImage =
158156 'data:image/svg+xml,%3Csvg xmlns=%27http://www.w3.org/2000/svg%27 viewBox=%270 0 ' +
159157 temporaryImageWidth +
160158 ' ' +
@@ -202,13 +200,13 @@ function getAndPrepareHTMLCode(noScriptTag) {
202200 */
203201function prepareElement ( noScriptTag ) {
204202 // 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' ) ;
206204
207205 lazyArea . innerHTML = getAndPrepareHTMLCode ( noScriptTag ) ;
208206
209207 // Move all children out of the element
210208 while ( lazyArea . firstChild ) {
211- var actualChild = lazyArea . firstChild ;
209+ const actualChild = lazyArea . firstChild ;
212210
213211 if (
214212 capabilities . scrolling &&
@@ -220,7 +218,7 @@ function prepareElement(noScriptTag) {
220218 ( actualChild . tagName . toLowerCase ( ) === 'iframe' &&
221219 ! capabilities . loading . iframe ) )
222220 ) {
223- var observedElement =
221+ const observedElement =
224222 actualChild . tagName . toLowerCase ( ) === 'picture'
225223 ? lazyArea . querySelector ( 'img' )
226224 : actualChild ;
@@ -238,8 +236,8 @@ function prepareElement(noScriptTag) {
238236/**
239237 * Get all the <noscript> tags on the page, prepare each and any one of them and setup the printing
240238 */
241- let prepareElements = ( ) => {
242- var lazyLoadAreas = document . querySelectorAll ( 'noscript.loading-lazy' ) ;
239+ const prepareElements = ( ) => {
240+ const lazyLoadAreas = document . querySelectorAll ( 'noscript.loading-lazy' ) ;
243241
244242 lazyLoadAreas . forEach ( ( element ) => prepareElement ( element ) ) ;
245243
@@ -252,19 +250,19 @@ let prepareElements = () => {
252250if ( / c o m p | i n t e r / . test ( document . readyState ) ) {
253251 prepareElements ( ) ;
254252} else if ( 'addEventListener' in document ) {
255- document . addEventListener ( 'DOMContentLoaded' , function ( ) {
253+ document . addEventListener ( 'DOMContentLoaded' , ( ) => {
256254 prepareElements ( ) ;
257255 } ) ;
258256} else {
259- document . attachEvent ( 'onreadystatechange' , function ( ) {
257+ document . attachEvent ( 'onreadystatechange' , ( ) => {
260258 if ( document . readyState === 'complete' ) {
261259 prepareElements ( ) ;
262260 }
263261 } ) ;
264262}
265263
266264const loadingAttributePolyfill = {
267- prepareElement : prepareElement ,
265+ prepareElement,
268266} ;
269267
270268export default loadingAttributePolyfill ;
0 commit comments