Skip to content

Commit ea423be

Browse files
authored
fix (EWWW compatibility): use MO in polyfill and plugins_loaded (#3405)
1 parent 93a9cc6 commit ea423be

File tree

2 files changed

+45
-26
lines changed

2 files changed

+45
-26
lines changed

src/block-components/image/image-optimizer-polyfill.js

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,41 @@ class ImageOptimizerPolyfill {
1111
*/
1212
init = () => {
1313
const imgs = document.querySelectorAll( '.stk-block img' )
14-
imgs.forEach( img => {
15-
if ( img.hasAttribute( 'srcset' ) ) {
16-
let srcset = img.getAttribute( 'srcset' )
17-
const pattern = /https?:\/\/[^\s,]+/g
18-
const urls = srcset.match( pattern )
19-
20-
urls.forEach( url => {
21-
const index = url.indexOf( '&fit' )
22-
if ( index !== -1 ) {
23-
const newUrl = url.slice( 0, index )
24-
srcset = srcset.replace( url, newUrl )
25-
}
26-
} )
27-
28-
img.setAttribute( 'srcset', srcset )
29-
}
30-
31-
if ( img.getAttribute( 'src' ).indexOf( '&fit' ) !== -1 ) {
32-
const src = img.getAttribute( 'src' )
33-
const index = src.indexOf( '&fit' )
34-
const newSrc = src.slice( 0, index )
35-
img.setAttribute( 'src', newSrc )
36-
}
14+
15+
// Use Mutation Observer because the src and/or srcset attributes may change if using dynamic content
16+
const MO = new MutationObserver( mutations => {
17+
mutations.forEach( mutation => {
18+
const img = mutation.target
19+
20+
if ( img.hasAttribute( 'srcset' ) ) {
21+
let srcset = img.getAttribute( 'srcset' )
22+
const pattern = /https?:\/\/[^\s,]+/g
23+
const urls = srcset.match( pattern )
24+
25+
urls.forEach( url => {
26+
const index = url.indexOf( '&fit' )
27+
if ( index !== -1 ) {
28+
const newUrl = url.slice( 0, index )
29+
srcset = srcset.replace( url, newUrl )
30+
}
31+
} )
32+
33+
img.setAttribute( 'srcset', srcset )
34+
}
35+
36+
if ( img.getAttribute( 'src' ).indexOf( '&fit' ) !== -1 ) {
37+
const src = img.getAttribute( 'src' )
38+
const index = src.indexOf( '&fit' )
39+
const newSrc = src.slice( 0, index )
40+
img.setAttribute( 'src', newSrc )
41+
}
42+
} )
43+
} )
44+
45+
imgs.forEach( image => {
46+
MO.observe( image, {
47+
attributeFilter: [ 'src', 'srcset' ],
48+
} )
3749
} )
3850
}
3951
}

src/block-components/image/index.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,15 @@ function stackable_load_image_optimizer_polyfill_frontend_script( $block_content
2323
}
2424
}
2525

26-
if ( ! is_admin() && defined( 'EWWW_IMAGE_OPTIMIZER_PLUGIN_FILE' )) {
27-
// Load the script in the frontend if EWWW Image Optimizer is active.
28-
add_action( 'stackable/enqueue_scripts', 'stackable_load_image_optimizer_polyfill_frontend_script', 10, 2 );
26+
function stackable_ewww_image_optimzer_plugin_checker() {
27+
if ( ! is_admin() && defined( 'EWWW_IMAGE_OPTIMIZER_PLUGIN_FILE' ) ) {
28+
// Load the script in the frontend if EWWW Image Optimizer is active.
29+
add_action( 'stackable/enqueue_scripts', 'stackable_load_image_optimizer_polyfill_frontend_script', 10, 2 );
30+
}
2931
}
32+
33+
// Run the plugin checker after all plugins are loaded because
34+
// the condition defined( 'EWWW_IMAGE_OPTIMIZER_PLUGIN_FILE' ) may return false
35+
// even if the plugin is actually activated
36+
add_action( 'plugins_loaded', 'stackable_ewww_image_optimzer_plugin_checker' );
3037
}

0 commit comments

Comments
 (0)