Skip to content

Embeds: Handle deeply nested links in the embed click handler - #13244

Closed
itzmekhokan wants to merge 3 commits into
WordPress:trunkfrom
itzmekhokan:fix/65947-embed-nested-link-clicks
Closed

Embeds: Handle deeply nested links in the embed click handler#13244
itzmekhokan wants to merge 3 commits into
WordPress:trunkfrom
itzmekhokan:fix/65947-embed-nested-link-clicks

Conversation

@itzmekhokan

Copy link
Copy Markdown

Clicking a link in a post embed opens it inside the embed iframe instead of the parent window when the link text is wrapped more than one element deep, for example <a href="..."><span><span>Link text</span></span></a>.

linkClickHandler() in src/js/_enqueues/lib/embed-template.js read href from the click target and, failing that, from its immediate parent only, so anything nested deeper resolved to no href. It now uses closest( '[href]' ) to walk the full ancestor chain, keeping the same attribute semantics.

The change is strictly widening — direct and single-level-nested links resolve exactly as before — and it also removes a TypeError thrown when the click target has no parent element.

Trac ticket: https://core.trac.wordpress.org/ticket/65947

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5
Used for: Diagnosing the DOM traversal bug, drafting the fix, and verifying link resolution against a real DOM. All changes were reviewed and validated by me.


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

The click handler in the embed template only looked for an `href` on the
event target and its immediate parent, so link text wrapped more than one
element deep resolved to no href and the link opened inside the embed
iframe instead of the parent window.

Use `closest( '[href]' )` to walk the full ancestor chain instead. This
also removes a `TypeError` thrown when the click target has no parent
element.

Fixes #65947.
@github-actions

github-actions Bot commented Aug 24, 2026

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props khokansardar, peterwilsoncc, westonruter.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions

Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

@peterwilsoncc peterwilsoncc left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the pull request, this looks good to me.

I've tested various scenarios using jsbin and your updated code will behave as expected.

Comment thread src/js/_enqueues/lib/embed-template.js Outdated
href = target.parentElement.getAttribute( 'href' );
}
var link = e.target.closest( '[href]' ),
href = link ? link.getAttribute( 'href' ) : null;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be preferable to have actually use the href property here:

Suggested change
href = link ? link.getAttribute( 'href' ) : null;
href = link ? link.href : null;

This is because .getAttribute( 'href' ) returns the HTML attribute contents verbatim, whereas the .href DOM property is parsed to be the actual absolute URL. This is important if there is a tag like <a href="/">Go home</a>:

  • link.getAttribute('href') returns "/"
  • link.href returns "https://example.com/"

See https://developer.mozilla.org/en-US/docs/Web/API/HTMLAnchorElement/href
and https://jakearchibald.com/2024/attributes-vs-properties/

This is especially important here in a cross-origin context!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively:

Suggested change
href = link ? link.getAttribute( 'href' ) : null;
href = link?.href;

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @westonruter — applied.

Also fixes a second bug: wp-embed.js calls new URL( data.value ) with no base, which throws on a relative href — and preventDefault() has already run, so the click silently dead-ends.

Used a ternary rather than link?.href (file isn't transpiled, ES5 throughout), plus a type guard — closest( '[href]' ) can match an SVG <a>, whose href is an SVGAnimatedString that postMessage can't clone:

href = link && 'string' === typeof link.href ? link.href : null;

Note href="" and href="#foo" now navigate to the ?embed=true URL instead of dead-ending — can skip fragment-only values if you prefer.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Used a ternary rather than link?.href (file isn't transpiled, ES5 throughout), plus a type guard — closest( '[href]' ) can match an SVG <a>, whose href is an SVGAnimatedString that postMessage can't clone:

We don't need to transpile this. We can use optional chaining.

It is supported in all browsers supported by WP: https://caniuse.com/mdn-javascript_operators_optional_chaining

It is used in non-transpiled files in core now. For example:

let options = settings.codemirror?.lint ?? false;

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switched to optional chaining, thanks. Kept the typeof check since it guards a different case than the null-check

The href DOM property resolves to an absolute URL against the embed
document, which the parent window requires. Reading the attribute
returned the value verbatim, so a relative link threw in the parent's
`new URL()` call and the click dead-ended after `preventDefault()`.

Elements whose href is not a string, such as SVG anchors, are skipped.
Optional chaining is supported in every browser WordPress supports and
is already used in non-transpiled core files, so the explicit ternary
guard against a missing link is unnecessary.

The string type check remains, so elements whose href is not a string,
such as SVG anchors, are still skipped.
pento pushed a commit that referenced this pull request Aug 30, 2026
Update `linkClickHandler()` to follow links with nested sub elements within the `_top` window, for example `<a href="http://wordpress.org"><span><span>WordPress</span></span>></a>`.

Previously the link handler only handled links with single nested elements, links further nested would open within the iframe.

Developed in #13244.

Props khokansardar, swissspidy, westonruter.
Fixes #65947.



git-svn-id: https://develop.svn.wordpress.org/trunk@63400 602fd350-edb4-49c9-b593-d223f7449a82
@github-actions

Copy link
Copy Markdown

A commit was made that fixes the Trac ticket referenced in the description of this pull request.

SVN changeset: 63400
GitHub commit: f819b4a

This PR will be closed, but please confirm the accuracy of this and reopen if there is more work to be done.

@github-actions github-actions Bot closed this Aug 30, 2026
markjaquith pushed a commit to markjaquith/WordPress that referenced this pull request Aug 30, 2026
Update `linkClickHandler()` to follow links with nested sub elements within the `_top` window, for example `<a href="http://wordpress.org"><span><span>WordPress</span></span>></a>`.

Previously the link handler only handled links with single nested elements, links further nested would open within the iframe.

Developed in WordPress/wordpress-develop#13244.

Props khokansardar, swissspidy, westonruter.
Fixes #65947.


Built from https://develop.svn.wordpress.org/trunk@63400


git-svn-id: http://core.svn.wordpress.org/trunk@62593 1a063a9b-81f0-0310-95a4-ce76da25c4cd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants