Skip to content

Commit 0f7dd5d

Browse files
committed
Move toEntity() function to util/string.ts
1 parent 11c6c82 commit 0f7dd5d

3 files changed

Lines changed: 15 additions & 3 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: 1 addition & 3 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
/**

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)