import { describe, expect, it } from '@jest/globals';
import { sanitize, sanitizeRawHtml, stripTags } from '../../sanitizeHtml.js';
describe('sanitize (rich-text allow-list)', () => {
it('drops script tags and their bodies but keeps surrounding text', () => {
expect(sanitize('
Hi
there
')).toBe(
'Hi
there
'
);
expect(sanitize('ab')).toBe('ab');
expect(sanitize('after
')).toBe(
'after
'
);
});
it('removes event-handler attributes', () => {
expect(sanitize('
')).toBe(
'
'
);
expect(sanitize('link')).toBe(
'link'
);
expect(sanitize('')).toBe(
''
);
});
it('removes href/src with a disallowed scheme, including obfuscated ones', () => {
for (const bad of [
'javascript:alert(1)',
'JaVaScRiPt:alert(1)',
'javascript:alert(1)',
'java\tscript:alert(1)',
' javascript:alert(1)',
'vbscript:msgbox(1)',
'data:text/html;base64,AAAA'
]) {
expect(sanitize(`bad`)).toBe('bad');
}
expect(sanitize('
')).toBe(
'
'
);
});
it('keeps http, https, mailto, tel, relative and protocol-relative URLs', () => {
expect(
sanitize('ok')
).toBe('ok');
expect(sanitize('m t')).toBe(
'm t'
);
expect(sanitize('
')).toBe(
'
'
);
expect(sanitize('relax')).toBe(
'relax'
);
});
it('keeps the image attributes the editor writes, including an empty alt', () => {
expect(
sanitize(
'
'
)
).toBe(
'
'
);
expect(sanitize('
')).toBe(
'
'
);
});
it('keeps class/id/style on any tag, but drops styles that smuggle scripts', () => {
expect(sanitize('s
')).toBe(
's
'
);
expect(sanitize('s
')).toBe(
's
'
);
expect(sanitize('s
')).toBe('s
');
});
it('strips unknown elements but keeps their text', () => {
expect(sanitize('keep text')).toBe(
'keep textm'
);
expect(sanitize('text')).toBe('text');
expect(sanitize('')).toBe('go');
expect(sanitize('