From 0a6a2754ac4f7a35b44fd4dfeb674aa1ddadb124 Mon Sep 17 00:00:00 2001 From: Ella Date: Thu, 7 Nov 2024 16:57:10 +0100 Subject: [PATCH] prevent click on links --- .../block-editor/src/components/iframe/index.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/packages/block-editor/src/components/iframe/index.js b/packages/block-editor/src/components/iframe/index.js index e55a1fed9c627f..76d2e09dfb7a30 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 );