From 11c6c8295ce1eada89a302d61482d251856f28f2 Mon Sep 17 00:00:00 2001 From: "Davide P. Cervone" Date: Mon, 14 Jul 2025 07:26:17 -0400 Subject: [PATCH 1/2] Move toEntry to be a method so that it can be override, if necessary --- ts/core/MmlTree/SerializedMmlVisitor.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/ts/core/MmlTree/SerializedMmlVisitor.ts b/ts/core/MmlTree/SerializedMmlVisitor.ts index 8a6c90adb..b11971de1 100644 --- a/ts/core/MmlTree/SerializedMmlVisitor.ts +++ b/ts/core/MmlTree/SerializedMmlVisitor.ts @@ -161,7 +161,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); } } From 0f7dd5d3d8381f331c157bc89f37bb3b0a0831a3 Mon Sep 17 00:00:00 2001 From: "Davide P. Cervone" Date: Fri, 18 Jul 2025 14:26:24 -0400 Subject: [PATCH 2/2] Move toEntity() function to util/string.ts --- testsuite/tests/util/string.test.ts | 4 ++++ ts/core/MmlTree/SerializedMmlVisitor.ts | 4 +--- ts/util/string.ts | 10 ++++++++++ 3 files changed, 15 insertions(+), 3 deletions(-) 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 b11971de1..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'; /*****************************************************************/ /** 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()};`; +}