Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/block-components/image/image-optimizer-polyfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class ImageOptimizerPolyfill {
init = () => {
const imgs = document.querySelectorAll( '.stk-block img' )

let updateCount = 0
// Use Mutation Observer because the src and/or srcset attributes may change if using dynamic content
const MO = new MutationObserver( mutations => {
mutations.forEach( mutation => {
Expand All @@ -33,12 +34,24 @@ class ImageOptimizerPolyfill {
img.setAttribute( 'srcset', srcset )
}

if ( img.getAttribute( 'data-src' ).indexOf( '&fit' ) !== -1 ) {
const src = img.getAttribute( 'data-src' )
const index = src.indexOf( '&fit' )
const newSrc = src.slice( 0, index )
img.setAttribute( 'data-src', newSrc )
}

if ( img.getAttribute( 'src' ).indexOf( '&fit' ) !== -1 ) {
const src = img.getAttribute( 'src' )
const index = src.indexOf( '&fit' )
const newSrc = src.slice( 0, index )
img.setAttribute( 'src', newSrc )
}

updateCount++
if ( updateCount === imgs.length ) {
MO.disconnect()
}
} )
} )

Expand Down
Loading