Embeds: Handle deeply nested links in the embed click handler - #13244
Embeds: Handle deeply nested links in the embed click handler#13244itzmekhokan wants to merge 3 commits into
Conversation
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.
|
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 Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Test using WordPress PlaygroundThe 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
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
peterwilsoncc
left a comment
There was a problem hiding this comment.
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.
| href = target.parentElement.getAttribute( 'href' ); | ||
| } | ||
| var link = e.target.closest( '[href]' ), | ||
| href = link ? link.getAttribute( 'href' ) : null; |
There was a problem hiding this comment.
It would be preferable to have actually use the href property here:
| 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.hrefreturns"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!
There was a problem hiding this comment.
Alternatively:
| href = link ? link.getAttribute( 'href' ) : null; | |
| href = link?.href; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Used a ternary rather than
link?.href(file isn't transpiled, ES5 throughout), plus a type guard —closest( '[href]' )can match an SVG<a>, whosehrefis anSVGAnimatedStringthatpostMessagecan'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:
There was a problem hiding this comment.
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.
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
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
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()insrc/js/_enqueues/lib/embed-template.jsreadhreffrom the click target and, failing that, from its immediate parent only, so anything nested deeper resolved to no href. It now usesclosest( '[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
TypeErrorthrown 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.