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('x')).toBe( 'x' ); expect(sanitize('link')).toBe( 'link' ); expect(sanitize('

t

')).toBe( '

t

' ); }); 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('d')).toBe( 'd' ); }); 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('p')).toBe( 'p' ); expect(sanitize('relax')).toBe( 'relax' ); }); it('keeps the image attributes the editor writes, including an empty alt', () => { expect( sanitize( '' ) ).toBe( '' ); expect(sanitize('s')).toBe( 's' ); }); 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 textm')).toBe( 'keep textm' ); expect(sanitize('text')).toBe('text'); expect(sanitize('
')).toBe('go'); expect(sanitize('')).toBe(''); expect(sanitize('x')).toBe('x'); }); it('does not lose text around a bare "<" that opens no tag', () => { expect(sanitize('

1 < 2 and 3 > 2

')).toBe('

1 < 2 and 3 > 2

'); }); it('removes comments and preserves formatting, lists and tables', () => { expect(sanitize('

a

b

')).toBe('

a

b

'); expect( sanitize('

Some bold, em, x,
next

') ).toBe('

Some bold, em, x,
next

'); expect(sanitize('')).toBe( '' ); expect( sanitize('
h
d
') ).toBe('
h
d
'); expect(sanitize('

t

')).toBe('

t

'); }); it('returns an empty string for non-string input', () => { expect(sanitize(undefined)).toBe(''); expect(sanitize(null)).toBe(''); expect(sanitize(42)).toBe(''); expect(sanitize('')).toBe(''); }); it('is idempotent, so write-time and render-time sanitization agree', () => { const once = sanitize('

Tom & J

'); expect(sanitize(once)).toBe(once); }); }); describe('sanitizeRawHtml (EditorJS rows)', () => { it('sanitizes raw blocks in place and leaves other blocks alone', () => { const rows = [ { id: 'r', size: 12, columns: [ { id: 'c', size: 12, data: { blocks: [ { type: 'raw', data: { html: '

ok

' } }, { type: 'paragraph', data: { text: '' } } ] } } ] } ]; sanitizeRawHtml(rows); expect(rows[0].columns[0].data.blocks[0].data.html).toBe('

ok

'); expect(rows[0].columns[0].data.blocks[1].data.text).toBe(''); }); it('tolerates malformed input', () => { expect(() => sanitizeRawHtml(undefined as any)).not.toThrow(); expect(() => sanitizeRawHtml([{ id: 'r', size: 12, columns: undefined as any }])).not.toThrow(); }); }); describe('stripTags (plain-text sinks such as blog comments)', () => { it('removes every tag and keeps the text', () => { expect(stripTags('hi there')).toBe('hi there'); }); it('drops script and style bodies entirely', () => { expect(stripTags('beforeafter')).toBe('beforeafter'); }); it('removes attribute-only payloads', () => { expect(stripTags('')).toBe(''); }); it('decodes entities so a text sink does not show them twice', () => { expect(stripTags('Tom & Jerry <3')).toBe('Tom & Jerry <3'); }); it('keeps a bare < that is not a tag', () => { expect(stripTags('1 < 2 and 3 > 2')).toBe('1 < 2 and 3 > 2'); }); it('tolerates null and undefined', () => { expect(stripTags(null)).toBe(''); expect(stripTags(undefined)).toBe(''); }); });