Aucune description

photon.js 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. // Copying CSS width/height into element attributes.
  12. var width = this.width;
  13. var height = this.height;
  14. if ( width && width > 0 && height && height > 0 ) {
  15. this.setAttribute( 'width', width );
  16. this.setAttribute( 'height', height );
  17. reset_for_retina( this );
  18. }
  19. } else {
  20. this.addEventListener( 'onload', recalculate );
  21. return;
  22. }
  23. }
  24. /**
  25. * For images lacking explicit dimensions and needing them, try to add them.
  26. */
  27. var restore_dims = function () {
  28. var elements = document.querySelectorAll( 'img[data-recalc-dims]' );
  29. for ( var i = 0; i < elements.length; i++ ) {
  30. recalculate.call( elements[ i ] );
  31. }
  32. },
  33. /**
  34. * Modify given image's markup so that devicepx-jetpack.js will act on the image and it won't be reprocessed by this script.
  35. */
  36. reset_for_retina = function ( img ) {
  37. img.removeAttribute( 'data-recalc-dims' );
  38. img.removeAttribute( 'scale' );
  39. };
  40. /**
  41. * Check both when page loads, and when IS is triggered.
  42. */
  43. if ( typeof window !== 'undefined' && typeof document !== 'undefined' ) {
  44. // `DOMContentLoaded` may fire before the script has a chance to run
  45. if ( document.readyState === 'loading' ) {
  46. document.addEventListener( 'DOMContentLoaded', restore_dims );
  47. } else {
  48. restore_dims();
  49. }
  50. }
  51. document.body.addEventListener( 'is.post-load', restore_dims );
  52. } )();