import {
sanitizeInlineHtml,
sanitizeRichTextHtml,
escapeHtml,
} from './sanitize';
import { describe, it, expect } from 'vitest';
describe('escapeHtml', () => {
it('escapes the five metacharacters', () => {
expect(escapeHtml(`a & b < c > d " e ' f`)).toBe(
'a & b < c > d " e ' f',
);
});
it('escapes ampersands first (no double-escape of entities)', () => {
expect(escapeHtml('<')).toBe('<');
});
it('returns empty string for empty input', () => {
expect(escapeHtml('')).toBe('');
});
it('leaves plain ASCII text untouched', () => {
expect(escapeHtml('Paris Adventure 2026')).toBe('Paris Adventure 2026');
});
it('neutralises a script tag without sanitising', () => {
expect(escapeHtml('')).toBe(
'<script>alert(1)</script>',
);
});
});
describe('sanitizeInlineHtml', () => {
it('returns empty string for empty input', () => {
expect(sanitizeInlineHtml('')).toBe('');
});
it('preserves the allowed inline tags', () => {
expect(sanitizeInlineHtml('a b c')).toBe(
'a b c',
);
expect(sanitizeInlineHtml('x')).toBe('x');
});
it('strips text');
expect(out).not.toContain(''),
).not.toContain('script');
});
it('does not preserve href / target on the inline tag set', () => {
// is not in the inline allow-list, so href can never appear here.
const out = sanitizeInlineHtml('x');
expect(out).toBe('x');
});
it('keeps user text content when the wrapping tag is stripped', () => {
expect(sanitizeInlineHtml('
hello world
'); expect(out).toContain('world'); expect(out).toContain('
before