import { render, screen } from '@testing-library/react';
import parse from 'html-react-parser';
import { afterEach, describe, expect, it, vi } from 'vitest';
import * as css from '$styles/CustomHtml.css';
import { sanitizeCustomHtml } from '$utils/sanitize';
import { getReactCustomHtmlParser, LINKIFY_OPTS } from './react-custom-html-parser';
const { CodeHighlightRenderer } = vi.hoisted(() => ({
CodeHighlightRenderer: vi.fn(
({
code,
language,
allowDetect,
}: {
code: string;
language?: string;
allowDetect?: boolean;
}) => (
{code}
)
),
}));
vi.mock('$components/code-highlight', () => ({
CodeHighlightRenderer,
}));
function createMatrixClient() {
return {
getRoom: () => undefined,
getUserId: () => '@me:example.com',
mxcUrlToHttp: () => null,
} as any;
}
function renderParsedHtml(
html: string,
options: {
sanitize?: boolean;
mx?: any;
} = {}
) {
const { sanitize = true, mx = createMatrixClient() } = options;
const parserOptions = getReactCustomHtmlParser(mx, '!room:example.com', {
linkifyOpts: LINKIFY_OPTS,
});
return render(
\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(css.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('getReactCustomHtmlParser', () => {
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(
'