Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions testsuite/tests/util/string.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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('​');
});

});
18 changes: 13 additions & 5 deletions ts/core/MmlTree/SerializedMmlVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/*****************************************************************/
/**
Expand Down Expand Up @@ -161,7 +159,17 @@ export class SerializedMmlVisitor extends MmlVisitor {
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.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);
}
}
10 changes: 10 additions & 0 deletions ts/util/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()};`;
}