Skip to content

Commit 9ffe6d5

Browse files
authored
Merge pull request #1307 from mathjax/update/mml-visitor
Move toEntry to be a method so that it can be override, if necessary
2 parents c693d1b + 0f7dd5d commit 9ffe6d5

3 files changed

Lines changed: 27 additions & 5 deletions

File tree

testsuite/tests/util/string.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,8 @@ describe('string functions', () => {
5959
expect(string.replaceUnicode(String.raw`x\\\\\U{61}`)).toBe(String.raw`x\\\\a`);
6060
});
6161

62+
test('toEntity()', () => {
63+
expect(string.toEntity('\u200B')).toBe('​');
64+
});
65+
6266
});

ts/core/MmlTree/SerializedMmlVisitor.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@
2525
import { MmlVisitor } from './MmlVisitor.js';
2626
import { MmlNode, TextNode, XMLNode } from './MmlNode.js';
2727
import { HtmlNode } from './MmlNodes/HtmlNode.js';
28-
29-
export const toEntity = (c: string) =>
30-
'&#x' + c.codePointAt(0).toString(16).toUpperCase() + ';';
28+
import { toEntity } from '../../util/string.js';
3129

3230
/*****************************************************************/
3331
/**
@@ -161,7 +159,17 @@ export class SerializedMmlVisitor extends MmlVisitor {
161159
.replace(/</g, '&lt;')
162160
.replace(/>/g, '&gt;')
163161
.replace(/"/g, '&quot;')
164-
.replace(/[\uD800-\uDBFF]./g, toEntity)
165-
.replace(/[\u0080-\uD7FF\uE000-\uFFFF]/g, toEntity);
162+
.replace(/[\uD800-\uDBFF]./g, this.toEntity)
163+
.replace(/[\u0080-\uD7FF\uE000-\uFFFF]/g, this.toEntity);
164+
}
165+
166+
/**
167+
* Access to the toEntity() function that can be overridden in subclasses.
168+
*
169+
* @param {string} c The character to encode.
170+
* @returns {string} The numeric entity for the character.
171+
*/
172+
protected toEntity(c: string): string {
173+
return toEntity(c);
166174
}
167175
}

ts/util/string.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,13 @@ export function replaceUnicode(text: string): string {
100100
(_m, pre, h1, h2) => pre + String.fromCodePoint(parseInt(h1 || h2, 16))
101101
);
102102
}
103+
104+
/**
105+
* Turn a character into its numeric entity for HTML
106+
*
107+
* @param {string} c The character to convert
108+
* @returns {string} The entity string for the character
109+
*/
110+
export function toEntity(c: string): string {
111+
return `&#x${c.codePointAt(0).toString(16).toUpperCase()};`;
112+
}

0 commit comments

Comments
 (0)