| 1 |
( function () { |
| 2 |
function recalculate() { |
| 3 |
if ( this.complete ) { |
| 4 |
// Support for lazy loading: if there is a lazy-src attribute and it's value |
| 5 |
// is not the same as the current src we should wait until the image load event |
| 6 |
var lazySrc = this.getAttribute( 'data-lazy-src' ); |
| 7 |
if ( lazySrc && this.src !== lazySrc ) { |
| 8 |
this.addEventListener( 'onload', recalculate ); |
| 9 |
return; |
| 10 |
} |
| 11 |
|
| 12 |
// Copying CSS width/height into element attributes. |
| 13 |
var width = this.width; |
| 14 |
var height = this.height; |
| 15 |
if ( width && width > 0 && height && height > 0 ) { |
| 16 |
this.setAttribute( 'width', width ); |
| 17 |
this.setAttribute( 'height', height ); |
| 18 |
|
| 19 |
reset_for_retina( this ); |
| 20 |
} |
| 21 |
} else { |
| 22 |
this.addEventListener( 'onload', recalculate ); |
| 23 |
return; |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* For images lacking explicit dimensions and needing them, try to add them. |
| 29 |
*/ |
| 30 |
var restore_dims = function () { |
| 31 |
var elements = document.querySelectorAll( 'img[data-recalc-dims]' ); |
| 32 |
for ( var i = 0; i < elements.length; i++ ) { |
| 33 |
recalculate.call( elements[ i ] ); |
| 34 |
} |
| 35 |
}, |
| 36 |
/** |
| 37 |
* Modify given image's markup so that devicepx-jetpack.js will act on the image and it won't be reprocessed by this script. |
| 38 |
*/ |
| 39 |
reset_for_retina = function ( img ) { |
| 40 |
img.removeAttribute( 'data-recalc-dims' ); |
| 41 |
img.removeAttribute( 'scale' ); |
| 42 |
}; |
| 43 |
|
| 44 |
/** |
| 45 |
* Check both when page loads, and when IS is triggered. |
| 46 |
*/ |
| 47 |
if ( typeof window !== 'undefined' && typeof document !== 'undefined' ) { |
| 48 |
// `DOMContentLoaded` may fire before the script has a chance to run |
| 49 |
if ( document.readyState === 'loading' ) { |
| 50 |
document.addEventListener( 'DOMContentLoaded', restore_dims ); |
| 51 |
} else { |
| 52 |
restore_dims(); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
document.body.addEventListener( 'is.post-load', restore_dims ); |
| 57 |
} )(); |
| 58 |
|