import { render, screen } from '@testing-library/react'; import parse from 'html-react-parser'; import { afterEach, describe, expect, it, vi } from 'vitest'; import * as customHtmlCss from '$styles/CustomHtml.css'; import { buildAbbrReplaceTextNode } from '$components/message/RenderBody'; import { sanitizeCustomHtml } from '$utils/sanitize'; import { LINKIFY_OPTS, factoryRenderLinkifyWithMention, getReactCustomHtmlParser, makeMentionCustomProps, renderMatrixMention, } from './react-custom-html-parser'; const settingsLinkBaseUrl = 'https://app.example'; const { CodeHighlightRenderer } = vi.hoisted(() => ({ CodeHighlightRenderer: vi.fn< (props: { code: string; language?: string; allowDetect?: boolean }) => JSX.Element >(({ code, language, allowDetect }) => ( {code} )), })); vi.mock('$components/code-highlight', () => ({ CodeHighlightRenderer, })); const createMatrixClient = (overrides: Record = {}) => ({ getUserId: () => '@alice:example.org', getRoom: () => undefined, mxcUrlToHttp: () => null, ...overrides, }) as never; function renderParsedHtml( html: string, options: { sanitize?: boolean; mx?: ReturnType; } = {} ) { const { sanitize = true, mx = createMatrixClient() } = options; const parserOptions = getReactCustomHtmlParser(mx, '!room:example.com', { settingsLinkBaseUrl, linkifyOpts: LINKIFY_OPTS, handleMentionClick: undefined, }); return render(
{parse(sanitize ? sanitizeCustomHtml(html) : html, parserOptions)}
); } const renderMessage = (html: string) => renderParsedHtml(html, { sanitize: false }); afterEach(() => { vi.clearAllMocks(); vi.restoreAllMocks(); }); describe('getReactCustomHtmlParser code blocks', () => { it('renders the Arborium renderer inside the existing code block shell for explicit data-lang metadata', () => { const { container } = renderMessage( `
\n  fn main() {\nlet value = 1;\nlet next = 2;\nlet third = 3;\nlet fourth = 4;\nlet fifth = 5;\nlet sixth = 6;\nlet seventh = 7;\nlet eighth = 8;\nlet ninth = 9;\nlet tenth = 10;\nlet eleventh = 11;\nlet twelfth = 12;\nlet thirteenth = 13;\nlet fourteenth = 14;\nlet fifteenth = 15;\n}\n
` ); expect(screen.getByText('rust')).toBeInTheDocument(); expect(screen.getByRole('button', { name: 'Copy' })).toBeInTheDocument(); expect(screen.getByRole('button', { name: 'Expand' })).toBeInTheDocument(); const arboriumCode = container.querySelector('[data-testid="arborium-code"]'); expect(arboriumCode).toBeInTheDocument(); expect(arboriumCode).toHaveTextContent('fn main()'); expect(arboriumCode).toHaveAttribute('data-language', 'rust'); expect(arboriumCode).toHaveAttribute('data-allow-detect', 'false'); expect(container.querySelector('#code-block-content')).toHaveClass( customHtmlCss.CodeBlockInternal ); expect(CodeHighlightRenderer).toHaveBeenCalledWith( expect.objectContaining({ code: expect.stringContaining('let fifteenth = 15;'), language: 'rust', allowDetect: false, }), expect.anything() ); }); it('preserves nested code children instead of routing them through Arborium', () => { const { container } = renderMessage(`
\n  alpha
beta
\n
`); expect(screen.getByText('Code')).toBeInTheDocument(); expect(screen.getByRole('button', { name: 'Copy' })).toBeInTheDocument(); expect(CodeHighlightRenderer).not.toHaveBeenCalled(); expect(container.querySelector('br')).toBeInTheDocument(); expect(container.querySelector('code')).toHaveTextContent('alphabeta'); }); it('uses data-lang on the pre element when the nested code element has no metadata', () => { renderMessage(`
fn main() {}
`); expect(screen.getByText('rust')).toBeInTheDocument(); expect(screen.getByRole('button', { name: 'Copy' })).toBeInTheDocument(); const shell = screen.getByTestId('arborium-code'); expect(shell).toHaveTextContent('fn main() {}'); expect(shell).toHaveAttribute('data-language', 'rust'); }); it('falls back to the language class when no explicit data-lang metadata is present', () => { renderMessage( `
const value = 1;\nconsole.log(value);
` ); expect(screen.getByText('ts')).toBeInTheDocument(); expect(screen.getByRole('button', { name: 'Copy' })).toBeInTheDocument(); const shell = screen.getByTestId('arborium-code'); expect(shell).toHaveTextContent('const value = 1;'); expect(shell).toHaveAttribute('data-language', 'ts'); }); }); describe('react custom html parser', () => { it('renders same-origin raw settings links as mention-style chips through the factory link render path', () => { const renderLink = factoryRenderLinkifyWithMention( settingsLinkBaseUrl, () => undefined, undefined ) as (ir: never) => JSX.Element; render(
{renderLink({ tagName: 'a', attributes: { href: 'https://app.example/settings/appearance?focus=message-link-preview&moe.sable.client.action=settings', }, content: 'https://app.example/settings/appearance?focus=message-link-preview&moe.sable.client.action=settings', } as never)}
); const link = screen.getByRole('link', { name: 'Appearance / Message Link Preview' }); expect(link).toHaveAttribute('data-settings-link-section', 'appearance'); expect(link).toHaveAttribute('data-settings-link-focus', 'message-link-preview'); expect(link.className).toContain(customHtmlCss.Mention({})); expect(link).not.toHaveTextContent('Settings >'); expect(link.className).toContain(customHtmlCss.MentionWithIcon); }); it('renders same-origin settings links as internal app links with settings metadata', () => { renderParsedHtml( 'Appearance', { sanitize: false } ); const link = screen.getByRole('link', { name: 'Appearance / Message Link Preview' }); expect(link).toHaveAttribute( 'href', 'https://app.example/settings/appearance?focus=message-link-preview&moe.sable.client.action=settings' ); expect(link).toHaveAttribute('data-settings-link-section', 'appearance'); expect(link).toHaveAttribute('data-settings-link-focus', 'message-link-preview'); expect(link).not.toHaveAttribute('data-mention-id'); expect(link.className).toContain(customHtmlCss.Mention({})); expect(link.className).toContain(customHtmlCss.MentionWithIcon); }); it('renders marked cross-instance settings links as internal app links with settings metadata', () => { renderParsedHtml( 'Account', { sanitize: false } ); const link = screen.getByRole('link', { name: 'Account / Status' }); expect(link).toHaveAttribute( 'href', 'https://other.example/#/client/settings/account?focus=status&moe.sable.client.action=settings' ); expect(link).toHaveAttribute('data-settings-link-section', 'account'); expect(link).toHaveAttribute('data-settings-link-focus', 'status'); }); it('keeps malformed settings-looking linkified tokens as normal links', () => { const renderLink = factoryRenderLinkifyWithMention( settingsLinkBaseUrl, () => undefined, undefined ) as (ir: never) => JSX.Element; const malformedToken = 'https://app.example/settings/account?focus=status&moe.sable.client.action=settings">Settings'; render(
{renderLink({ tagName: 'a', attributes: { href: malformedToken, }, content: malformedToken, } as never)}
); const link = screen.getByRole('link', { name: malformedToken }); expect(link).not.toHaveAttribute('data-settings-link-section'); expect(link).not.toHaveAttribute('data-settings-link-focus'); expect(link.className).not.toContain(customHtmlCss.MentionWithIcon); }); it('keeps settings links with unknown focus ids as normal links', () => { renderParsedHtml( 'Settings > Account > Display Name2', { sanitize: false } ); const link = screen.getByRole('link', { name: 'Settings > Account > Display Name2' }); expect(link).toHaveAttribute( 'href', 'https://app.example/settings/account?focus=display-name2' ); expect(link).not.toHaveAttribute('data-settings-link-section'); expect(link).not.toHaveAttribute('data-settings-link-focus'); expect(link.className).not.toContain(customHtmlCss.MentionWithIcon); }); it('renders matrix message permalinks with an icon instead of the Message prefix', () => { render(
{renderMatrixMention( createMatrixClient({ getRoom: () => ({ roomId: '!room:example.org', name: 'Lobby' }), }), undefined, 'https://matrix.to/#/!room:example.org/$event123', makeMentionCustomProps(undefined) )}
); const link = screen.getByRole('link', { name: '#Lobby' }); expect(link).toHaveAttribute('data-mention-id', '!room:example.org'); expect(link).toHaveAttribute('data-mention-event-id', '$event123'); expect(link.className).toContain(customHtmlCss.Mention({})); expect(link.className).toContain(customHtmlCss.MentionWithIcon); expect(link).not.toHaveTextContent('Message:'); expect(link.querySelector('[aria-hidden="true"]')).not.toBeNull(); }); it('translates Matrix color data attributes into rendered styles', () => { renderParsedHtml('colored'); expect(screen.getByText('colored')).toHaveStyle({ color: 'rgb(255, 0, 0)', backgroundColor: 'rgb(0, 255, 0)', }); }); it('drops incoming style attributes even if unsanitized html reaches the parser', () => { renderParsedHtml( 'styled', { sanitize: false } ); const styled = screen.getByText('styled'); expect(styled).toHaveStyle({ color: 'rgb(255, 0, 0)', }); expect(styled).not.toHaveStyle({ backgroundColor: 'rgb(0, 255, 0)', }); }); it('renders a readable fallback for unresolved legacy emote MXC images', () => { const { container } = renderParsedHtml( 'blobcat', { sanitize: false } ); expect(screen.getByText(':blobcat:')).toBeInTheDocument(); expect(container.querySelector('img[src^="mxc://"]')).toBeNull(); }); it('renders a readable fallback for unresolved non-emote MXC images', () => { const { container } = renderParsedHtml( 'media', { sanitize: false } ); expect(screen.getByText('media')).toBeInTheDocument(); expect(container.querySelector('img[src^="mxc://"]')).toBeNull(); }); it('renders unresolved MXC fallbacks without emitting debug logs', () => { const logSpy = vi.spyOn(console, 'warn').mockImplementation(vi.fn<() => void>()); renderParsedHtml('media', { sanitize: false, }); expect(logSpy).not.toHaveBeenCalled(); }); it('linkifies bare urls in formatted html text nodes even when abbreviation replacement runs', () => { const parserOptions = getReactCustomHtmlParser(createMatrixClient(), '!room:example.com', { settingsLinkBaseUrl, linkifyOpts: LINKIFY_OPTS, handleMentionClick: undefined, replaceTextNode: buildAbbrReplaceTextNode(new Map([['PR', 'Pull request']]), LINKIFY_OPTS), }); render(
{parse( '

figured out the section could be removed; set up a PR for it: https://github.com/SableClient/Sable/pull/626

', parserOptions )}
); expect(screen.getByText('PR')).toBeInTheDocument(); expect( screen.getByRole('link', { name: 'https://github.com/SableClient/Sable/pull/626', }) ).toHaveAttribute('href', 'https://github.com/SableClient/Sable/pull/626'); }); it('keeps the full link intact when an abbreviation term appears inside the url token', () => { const parserOptions = getReactCustomHtmlParser(createMatrixClient(), '!room:example.com', { settingsLinkBaseUrl, linkifyOpts: LINKIFY_OPTS, handleMentionClick: undefined, replaceTextNode: buildAbbrReplaceTextNode(new Map([['PR', 'Pull request']]), LINKIFY_OPTS), }); render(
{parse( '

see https://github.com/SableClient/Sable/pull/PR/626 for context

', parserOptions )}
); expect( screen.getByRole('link', { name: 'https://github.com/SableClient/Sable/pull/PR/626', }) ).toHaveAttribute('href', 'https://github.com/SableClient/Sable/pull/PR/626'); }); });