Skip to content

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

Open
itzmekhokan wants to merge 1 commit into
WordPress:trunkfrom
itzmekhokan:fix/65947-embed-nested-link-clicks
Open

Embeds: Handle deeply nested links in the embed click handler#13244
itzmekhokan wants to merge 1 commit 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

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.

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.

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.

2 participants