When emitting react code, replace HTML numeric entities with their encoded characters

This commit is contained in:
Andy Hanson
2016-09-07 06:34:28 -07:00
parent 874846a534
commit eea03801e0
5 changed files with 23 additions and 5 deletions

View File

@@ -210,15 +210,21 @@ namespace ts {
}
/**
* Decodes JSX entities.
* Replace entities like " ", "{", and "�" with the characters they encode.
* See https://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references
*/
function decodeEntities(text: string) {
return text.replace(/&(\w+);/g, function(s: any, m: string) {
if (entities[m] !== undefined) {
return String.fromCharCode(entities[m]);
return text.replace(/&((#((\d+)|x([\da-fA-F]+)))|(\w+));/g, (match, _all, _number, _digits, decimal, hex, word) => {
if (decimal) {
return String.fromCharCode(parseInt(decimal, 10));
}
else if (hex) {
return String.fromCharCode(parseInt(hex, 16));
}
else {
return s;
const ch = entities[word];
// If this is not a valid entity, then just use `match` (replace it with itself, i.e. don't replace)
return ch ? String.fromCharCode(ch) : match;
}
});
}