import { describe, expect, it } from 'vitest' import { sanitizeLinkHref } from '../mark/sanitize-href' describe('sanitizeLinkHref', () => { it('keeps safe absolute URLs', () => { for (const href of [ 'https://example.com/path?q=1#frag', 'http://example.com', 'mailto:hi@example.com', 'tel:+123456789', 'ftp://files.example.com/a', ]) { expect(sanitizeLinkHref(href)).toBe(href) } }) it('keeps schemeless relative URLs, queries and fragments', () => { for (const href of [ '/absolute/path', './relative', '../up', 'foo/bar', '#anchor', '?query=1', '//protocol-relative.example.com', ]) { expect(sanitizeLinkHref(href)).toBe(href) } }) it('strips dangerous schemes', () => { for (const href of [ 'javascript:alert(1)', 'JavaScript:alert(1)', 'data:text/html,', 'vbscript:msgbox(1)', ' javascript:alert(1)', ]) { expect(sanitizeLinkHref(href)).toBe('') } }) it('strips schemes obfuscated with control characters and whitespace', () => { for (const href of [ 'java\tscript:alert(1)', 'java\nscript:alert(1)', 'java\r\nscript:alert(1)', 'javascript:alert(1)', 'javascript:alert(1)', ]) { expect(sanitizeLinkHref(href)).toBe('') } }) it('returns empty string for non-string or empty input', () => { expect(sanitizeLinkHref(undefined)).toBe('') expect(sanitizeLinkHref(null)).toBe('') expect(sanitizeLinkHref(123)).toBe('') expect(sanitizeLinkHref('')).toBe('') expect(sanitizeLinkHref(' ')).toBe('') }) })