Skip to content

Commit

Permalink
Prevent link clicks
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Nov 7, 2024
1 parent d195508 commit 556860a
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions packages/block-editor/src/components/iframe/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,24 @@ 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' ) {
const href = event.target.getAttribute( 'href' );
if ( href.startsWith( '#' ) ) {
// Appending a hash to the current URL will not reload
// the page. This is useful for e.g. footnotes.
window.location.href = new URL(
href,
window.location.href
).toString();
} else {
event.preventDefault();
}
}
} );
}

node.addEventListener( 'load', onLoad );
Expand Down

0 comments on commit 556860a

Please sign in to comment.