Skip to content

Commit 7d6e8ee

Browse files
akabiruclaude
andcommitted
Guard isHrefExternal against non-web protocols
isHrefExternal treated mailto:, tel:, and javascript: URLs as external because new URL() yields origin "null" for non-web protocols, which never matches window.location.origin. This meant the a11y decoration plugin would incorrectly add the "opens in new tab" hint to non-web links. Add an explicit protocol check so only http/https URLs are considered external. This mirrors the existing guard in isExternalLinkCandidate but applies at the lower level so all callers are safe. Co-authored-by: Claude <noreply@anthropic.com>
1 parent 211ed27 commit 7d6e8ee

1 file changed

Lines changed: 5 additions & 0 deletions

File tree

frontend/src/stimulus/helpers/external-link-helpers.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,15 @@ export function isLinkBlank(link:HTMLAnchorElement) {
4545
* Returns true when the given href string points to a different origin than
4646
* the current page. Works with plain URL strings (e.g. from ProseMirror mark
4747
* attrs) where no HTMLAnchorElement is available.
48+
*
49+
* Only considers http/https URLs — non-web protocols (mailto:, tel:,
50+
* javascript:, etc.) return false because they don't navigate to an
51+
* external origin.
4852
*/
4953
export function isHrefExternal(href:string):boolean {
5054
try {
5155
const linkUrl = new URL(href, window.location.origin);
56+
if (!linkUrl.protocol.startsWith('http')) return false;
5257
return linkUrl.origin !== window.location.origin;
5358
} catch {
5459
return false;

0 commit comments

Comments
 (0)