Skip to content

Commit 3f6a510

Browse files
author
Andy Hanson
committed
When emitting react code, replace HTML numeric entities with their encoded characters
1 parent 890c793 commit 3f6a510

File tree

5 files changed

+23
-5
lines changed

5 files changed

+23
-5
lines changed

src/compiler/transformers/jsx.ts

+11-5
Original file line numberDiff line numberDiff line change
@@ -210,15 +210,21 @@ namespace ts {
210210
}
211211

212212
/**
213-
* Decodes JSX entities.
213+
* Replace entities like " ", "{", and "�" with the characters they encode.
214+
* See https://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references
214215
*/
215216
function decodeEntities(text: string) {
216-
return text.replace(/&(\w+);/g, function(s: any, m: string) {
217-
if (entities[m] !== undefined) {
218-
return String.fromCharCode(entities[m]);
217+
return text.replace(/&((#((\d+)|x([\da-fA-F]+)))|(\w+));/g, (match, _all, _number, _digits, decimal, hex, word) => {
218+
if (decimal) {
219+
return String.fromCharCode(parseInt(decimal, 10));
220+
}
221+
else if (hex) {
222+
return String.fromCharCode(parseInt(hex, 16));
219223
}
220224
else {
221-
return s;
225+
const ch = entities[word];
226+
// If this is not a valid entity, then just use `match` (replace it with itself, i.e. don't replace)
227+
return ch ? String.fromCharCode(ch) : match;
222228
}
223229
});
224230
}

tests/baselines/reference/tsxReactEmitEntities.js

+2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ declare var React: any;
99

1010
<div>Dot goes here: &middot; &notAnEntity; </div>;
1111
<div>Be careful of &quot;-ed strings!</div>;
12+
<div>&#0123;&#123;braces&#x7d;&#x7D;</div>;
1213

1314

1415
//// [file.js]
1516
React.createElement("div", null, "Dot goes here: \u00B7 &notAnEntity; ");
1617
React.createElement("div", null, "Be careful of \"-ed strings!");
18+
React.createElement("div", null, "{{braces}}");

tests/baselines/reference/tsxReactEmitEntities.symbols

+4
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,7 @@ declare var React: any;
2323
>div : Symbol(JSX.IntrinsicElements, Decl(file.tsx, 1, 22))
2424
>div : Symbol(JSX.IntrinsicElements, Decl(file.tsx, 1, 22))
2525

26+
<div>&#0123;&#123;braces&#x7d;&#x7D;</div>;
27+
>div : Symbol(JSX.IntrinsicElements, Decl(file.tsx, 1, 22))
28+
>div : Symbol(JSX.IntrinsicElements, Decl(file.tsx, 1, 22))
29+

tests/baselines/reference/tsxReactEmitEntities.types

+5
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,8 @@ declare var React: any;
2525
>div : any
2626
>div : any
2727

28+
<div>&#0123;&#123;braces&#x7d;&#x7D;</div>;
29+
><div>&#0123;&#123;braces&#x7d;&#x7D;</div> : JSX.Element
30+
>div : any
31+
>div : any
32+

tests/cases/conformance/jsx/tsxReactEmitEntities.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ declare var React: any;
1010

1111
<div>Dot goes here: &middot; &notAnEntity; </div>;
1212
<div>Be careful of &quot;-ed strings!</div>;
13+
<div>&#0123;&#123;braces&#x7d;&#x7D;</div>;

0 commit comments

Comments
 (0)