A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
import { describe, it, expect } from 'vitest';
import { parseRichText, type RichTextRun } from '../src/ui/text/rich-text-parser';
const IMG_DEFAULTS = { offsetX: 0, offsetY: 0, scale: 1, tint: null };
describe('parseRichText – <img> tag', () => {
it('parses basic <img> with all attributes', () => {
const runs = parseRichText('<img src="heart" width=24 height=32/>');
expect(runs).toEqual([
{ type: 'image', src: 'heart', width: 24, height: 32, valign: 'baseline', ...IMG_DEFAULTS },
]);
});
it('parses <img> with quoted attribute values', () => {
const runs = parseRichText('<img src="coin" width="16" height="16"/>');
expect(runs).toEqual([
{ type: 'image', src: 'coin', width: 16, height: 16, valign: 'baseline', ...IMG_DEFAULTS },
]);
});
it('defaults width/height to 0 when omitted', () => {
const runs = parseRichText('<img src="star"/>');
expect(runs).toEqual([
{ type: 'image', src: 'star', width: 0, height: 0, valign: 'baseline', ...IMG_DEFAULTS },
]);
});
it('parses valign attribute', () => {
const runs = parseRichText('<img src="icon" width=20 height=20 valign=middle/>');
expect(runs).toEqual([
{ type: 'image', src: 'icon', width: 20, height: 20, valign: 'middle', ...IMG_DEFAULTS },
]);
});
it('parses offsetX, offsetY, scale attributes', () => {
const runs = parseRichText('<img src="star" width=16 height=16 offsetX=2 offsetY=-3 scale=1.5/>');
expect(runs).toHaveLength(1);
const img = runs[0];
expect(img).toMatchObject({ type: 'image', src: 'star', offsetX: 2, offsetY: -3, scale: 1.5 });
});
it('parses tint attribute as hex color', () => {
const runs = parseRichText('<img src="heart" width=16 height=16 tint=#FF000080/>');
expect(runs).toHaveLength(1);
const img = runs[0] as any;
expect(img.tint).toEqual({ r: 1, g: 0, b: 0, a: expect.closeTo(0.502, 1) });
});
it('defaults scale=1, offset=0, tint=null when omitted', () => {
const runs = parseRichText('<img src="x" width=10 height=10/>');
const img = runs[0] as any;
expect(img.scale).toBe(1);
expect(img.offsetX).toBe(0);
expect(img.offsetY).toBe(0);
expect(img.tint).toBeNull();
});
it('ignores surrounding text styles on <img>', () => {
const runs = parseRichText('<b>HP<img src="heart" width=16 height=16/>100</b>');
expect(runs).toHaveLength(3);
expect(runs[0]).toEqual({ type: 'text', text: 'HP', bold: true, italic: false, color: null, fontSize: null });
expect(runs[1]).toMatchObject({ type: 'image', src: 'heart', width: 16, height: 16, valign: 'baseline' });
expect(runs[2]).toEqual({ type: 'text', text: '100', bold: true, italic: false, color: null, fontSize: null });
});
it('handles consecutive <img> tags', () => {
const runs = parseRichText('<img src="a" width=10 height=10/><img src="b" width=20 height=20/>');
expect(runs).toHaveLength(2);
expect(runs[0]).toMatchObject({ type: 'image', src: 'a' });
expect(runs[1]).toMatchObject({ type: 'image', src: 'b' });
});
it('handles mixed text and images', () => {
const runs = parseRichText('Score: <img src="coin" width=16 height=16/>999');
expect(runs).toHaveLength(3);
expect(runs[0]).toEqual({ type: 'text', text: 'Score: ', bold: false, italic: false, color: null, fontSize: null });
expect(runs[1]).toMatchObject({ type: 'image', src: 'coin' });
expect(runs[2]).toEqual({ type: 'text', text: '999', bold: false, italic: false, color: null, fontSize: null });
});
it('treats <img> without src as literal text', () => {
const runs = parseRichText('<img width=10 height=10/>');
expect(runs).toHaveLength(1);
expect(runs[0]).toMatchObject({ type: 'text' });
});
it('existing text runs have type "text"', () => {
const runs = parseRichText('<b>bold</b> normal');
for (const run of runs) {
expect(run.type).toBe('text');
}
});
it('handles <img> inside <color>', () => {
const runs = parseRichText('<color=#FF0000>red<img src="x" width=8 height=8/>text</color>');
expect(runs).toHaveLength(3);
expect(runs[0]).toMatchObject({ type: 'text', text: 'red', color: { r: 1, g: 0, b: 0, a: 1 } });
expect(runs[1]).toMatchObject({ type: 'image', src: 'x' });
expect(runs[2]).toMatchObject({ type: 'text', text: 'text', color: { r: 1, g: 0, b: 0, a: 1 } });
});
});
describe('parseRichText – <font> tag', () => {
it('parses <font size=24> tag', () => {
const runs = parseRichText('<font size=24>big</font> normal');
expect(runs).toHaveLength(2);
expect(runs[0]).toMatchObject({ type: 'text', text: 'big', fontSize: 24 });
expect(runs[1]).toMatchObject({ type: 'text', text: ' normal', fontSize: null });
});
it('supports quoted size value', () => {
const runs = parseRichText('<font size="16">small</font>');
expect(runs).toHaveLength(1);
expect(runs[0]).toMatchObject({ type: 'text', text: 'small', fontSize: 16 });
});
it('nests inside color', () => {
const runs = parseRichText('<color=#FF0000><font size=20>red big</font></color>');
expect(runs).toHaveLength(1);
expect(runs[0]).toMatchObject({
type: 'text', text: 'red big', fontSize: 20,
color: { r: 1, g: 0, b: 0, a: 1 },
});
});
it('ignores invalid size', () => {
const runs = parseRichText('<font size=abc>text</font>');
expect(runs.length).toBeGreaterThanOrEqual(1);
const textRuns = runs.filter(r => r.type === 'text');
const fullText = textRuns.map(r => (r as any).text).join('');
expect(fullText).toContain('<font size=abc>');
expect(fullText).toContain('text');
});
});
describe('parseRichText – the tags Cocos Creator\'s rich text writes', () => {
const text = (runs: RichTextRun[]): string[] => runs.filter((r) => r.type === 'text').map((r) => (r as { text: string }).text);
it('reads <size=N> as a font size, and </size> ends it', () => {
const runs = parseRichText('a<size=30>b</size>c');
expect(text(runs)).toEqual(['a', 'b', 'c']);
expect(runs.map((r) => (r as { fontSize: number | null }).fontSize)).toEqual([null, 30, null]);
});
it('reads a colour by name as well as by hex', () => {
const runs = parseRichText('<color=red>x</color><color=#00ff00>y</color><color=Blue>z</color>');
const colors = runs.map((r) => (r as { color: { r: number; g: number; b: number } | null }).color);
expect(colors[0]).toMatchObject({ r: 1, g: 0, b: 0 });
expect(colors[1]).toMatchObject({ r: 0, g: 1, b: 0 });
expect(colors[2]).toMatchObject({ r: 0, g: 0, b: 1 });
});
it('does not take an unknown colour name for markup: it stays as text', () => {
expect(text(parseRichText('<color=nonesuch>x</color>')).join('')).toContain('<color=nonesuch>');
});
it('turns <br/> into a line break', () => {
expect(text(parseRichText('a<br/>b')).join('')).toBe('a\nb');
expect(text(parseRichText('a<br>b')).join('')).toBe('a\nb');
});
it('keeps the text of tags it cannot style (underline, strike, outline, click) and drops the tag', () => {
expect(text(parseRichText('<u>under</u> <s>gone</s> <outline color=black width=2>edge</outline> <on click="go">tap</on>')).join(''))
.toBe('under gone edge tap');
});
it('still styles text inside a tag it cannot style', () => {
const runs = parseRichText('<u><b>both</b></u>');
expect(text(runs)).toEqual(['both']);
expect((runs[0] as { bold: boolean }).bold).toBe(true);
});
});