import { parseMarkup, MAX_NESTING_DEPTH, MAX_INPUT_LENGTH, MAX_ACCESSIBILITY_LABEL_LENGTH, } from './parseMarkup'; describe('parseMarkup', () => { it('returns a single text node for plain text', () => { expect(parseMarkup('hello world')).toEqual([{ type: 'text', content: 'hello world' }]); }); it('returns empty array for empty string', () => { expect(parseMarkup('')).toEqual([]); }); it('splits text on newlines', () => { expect(parseMarkup('line one\nline two')).toEqual([ { type: 'text', content: 'line one' }, { type: 'newline' }, { type: 'text', content: 'line two' }, ]); }); it('preserves literal backslash-n as text (not treated as newline)', () => { expect(parseMarkup('line one\\nline two')).toEqual([ { type: 'text', content: 'line one\\nline two' }, ]); }); describe('link tags', () => { it('parses with href attribute (double quotes)', () => { expect(parseMarkup('go to settings page')).toEqual([ { type: 'text', content: 'go to ' }, { type: 'link', href: '/settings', children: [{ type: 'text', content: 'settings' }] }, { type: 'text', content: ' page' }, ]); }); it('parses with href attribute (single quotes)', () => { expect(parseMarkup("click")).toEqual([ { type: 'link', href: '/path', children: [{ type: 'text', content: 'click' }] }, ]); }); it('parses with action attribute', () => { expect(parseMarkup('Save now')).toEqual([ { type: 'link', action: 'save', children: [{ type: 'text', content: 'Save now' }], }, ]); }); it('parses with href and target attributes', () => { expect(parseMarkup('visit')).toEqual([ { type: 'link', href: 'https://example.com', target: '_blank', children: [{ type: 'text', content: 'visit' }], }, ]); }); it('parses as an alias for link', () => { expect(parseMarkup('click')).toEqual([ { type: 'link', href: '/page', children: [{ type: 'text', content: 'click' }] }, ]); }); }); describe('nesting', () => { it('supports emphasis inside link', () => { expect(parseMarkup('bold link')).toEqual([ { type: 'link', href: '/x', children: [ { type: 'important', children: [{ type: 'text', content: 'bold link' }], }, ], }, ]); }); it('supports link inside emphasis', () => { expect( parseMarkup('Read the terms carefully'), ).toEqual([ { type: 'important', children: [ { type: 'text', content: 'Read the ' }, { type: 'link', href: '/terms', children: [{ type: 'text', content: 'terms' }], }, { type: 'text', content: ' carefully' }, ], }, ]); }); it('supports multi-level nesting', () => { expect( parseMarkup('deep'), ).toEqual([ { type: 'positive', children: [ { type: 'important', children: [ { type: 'link', href: '/x', children: [{ type: 'text', content: 'deep' }], }, ], }, ], }, ]); }); }); describe('malformed input', () => { it('treats unclosed tags as literal text', () => { expect(parseMarkup('unclosed')).toEqual([ { type: 'text', content: '' }, { type: 'text', content: 'unclosed' }, ]); }); it('treats unrecognized tags as literal text', () => { expect(parseMarkup('')).toEqual([ { type: 'text', content: '' }, ]); }); it('treats mismatched close tag as literal text', () => { expect(parseMarkup('text')).toEqual([ { type: 'text', content: '' }, { type: 'text', content: 'text' }, { type: 'text', content: '' }, ]); }); it('ignores unknown attributes on link tags', () => { expect(parseMarkup('text')).toEqual([ { type: 'link', href: '/x', children: [{ type: 'text', content: 'text' }] }, ]); }); it('does not match tags that are prefixes of longer words', () => { expect(parseMarkup('text')).toEqual([ { type: 'text', content: 'text' }, ]); }); it('does not match as ', () => { expect(parseMarkup('text')).toEqual([ { type: 'text', content: 'text' }, ]); }); }); describe('emphasis tags', () => { it('parses tag', () => { expect(parseMarkup('this is critical info')).toEqual([ { type: 'text', content: 'this is ' }, { type: 'important', children: [{ type: 'text', content: 'critical' }] }, { type: 'text', content: ' info' }, ]); }); it('parses all emphasis tag variants', () => { const result = parseMarkup('good bad'); expect(result).toEqual([ { type: 'positive', children: [{ type: 'text', content: 'good' }] }, { type: 'text', content: ' ' }, { type: 'negative', children: [{ type: 'text', content: 'bad' }] }, ]); }); }); describe('strong and strikethrough tags', () => { it('parses tag', () => { expect(parseMarkup('this is bold text')).toEqual([ { type: 'text', content: 'this is ' }, { type: 'strong', children: [{ type: 'text', content: 'bold' }] }, { type: 'text', content: ' text' }, ]); }); it('parses tag', () => { expect(parseMarkup('this is removed text')).toEqual([ { type: 'text', content: 'this is ' }, { type: 'strikethrough', children: [{ type: 'text', content: 'removed' }] }, { type: 'text', content: ' text' }, ]); }); }); describe('paragraph tags', () => { it('parses tag', () => { expect(parseMarkup('A full paragraph.')).toEqual([ { type: 'paragraph', children: [{ type: 'text', content: 'A full paragraph.' }] }, ]); }); it('parses

as an alias for paragraph', () => { expect(parseMarkup('

Short form.

')).toEqual([ { type: 'paragraph', children: [{ type: 'text', content: 'Short form.' }] }, ]); }); }); describe('accessibilityLabel', () => { it('extracts accessibilityLabel from link tags', () => { expect( parseMarkup('settings'), ).toEqual([ { type: 'link', href: '/settings', accessibilityLabel: 'Go to settings', children: [{ type: 'text', content: 'settings' }], }, ]); }); it('extracts accessibilityLabel from emphasis tags', () => { expect(parseMarkup('critical')).toEqual([ { type: 'important', accessibilityLabel: 'Notice', children: [{ type: 'text', content: 'critical' }], }, ]); }); it('extracts accessibilityLabel from strong tags', () => { expect(parseMarkup('bold')).toEqual([ { type: 'strong', accessibilityLabel: 'Key point', children: [{ type: 'text', content: 'bold' }], }, ]); }); it('extracts accessibilityLabel from strikethrough tags', () => { expect( parseMarkup('£50'), ).toEqual([ { type: 'strikethrough', accessibilityLabel: 'Was 50 GBP', children: [{ type: 'text', content: '£50' }], }, ]); }); it('does not extract accessibilityLabel from paragraph tags', () => { expect(parseMarkup('text')).toEqual([ { type: 'paragraph', children: [{ type: 'text', content: 'text' }] }, ]); }); it('does not include accessibilityLabel key when attribute is absent', () => { const result = parseMarkup('text'); expect(result[0]).not.toHaveProperty('accessibilityLabel'); }); }); describe('stack overflow via deep nesting (DoS)', () => { it('handles deeply nested tags without crashing', () => { const depth = 10000; const input = `${''.repeat(depth)}x${''.repeat(depth)}`; expect(() => parseMarkup(input)).not.toThrow(); }); it('degrades gracefully at excessive depth', () => { const depth = 10000; const input = `${''.repeat(depth)}x${''.repeat(depth)}`; const result = parseMarkup(input); expect(result.length).toBeGreaterThan(0); }); it('respects MAX_NESTING_DEPTH', () => { const depth = MAX_NESTING_DEPTH + 20; const input = `${''.repeat(depth)}x${''.repeat(depth)}`; const result = parseMarkup(input); const deepNode = JSON.stringify(result); let nesting = 0; let maxNesting = 0; for (const char of deepNode) { if (char === '{') nesting += 1; if (char === '}') nesting -= 1; maxNesting = Math.max(maxNesting, nesting); } expect(maxNesting).toBeLessThan(MAX_NESTING_DEPTH * 3); }); }); describe('prototype pollution via action attribute', () => { it('does not extract __proto__ as action value', () => { const result = parseMarkup('click'); const linkNode = result[0]; expect(linkNode).toHaveProperty('type', 'link'); expect(linkNode).not.toHaveProperty('action'); }); it('does not extract constructor as action value', () => { const result = parseMarkup('click'); const linkNode = result[0]; expect(linkNode).toHaveProperty('type', 'link'); expect(linkNode).not.toHaveProperty('action'); }); it('does not extract toString as action value', () => { const result = parseMarkup('click'); const linkNode = result[0]; expect(linkNode).toHaveProperty('type', 'link'); expect(linkNode).not.toHaveProperty('action'); }); it('does not extract valueOf as action value', () => { const result = parseMarkup('click'); const linkNode = result[0]; expect(linkNode).toHaveProperty('type', 'link'); expect(linkNode).not.toHaveProperty('action'); }); it('does not extract hasOwnProperty as action value', () => { const result = parseMarkup('click'); const linkNode = result[0]; expect(linkNode).toHaveProperty('type', 'link'); expect(linkNode).not.toHaveProperty('action'); }); it('allows legitimate action values', () => { const result = parseMarkup('click'); const linkNode = result[0] as { type: string; action?: string }; expect(linkNode.action).toBe('save'); }); }); describe('duplicate attribute injection', () => { it('uses first href value when duplicated', () => { const result = parseMarkup('click'); const linkNode = result[0] as { type: string; href?: string }; expect(linkNode.href).toBe('/safe'); }); it('uses first action value when duplicated', () => { const result = parseMarkup('click'); const linkNode = result[0] as { type: string; action?: string }; expect(linkNode.action).toBe('safe'); }); it('uses first target value when duplicated', () => { const result = parseMarkup('click'); const linkNode = result[0] as { type: string; target?: string }; expect(linkNode.target).toBe('_blank'); }); }); describe('HTML/script in accessibilityLabel', () => { it('strips HTML from accessibilityLabel', () => { const result = parseMarkup( 'text', ); const linkNode = result[0] as { type: string; accessibilityLabel?: string }; expect(linkNode.accessibilityLabel).not.toContain('<'); expect(linkNode.accessibilityLabel).not.toContain('>'); }); it('strips angle brackets from accessibilityLabel (defense-in-depth)', () => { const result = parseMarkup( 'text', ); const node = result[0] as { type: string; accessibilityLabel?: string }; expect(node.accessibilityLabel).toBe('testbbold/bend'); expect(node.accessibilityLabel).not.toContain('<'); expect(node.accessibilityLabel).not.toContain('>'); }); it('strips quotes from accessibilityLabel to prevent attribute injection', () => { const result = parseMarkup( 'text', ); const node = result[0] as { type: string; accessibilityLabel?: string }; if (node.accessibilityLabel) { expect(node.accessibilityLabel).not.toContain('"'); } }); it('preserves safe accessibilityLabel text', () => { const result = parseMarkup( 'text', ); const node = result[0] as { type: string; accessibilityLabel?: string }; expect(node.accessibilityLabel).toBe('Navigate to settings page'); }); }); describe('unvalidated target attribute', () => { it('allows target="_blank"', () => { const result = parseMarkup('text'); const linkNode = result[0] as { type: string; target?: string }; expect(linkNode.target).toBe('_blank'); }); it('allows target="_self"', () => { const result = parseMarkup('text'); const linkNode = result[0] as { type: string; target?: string }; expect(linkNode.target).toBe('_self'); }); it('blocks target="_top" (iframe breakout)', () => { const result = parseMarkup('text'); const linkNode = result[0] as { type: string; target?: string }; expect(linkNode).not.toHaveProperty('target'); }); it('blocks target="_parent" (iframe breakout)', () => { const result = parseMarkup('text'); const linkNode = result[0] as { type: string; target?: string }; expect(linkNode).not.toHaveProperty('target'); }); it('blocks arbitrary target values', () => { const result = parseMarkup('text'); const linkNode = result[0] as { type: string; target?: string }; expect(linkNode).not.toHaveProperty('target'); }); }); describe('slash as attribute separator bypass', () => { it('does not parse attributes after / separator', () => { const result = parseMarkup('click'); const linkNode = result[0] as { type: string; href?: string }; expect(linkNode).not.toHaveProperty('href'); }); }); describe('control characters in attribute values', () => { it('strips control characters from href values', () => { const result = parseMarkup('text'); const linkNode = result[0] as { type: string; href?: string }; if (linkNode.href) { expect(linkNode.href).not.toContain('\x00'); } }); it('strips newlines from href values', () => { const result = parseMarkup('text'); const linkNode = result[0] as { type: string; href?: string }; if (linkNode.href) { expect(linkNode.href).not.toContain('\n'); } }); it('strips tabs from href values', () => { const result = parseMarkup('text'); const linkNode = result[0] as { type: string; href?: string }; if (linkNode.href) { expect(linkNode.href).not.toContain('\t'); } }); }); describe('mismatched nesting (graceful degradation)', () => { it('handles inner tag closed by outer close tag', () => { const result = parseMarkup('text'); expect(result).toBeDefined(); expect(result.length).toBeGreaterThan(0); }); it('handles interleaved open/close tags', () => { const result = parseMarkup('textmore'); expect(result).toBeDefined(); expect(result.length).toBeGreaterThan(0); }); }); describe('tag-like content inside attribute values', () => { it('captures full href even when it contains > inside quotes', () => { const result = parseMarkup('remaining text'); // TAG_REGEX sees through > inside quotes — full href is captured // sanitiseHref (at render time) blocks it because it contains < expect(result).toEqual([ { type: 'link', href: '', children: [{ type: 'text', content: 'remaining text' }], }, ]); }); }); describe('empty tags and self-closing patterns', () => { it('parses empty element', () => { expect(parseMarkup('')).toEqual([{ type: 'important', children: [] }]); }); it('parses empty link', () => { expect(parseMarkup('')).toEqual([ { type: 'link', href: '/x', children: [] }, ]); }); it('handles self-closing slash as unclosed tag', () => { // matches TAG_REGEX as open tag with attrs="/", lacks close → text fallback const result = parseMarkup(''); expect(result).toEqual([{ type: 'text', content: '' }]); }); }); describe('SVG and IMG injection (regression — must remain as plain text)', () => { it('treats with onerror as plain text', () => { expect(parseMarkup('')).toEqual([ { type: 'text', content: '' }, ]); }); it('treats with onerror as plain text', () => { expect(parseMarkup('')).toEqual([ { type: 'text', content: '' }, ]); }); it('treats with onload as plain text', () => { expect(parseMarkup('')).toEqual([ { type: 'text', content: '' }, ]); }); it('treats with onload as plain text', () => { expect(parseMarkup('')).toEqual([ { type: 'text', content: '' }, ]); }); it('treats as plain text', () => { expect(parseMarkup('')).toEqual([ { type: 'text', content: '' }, ]); }); it('treats inside allowed element as text child', () => { const result = parseMarkup(''); expect(result).toEqual([ { type: 'important', children: [{ type: 'text', content: '' }], }, ]); }); it('treats inside allowed element as text child', () => { const result = parseMarkup(''); expect(result).toEqual([ { type: 'important', children: [{ type: 'text', content: '' }], }, ]); }); it('treats inside link as text child', () => { const result = parseMarkup(''); expect(result).toEqual([ { type: 'link', href: '/safe', children: [{ type: 'text', content: '' }], }, ]); }); it('treats between text as plain text', () => { expect(parseMarkup('text more text')).toEqual([ { type: 'text', content: 'text more text' }, ]); }); it('treats between text as plain text', () => { expect(parseMarkup('before after')).toEqual([ { type: 'text', content: 'before after' }, ]); }); it('treats as plain text', () => { expect(parseMarkup('')).toEqual([ { type: 'text', content: '' }, ]); }); it('treats as plain text', () => { expect(parseMarkup('')).toEqual([ { type: 'text', content: '' }, ]); }); it('treats with event handlers as plain text', () => { expect(parseMarkup('')).toEqual([ { type: 'text', content: '' }, ]); }); it('treats
as plain text', () => { expect(parseMarkup('
')).toEqual([ { type: 'text', content: '
' }, ]); }); }); describe('SVG/IMG in attribute values', () => { it('extracts href containing img tag (blocked by sanitiseHref at render)', () => { const result = parseMarkup('text'); // TAG_REGEX now captures > inside quotes — full href is extracted // sanitiseHref blocks it at render time (contains < and >) expect(result).toEqual([ { type: 'link', href: '', children: [{ type: 'text', content: 'text' }], }, ]); }); it('strips HTML metacharacters from accessibilityLabel containing svg', () => { const result = parseMarkup( 'text', ); // TAG_REGEX captures the full value; sanitizeAttributeValue strips < > " ' const node = result[0] as { type: string; accessibilityLabel?: string }; expect(node.accessibilityLabel).toBe('svg onload=alert(1)'); expect(node.accessibilityLabel).not.toContain('<'); expect(node.accessibilityLabel).not.toContain('>'); }); }); describe('input length limit (DoS prevention)', () => { it('returns input as plain text when exceeding MAX_INPUT_LENGTH', () => { const input = 'x'.repeat(MAX_INPUT_LENGTH + 1); expect(parseMarkup(input)).toEqual([{ type: 'text', content: input }]); }); it('parses normally at exactly MAX_INPUT_LENGTH', () => { const input = `${'x'.repeat(MAX_INPUT_LENGTH - 24)}`; const result = parseMarkup(input); expect(result[0]).toHaveProperty('type', 'important'); }); it('does not parse markup tags in oversized input', () => { const input = `text${'x'.repeat(MAX_INPUT_LENGTH)}`; const result = parseMarkup(input); expect(result).toEqual([{ type: 'text', content: input }]); }); }); describe('accessibilityLabel length limit', () => { it('truncates accessibilityLabel exceeding MAX_ACCESSIBILITY_LABEL_LENGTH', () => { const longLabel = 'a'.repeat(MAX_ACCESSIBILITY_LABEL_LENGTH + 100); const result = parseMarkup(`text`); const node = result[0] as { type: string; accessibilityLabel?: string }; expect(node.accessibilityLabel).toHaveLength(MAX_ACCESSIBILITY_LABEL_LENGTH); }); it('preserves label at exactly MAX_ACCESSIBILITY_LABEL_LENGTH', () => { const exactLabel = 'b'.repeat(MAX_ACCESSIBILITY_LABEL_LENGTH); const result = parseMarkup(`text`); const node = result[0] as { type: string; accessibilityLabel?: string }; expect(node.accessibilityLabel).toBe(exactLabel); }); it('preserves short labels unchanged', () => { const result = parseMarkup( 'text', ); const node = result[0] as { type: string; accessibilityLabel?: string }; expect(node.accessibilityLabel).toBe('Navigate to settings'); }); }); });