import { describe, test, expect } from 'vitest'; import { purifyHtml } from './HtmlBlock'; describe('purifyHtml', () => { test('strips script tags', () => { expect(purifyHtml('

ok

')).not.toContain(' { const out = purifyHtml('x'); expect(out).not.toContain('onclick'); expect(out).toContain('href="/x"'); }); test('blocks javascript: URLs', () => { expect(purifyHtml('x')).not.toContain('javascript:'); }); test('allows YouTube iframe', () => { const out = purifyHtml(''); expect(out).toContain('youtube.com/embed/abc'); }); test('strips form/input', () => { expect(purifyHtml('
')).not.toContain(' { test('forces a restrictive sandbox attribute onto every iframe', () => { const out = purifyHtml(''); expect(out).toMatch(/]*\bsandbox="[^"]+"/); }); test('sandbox value omits allow-top-navigation (no top-level nav escape)', () => { const out = purifyHtml(''); const sandbox = out.match(/sandbox="([^"]*)"/)![1]; expect(sandbox).not.toMatch(/allow-top-navigation/); }); test('legitimate embeds (YouTube) still work and get sandboxed too', () => { const out = purifyHtml(''); expect(out).toContain('youtube.com/embed/abc'); expect(out).toMatch(/]*\bsandbox="[^"]+"/); }); test('adds referrerpolicy=no-referrer to iframes', () => { const out = purifyHtml(''); expect(out).toContain('referrerpolicy="no-referrer"'); }); test('script/on* attributes are still stripped alongside the sandboxed iframe', () => { const out = purifyHtml(''); expect(out).not.toContain('onload'); expect(out).not.toContain(' { purifyHtml(''); purifyHtml(''); const out = purifyHtml(''); const sandboxMatches = out.match(/sandbox="/g) || []; expect(sandboxMatches.length).toBe(1); }); test('a non-iframe element sanitized alongside an iframe is not touched by the hook', () => { const out = purifyHtml('

hi

'); expect(out).toContain('

hi

'); }); });