Skip to content

[6.x] Fix is_external for anchor, mailto and tel links in navs - #15082

Open
duncanmcclean wants to merge 2 commits into
6.xfrom
nav-anchor-is-external
Open

[6.x] Fix is_external for anchor, mailto and tel links in navs#15082
duncanmcclean wants to merge 2 commits into
6.xfrom
nav-anchor-is-external

Conversation

@duncanmcclean

Copy link
Copy Markdown
Member

This pull request fixes an issue where a navigation item pointing at an anchor on the current site, like /#anchor, was reported with is_external => true by the {{ nav }} tag.

This was happening because URL::isExternal() appended a trailing slash to the whole URL, fragment included. The nav item's absolute URL (http://site.test#anchor) became http://site.test#anchor/, which no longer started with the site URL, so it was considered external.

This PR fixes it by stripping the query string and fragment before comparing against the site URL.

While in there, I noticed mailto: and tel: links had the opposite problem — they were being reported as internal. This was because URL::makeAbsolute() prepended the site URL to anything without an http/https scheme, turning mailto:hello@statamic.com into http://site.test/mailto:hello@statamic.com. This PR leaves URLs with other schemes alone, and also treats protocol-relative URLs (//statamic.com) as external.

Fixes #15081

`URL::isExternal()` appended a trailing slash to the whole URL, fragment
included, so `http://site.test#anchor` became `http://site.test#anchor/`
and no longer matched the site URL. It now strips the query string and
fragment before comparing, and treats protocol-relative URLs as external.

`URL::makeAbsolute()` prepended the site URL to anything without an
`http`/`https` scheme, turning `mailto:` and `tel:` links into internal
ones. Those are now left alone.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@duncanmcclean

Copy link
Copy Markdown
Member Author

#15083 will fix the PHPStan failures.

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.

Anchor link is wrongly classified as is_external

2 participants