diff --git a/testsuite/tests/util/string.test.ts b/testsuite/tests/util/string.test.ts index 884751cd8..5818e22f7 100644 --- a/testsuite/tests/util/string.test.ts +++ b/testsuite/tests/util/string.test.ts @@ -59,4 +59,8 @@ describe('string functions', () => { expect(string.replaceUnicode(String.raw`x\\\\\U{61}`)).toBe(String.raw`x\\\\a`); }); + test('toEntity()', () => { + expect(string.toEntity('\u200B')).toBe('​'); + }); + }); diff --git a/ts/core/MmlTree/SerializedMmlVisitor.ts b/ts/core/MmlTree/SerializedMmlVisitor.ts index 8a6c90adb..a09add02e 100644 --- a/ts/core/MmlTree/SerializedMmlVisitor.ts +++ b/ts/core/MmlTree/SerializedMmlVisitor.ts @@ -25,9 +25,7 @@ import { MmlVisitor } from './MmlVisitor.js'; import { MmlNode, TextNode, XMLNode } from './MmlNode.js'; import { HtmlNode } from './MmlNodes/HtmlNode.js'; - -export const toEntity = (c: string) => - '&#x' + c.codePointAt(0).toString(16).toUpperCase() + ';'; +import { toEntity } from '../../util/string.js'; /*****************************************************************/ /** @@ -161,7 +159,17 @@ export class SerializedMmlVisitor extends MmlVisitor { .replace(//g, '>') .replace(/"/g, '"') - .replace(/[\uD800-\uDBFF]./g, toEntity) - .replace(/[\u0080-\uD7FF\uE000-\uFFFF]/g, toEntity); + .replace(/[\uD800-\uDBFF]./g, this.toEntity) + .replace(/[\u0080-\uD7FF\uE000-\uFFFF]/g, this.toEntity); + } + + /** + * Access to the toEntity() function that can be overridden in subclasses. + * + * @param {string} c The character to encode. + * @returns {string} The numeric entity for the character. + */ + protected toEntity(c: string): string { + return toEntity(c); } } diff --git a/ts/util/string.ts b/ts/util/string.ts index 453d38d63..df716d166 100644 --- a/ts/util/string.ts +++ b/ts/util/string.ts @@ -100,3 +100,13 @@ export function replaceUnicode(text: string): string { (_m, pre, h1, h2) => pre + String.fromCodePoint(parseInt(h1 || h2, 16)) ); } + +/** + * Turn a character into its numeric entity for HTML + * + * @param {string} c The character to convert + * @returns {string} The entity string for the character + */ +export function toEntity(c: string): string { + return `&#x${c.codePointAt(0).toString(16).toUpperCase()};`; +}