Skip to content

Commit 6c123f7

Browse files
Copilotpattonwebz
andcommitted
Fix: trigger accessibility rescan after Elementor post save
Co-authored-by: pattonwebz <3902039+pattonwebz@users.noreply.github.com> Agent-Logs-Url: https://github.com/equalizedigital/accessibility-checker/sessions/11ed3e03-813c-47b9-9721-22a8a21ff2f8
1 parent a0c478f commit 6c123f7

1 file changed

Lines changed: 45 additions & 1 deletion

File tree

src/frontendHighlighterApp/index.js

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1486,16 +1486,60 @@ class AccessibilityCheckerHighlight {
14861486
}
14871487
}
14881488

1489+
/**
1490+
* Set up a listener for Elementor save events and trigger an automatic rescan.
1491+
*
1492+
* When a post is saved in the Elementor editor the front-end highlighter report
1493+
* must update to reflect the latest accessibility-check data. Elementor runs the
1494+
* page preview inside an iframe; this function accesses the parent window's
1495+
* Elementor editor object (`window.parent.elementor`) and attaches a one-time
1496+
* handler to the `after:save` event exposed by Elementor's saver module so that
1497+
* `rescanPage()` is called as soon as Elementor finishes saving.
1498+
*
1499+
* A try/catch guards the cross-origin boundary: if the parent window is on a
1500+
* different origin the access attempt will throw a `DOMException` which is
1501+
* silently swallowed so normal page operation is not affected.
1502+
*
1503+
* @param {AccessibilityCheckerHighlight} highlighter The active highlighter instance.
1504+
*/
1505+
const setupElementorSaveListener = ( highlighter ) => {
1506+
try {
1507+
// Only proceed when this script is running inside an iframe (Elementor preview).
1508+
if ( window === window.parent ) {
1509+
return;
1510+
}
1511+
1512+
const parentElementor = window.parent?.elementor;
1513+
if ( ! parentElementor?.saver ) {
1514+
return;
1515+
}
1516+
1517+
// Use Backbone's `listenTo` pattern via `on` – `initHighlighter` is guarded by
1518+
// `highlighterInitialized` so this handler is only ever registered once per page load.
1519+
parentElementor.saver.on( 'after:save', () => {
1520+
highlighter.rescanPage();
1521+
} );
1522+
} catch ( e ) {
1523+
// Accessing a cross-origin parent window throws a SecurityError DOMException.
1524+
// Re-throw anything unexpected so genuine programming errors remain visible.
1525+
if ( e instanceof DOMException ) {
1526+
return;
1527+
}
1528+
throw e;
1529+
}
1530+
};
1531+
14891532
// Some systems (Cloudflare Rocket Loader) defers scripts for performance but that can
14901533
// cause some DOMContentLoaded events to be missed. This is flag tracks if it run so we
14911534
// can retry at a latter event listener.
14921535
let highlighterInitialized = false;
14931536
const initHighlighter = () => {
14941537
if ( ! highlighterInitialized ) {
1495-
new AccessibilityCheckerHighlight();
1538+
const highlighter = new AccessibilityCheckerHighlight();
14961539
if ( window.edacFrontendHighlighterApp?.userCanFix ) {
14971540
fixSettingsModalInit();
14981541
}
1542+
setupElementorSaveListener( highlighter );
14991543
highlighterInitialized = true;
15001544
}
15011545
};

0 commit comments

Comments
 (0)