diff --git a/packages/block-editor/src/components/iframe/index.js b/packages/block-editor/src/components/iframe/index.js index 85e1f12a7c0d6..76d2e09dfb7a3 100644 --- a/packages/block-editor/src/components/iframe/index.js +++ b/packages/block-editor/src/components/iframe/index.js @@ -191,6 +191,22 @@ function Iframe( { preventFileDropDefault, false ); + // Prevent clicks on links from navigating away. Note that links + // inside `contenteditable` are already disabled by the browser, so + // this is for links in blocks outside of `contenteditable`. + iFrameDocument.addEventListener( 'click', ( event ) => { + if ( event.target.tagName === 'A' ) { + event.preventDefault(); + + // Appending a hash to the current URL will not reload the + // page. This is useful for e.g. footnotes. + const href = event.target.getAttribute( 'href' ); + if ( href.startsWith( '#' ) ) { + iFrameDocument.defaultView.location.hash = + href.slice( 1 ); + } + } + } ); } node.addEventListener( 'load', onLoad ); @@ -272,6 +288,7 @@ function Iframe( { +