feat(web): open every external link in a new tab, derived from the URL (MITHOME-124)
General rule: links to another host open in a new tab with rel="noopener noreferrer"; internal paths, mailto: and tel: stay in the same tab. Behaviour was inconsistent before — the home Webmail button opened in the same tab while the other webmail links did not, and the CTA and header relied on a manual "external" switch. src/lib/external-link.ts decides from the URL (externalLinkProps for JSX, openExternalLinksInNewTab for CMS-provided HTML in About paragraphs, legal pages and the GDPR label). All link sites use it; the manual "external" CTA checkbox and NavigationItem flag are removed so the rule cannot be bypassed. Rule documented in architecture.md and CLAUDE.md; staging smoke SMOKE-07 checks every external homepage link. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
183b304687
commit
ca4d447d73
@@ -0,0 +1,73 @@
|
||||
import { externalLinkProps, isExternalHref, openExternalLinksInNewTab } from './external-link'
|
||||
|
||||
const SITE = 'https://stage.mozdit.hu'
|
||||
|
||||
describe('isExternalHref (MITHOME-124)', () => {
|
||||
it.each([
|
||||
'https://mail.mozdit.hu',
|
||||
'http://example.com/path',
|
||||
'//cdn.example.com/x.png',
|
||||
'HTTPS://PARTNER.EXAMPLE',
|
||||
])('should treat %s as external', (href) => {
|
||||
expect(isExternalHref(href, SITE)).toBe(true)
|
||||
})
|
||||
|
||||
it.each([
|
||||
'/hu/kapcsolat',
|
||||
'#top',
|
||||
'?q=1',
|
||||
'relative/path',
|
||||
'mailto:info@mozdit.hu',
|
||||
'tel:+3612345678',
|
||||
'https://stage.mozdit.hu/hu/rolunk',
|
||||
'',
|
||||
])('should treat %s as internal (same tab)', (href) => {
|
||||
expect(isExternalHref(href, SITE)).toBe(false)
|
||||
})
|
||||
|
||||
it('should not throw on malformed absolute URLs', () => {
|
||||
expect(isExternalHref('https://', SITE)).toBe(false)
|
||||
})
|
||||
|
||||
it('should fall back to treating every absolute http(s) URL as external without a valid site URL', () => {
|
||||
expect(isExternalHref('https://stage.mozdit.hu', 'not a url')).toBe(true)
|
||||
})
|
||||
})
|
||||
|
||||
describe('externalLinkProps', () => {
|
||||
it('should open external links in a new tab without leaking the opener', () => {
|
||||
expect(externalLinkProps('https://mail.mozdit.hu', SITE)).toEqual({
|
||||
target: '_blank',
|
||||
rel: 'noopener noreferrer',
|
||||
})
|
||||
})
|
||||
|
||||
it('should return no props for internal links', () => {
|
||||
expect(externalLinkProps('/en/contact', SITE)).toEqual({})
|
||||
})
|
||||
})
|
||||
|
||||
describe('openExternalLinksInNewTab', () => {
|
||||
it('should add target and rel to external anchors in CMS HTML', () => {
|
||||
expect(openExternalLinksInNewTab('Lásd <a href="https://naih.hu">NAIH</a>.', SITE)).toBe(
|
||||
'Lásd <a href="https://naih.hu" target="_blank" rel="noopener noreferrer">NAIH</a>.',
|
||||
)
|
||||
})
|
||||
|
||||
it('should replace an existing target/rel on external anchors', () => {
|
||||
expect(
|
||||
openExternalLinksInNewTab(`<a class="x" target='_self' rel="nofollow" href='https://naih.hu'>N</a>`, SITE),
|
||||
).toBe(`<a class="x" href='https://naih.hu' target="_blank" rel="noopener noreferrer">N</a>`)
|
||||
})
|
||||
|
||||
it('should leave internal anchors, mailto links and other tags untouched', () => {
|
||||
const html =
|
||||
'<p>Az <a href="/hu/adatvedelem" class="underline">adatkezelési tájékoztatót</a>, <a href="mailto:info@mozdit.hu">írjon</a>, <abbr title="x">GDPR</abbr>.</p>'
|
||||
expect(openExternalLinksInNewTab(html, SITE)).toBe(html)
|
||||
})
|
||||
|
||||
it('should handle several anchors in one string', () => {
|
||||
const out = openExternalLinksInNewTab('<a href="https://a.example">A</a> <a href="/hu">B</a> <a href="https://b.example">C</a>', SITE)
|
||||
expect(out.match(/target="_blank"/g)).toHaveLength(2)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,56 @@
|
||||
/**
|
||||
* MITHOME-124 — general rule: every external link opens in a new tab.
|
||||
*
|
||||
* "External" = an absolute http(s) or protocol-relative URL whose host is
|
||||
* not the site's own host. Internal paths, anchors, `mailto:` and `tel:`
|
||||
* stay in the same tab.
|
||||
*
|
||||
* DECISION: decided from the URL itself, never from a manual flag — a
|
||||
* per-link "external" switch would allow exceptions to the rule (the old
|
||||
* CTA checkbox and the webmail button drifted apart exactly that way).
|
||||
*/
|
||||
import { siteConfig } from '@/config/site'
|
||||
|
||||
const NEW_TAB_PROPS = { target: '_blank', rel: 'noopener noreferrer' } as const
|
||||
|
||||
type NewTabProps = typeof NEW_TAB_PROPS | Record<string, never>
|
||||
|
||||
function hostOf(url: string): string | undefined {
|
||||
try {
|
||||
return new URL(url).host.toLowerCase()
|
||||
} catch {
|
||||
return undefined
|
||||
}
|
||||
}
|
||||
|
||||
export function isExternalHref(href: string, siteUrl: string = siteConfig.general.url): boolean {
|
||||
const normalized = href.trim()
|
||||
if (!/^(https?:)?\/\//i.test(normalized)) return false
|
||||
const host = hostOf(normalized.startsWith('//') ? `https:${normalized}` : normalized)
|
||||
if (!host) return false
|
||||
return host !== hostOf(siteUrl)
|
||||
}
|
||||
|
||||
/** Spread onto an `<a>`: `<a href={href} {...externalLinkProps(href)}>`. */
|
||||
export function externalLinkProps(href: string, siteUrl?: string): NewTabProps {
|
||||
return isExternalHref(href, siteUrl) ? NEW_TAB_PROPS : {}
|
||||
}
|
||||
|
||||
const ANCHOR_OPEN_TAG = /<a\b([^>]*)>/gi
|
||||
const HREF_ATTR = /\bhref\s*=\s*(["'])(.*?)\1/i
|
||||
const TARGET_OR_REL_ATTR = /\s+(?:target|rel)\s*=\s*(["']).*?\1/gi
|
||||
|
||||
/**
|
||||
* Applies the rule to CMS-provided HTML (rendered with
|
||||
* dangerouslySetInnerHTML), where JSX props cannot be spread.
|
||||
* TRADEOFF: regex over a full HTML parser — the input is short, admin-
|
||||
* authored markup; an unparseable anchor is left as is (same tab).
|
||||
*/
|
||||
export function openExternalLinksInNewTab(html: string, siteUrl?: string): string {
|
||||
return html.replace(ANCHOR_OPEN_TAG, (tag: string, attrs: string) => {
|
||||
const href = HREF_ATTR.exec(attrs)?.[2]
|
||||
if (href === undefined || !isExternalHref(href, siteUrl)) return tag
|
||||
const cleaned = attrs.replace(TARGET_OR_REL_ATTR, '')
|
||||
return `<a${cleaned} target="${NEW_TAB_PROPS.target}" rel="${NEW_TAB_PROPS.rel}">`
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user