Skip to content

Commit 65f4ac6

Browse files
committed
Merge pull request microsoft#4381 from RyanCavanaugh/fix4125
Convert HTML entities to strings (microsoft#4125)
2 parents 2ed9bdb + 6eca09b commit 65f4ac6

File tree

5 files changed

+341
-4
lines changed

5 files changed

+341
-4
lines changed

src/compiler/emitter.ts

+273-4
Original file line numberDiff line numberDiff line change
@@ -6512,7 +6512,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
65126512
}
65136513
}
65146514

6515-
function trimReactWhitespace(node: JsxText): string {
6515+
function trimReactWhitespaceAndApplyEntities(node: JsxText): string {
65166516
let result: string = undefined;
65176517
let text = getTextOfNode(node);
65186518
let firstNonWhitespace = 0;
@@ -6537,19 +6537,32 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
65376537
}
65386538
}
65396539
}
6540+
65406541
if (firstNonWhitespace !== -1) {
65416542
let part = text.substr(firstNonWhitespace);
65426543
result = (result ? result + "\" + ' ' + \"" : "") + part;
65436544
}
65446545

6546+
if (result) {
6547+
// Replace entities like  
6548+
result = result.replace(/&(\w+);/g, function(s: any, m: string) {
6549+
if (entities[m] !== undefined) {
6550+
return String.fromCharCode(entities[m]);
6551+
}
6552+
else {
6553+
return s;
6554+
}
6555+
});
6556+
}
6557+
65456558
return result;
65466559
}
65476560

65486561
function getTextToEmit(node: JsxText) {
65496562
switch (compilerOptions.jsx) {
65506563
case JsxEmit.React:
6551-
let text = trimReactWhitespace(node);
6552-
if (text.length === 0) {
6564+
let text = trimReactWhitespaceAndApplyEntities(node);
6565+
if (text === undefined || text.length === 0) {
65536566
return undefined;
65546567
}
65556568
else {
@@ -6565,7 +6578,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
65656578
switch (compilerOptions.jsx) {
65666579
case JsxEmit.React:
65676580
write("\"");
6568-
write(trimReactWhitespace(node));
6581+
write(trimReactWhitespaceAndApplyEntities(node));
65696582
write("\"");
65706583
break;
65716584

@@ -7138,4 +7151,260 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
71387151
}
71397152
}
71407153
}
7154+
7155+
var entities: Map<number> = {
7156+
"quot": 0x0022,
7157+
"amp": 0x0026,
7158+
"apos": 0x0027,
7159+
"lt": 0x003C,
7160+
"gt": 0x003E,
7161+
"nbsp": 0x00A0,
7162+
"iexcl": 0x00A1,
7163+
"cent": 0x00A2,
7164+
"pound": 0x00A3,
7165+
"curren": 0x00A4,
7166+
"yen": 0x00A5,
7167+
"brvbar": 0x00A6,
7168+
"sect": 0x00A7,
7169+
"uml": 0x00A8,
7170+
"copy": 0x00A9,
7171+
"ordf": 0x00AA,
7172+
"laquo": 0x00AB,
7173+
"not": 0x00AC,
7174+
"shy": 0x00AD,
7175+
"reg": 0x00AE,
7176+
"macr": 0x00AF,
7177+
"deg": 0x00B0,
7178+
"plusmn": 0x00B1,
7179+
"sup2": 0x00B2,
7180+
"sup3": 0x00B3,
7181+
"acute": 0x00B4,
7182+
"micro": 0x00B5,
7183+
"para": 0x00B6,
7184+
"middot": 0x00B7,
7185+
"cedil": 0x00B8,
7186+
"sup1": 0x00B9,
7187+
"ordm": 0x00BA,
7188+
"raquo": 0x00BB,
7189+
"frac14": 0x00BC,
7190+
"frac12": 0x00BD,
7191+
"frac34": 0x00BE,
7192+
"iquest": 0x00BF,
7193+
"Agrave": 0x00C0,
7194+
"Aacute": 0x00C1,
7195+
"Acirc": 0x00C2,
7196+
"Atilde": 0x00C3,
7197+
"Auml": 0x00C4,
7198+
"Aring": 0x00C5,
7199+
"AElig": 0x00C6,
7200+
"Ccedil": 0x00C7,
7201+
"Egrave": 0x00C8,
7202+
"Eacute": 0x00C9,
7203+
"Ecirc": 0x00CA,
7204+
"Euml": 0x00CB,
7205+
"Igrave": 0x00CC,
7206+
"Iacute": 0x00CD,
7207+
"Icirc": 0x00CE,
7208+
"Iuml": 0x00CF,
7209+
"ETH": 0x00D0,
7210+
"Ntilde": 0x00D1,
7211+
"Ograve": 0x00D2,
7212+
"Oacute": 0x00D3,
7213+
"Ocirc": 0x00D4,
7214+
"Otilde": 0x00D5,
7215+
"Ouml": 0x00D6,
7216+
"times": 0x00D7,
7217+
"Oslash": 0x00D8,
7218+
"Ugrave": 0x00D9,
7219+
"Uacute": 0x00DA,
7220+
"Ucirc": 0x00DB,
7221+
"Uuml": 0x00DC,
7222+
"Yacute": 0x00DD,
7223+
"THORN": 0x00DE,
7224+
"szlig": 0x00DF,
7225+
"agrave": 0x00E0,
7226+
"aacute": 0x00E1,
7227+
"acirc": 0x00E2,
7228+
"atilde": 0x00E3,
7229+
"auml": 0x00E4,
7230+
"aring": 0x00E5,
7231+
"aelig": 0x00E6,
7232+
"ccedil": 0x00E7,
7233+
"egrave": 0x00E8,
7234+
"eacute": 0x00E9,
7235+
"ecirc": 0x00EA,
7236+
"euml": 0x00EB,
7237+
"igrave": 0x00EC,
7238+
"iacute": 0x00ED,
7239+
"icirc": 0x00EE,
7240+
"iuml": 0x00EF,
7241+
"eth": 0x00F0,
7242+
"ntilde": 0x00F1,
7243+
"ograve": 0x00F2,
7244+
"oacute": 0x00F3,
7245+
"ocirc": 0x00F4,
7246+
"otilde": 0x00F5,
7247+
"ouml": 0x00F6,
7248+
"divide": 0x00F7,
7249+
"oslash": 0x00F8,
7250+
"ugrave": 0x00F9,
7251+
"uacute": 0x00FA,
7252+
"ucirc": 0x00FB,
7253+
"uuml": 0x00FC,
7254+
"yacute": 0x00FD,
7255+
"thorn": 0x00FE,
7256+
"yuml": 0x00FF,
7257+
"OElig": 0x0152,
7258+
"oelig": 0x0153,
7259+
"Scaron": 0x0160,
7260+
"scaron": 0x0161,
7261+
"Yuml": 0x0178,
7262+
"fnof": 0x0192,
7263+
"circ": 0x02C6,
7264+
"tilde": 0x02DC,
7265+
"Alpha": 0x0391,
7266+
"Beta": 0x0392,
7267+
"Gamma": 0x0393,
7268+
"Delta": 0x0394,
7269+
"Epsilon": 0x0395,
7270+
"Zeta": 0x0396,
7271+
"Eta": 0x0397,
7272+
"Theta": 0x0398,
7273+
"Iota": 0x0399,
7274+
"Kappa": 0x039A,
7275+
"Lambda": 0x039B,
7276+
"Mu": 0x039C,
7277+
"Nu": 0x039D,
7278+
"Xi": 0x039E,
7279+
"Omicron": 0x039F,
7280+
"Pi": 0x03A0,
7281+
"Rho": 0x03A1,
7282+
"Sigma": 0x03A3,
7283+
"Tau": 0x03A4,
7284+
"Upsilon": 0x03A5,
7285+
"Phi": 0x03A6,
7286+
"Chi": 0x03A7,
7287+
"Psi": 0x03A8,
7288+
"Omega": 0x03A9,
7289+
"alpha": 0x03B1,
7290+
"beta": 0x03B2,
7291+
"gamma": 0x03B3,
7292+
"delta": 0x03B4,
7293+
"epsilon": 0x03B5,
7294+
"zeta": 0x03B6,
7295+
"eta": 0x03B7,
7296+
"theta": 0x03B8,
7297+
"iota": 0x03B9,
7298+
"kappa": 0x03BA,
7299+
"lambda": 0x03BB,
7300+
"mu": 0x03BC,
7301+
"nu": 0x03BD,
7302+
"xi": 0x03BE,
7303+
"omicron": 0x03BF,
7304+
"pi": 0x03C0,
7305+
"rho": 0x03C1,
7306+
"sigmaf": 0x03C2,
7307+
"sigma": 0x03C3,
7308+
"tau": 0x03C4,
7309+
"upsilon": 0x03C5,
7310+
"phi": 0x03C6,
7311+
"chi": 0x03C7,
7312+
"psi": 0x03C8,
7313+
"omega": 0x03C9,
7314+
"thetasym": 0x03D1,
7315+
"upsih": 0x03D2,
7316+
"piv": 0x03D6,
7317+
"ensp": 0x2002,
7318+
"emsp": 0x2003,
7319+
"thinsp": 0x2009,
7320+
"zwnj": 0x200C,
7321+
"zwj": 0x200D,
7322+
"lrm": 0x200E,
7323+
"rlm": 0x200F,
7324+
"ndash": 0x2013,
7325+
"mdash": 0x2014,
7326+
"lsquo": 0x2018,
7327+
"rsquo": 0x2019,
7328+
"sbquo": 0x201A,
7329+
"ldquo": 0x201C,
7330+
"rdquo": 0x201D,
7331+
"bdquo": 0x201E,
7332+
"dagger": 0x2020,
7333+
"Dagger": 0x2021,
7334+
"bull": 0x2022,
7335+
"hellip": 0x2026,
7336+
"permil": 0x2030,
7337+
"prime": 0x2032,
7338+
"Prime": 0x2033,
7339+
"lsaquo": 0x2039,
7340+
"rsaquo": 0x203A,
7341+
"oline": 0x203E,
7342+
"frasl": 0x2044,
7343+
"euro": 0x20AC,
7344+
"image": 0x2111,
7345+
"weierp": 0x2118,
7346+
"real": 0x211C,
7347+
"trade": 0x2122,
7348+
"alefsym": 0x2135,
7349+
"larr": 0x2190,
7350+
"uarr": 0x2191,
7351+
"rarr": 0x2192,
7352+
"darr": 0x2193,
7353+
"harr": 0x2194,
7354+
"crarr": 0x21B5,
7355+
"lArr": 0x21D0,
7356+
"uArr": 0x21D1,
7357+
"rArr": 0x21D2,
7358+
"dArr": 0x21D3,
7359+
"hArr": 0x21D4,
7360+
"forall": 0x2200,
7361+
"part": 0x2202,
7362+
"exist": 0x2203,
7363+
"empty": 0x2205,
7364+
"nabla": 0x2207,
7365+
"isin": 0x2208,
7366+
"notin": 0x2209,
7367+
"ni": 0x220B,
7368+
"prod": 0x220F,
7369+
"sum": 0x2211,
7370+
"minus": 0x2212,
7371+
"lowast": 0x2217,
7372+
"radic": 0x221A,
7373+
"prop": 0x221D,
7374+
"infin": 0x221E,
7375+
"ang": 0x2220,
7376+
"and": 0x2227,
7377+
"or": 0x2228,
7378+
"cap": 0x2229,
7379+
"cup": 0x222A,
7380+
"int": 0x222B,
7381+
"there4": 0x2234,
7382+
"sim": 0x223C,
7383+
"cong": 0x2245,
7384+
"asymp": 0x2248,
7385+
"ne": 0x2260,
7386+
"equiv": 0x2261,
7387+
"le": 0x2264,
7388+
"ge": 0x2265,
7389+
"sub": 0x2282,
7390+
"sup": 0x2283,
7391+
"nsub": 0x2284,
7392+
"sube": 0x2286,
7393+
"supe": 0x2287,
7394+
"oplus": 0x2295,
7395+
"otimes": 0x2297,
7396+
"perp": 0x22A5,
7397+
"sdot": 0x22C5,
7398+
"lceil": 0x2308,
7399+
"rceil": 0x2309,
7400+
"lfloor": 0x230A,
7401+
"rfloor": 0x230B,
7402+
"lang": 0x2329,
7403+
"rang": 0x232A,
7404+
"loz": 0x25CA,
7405+
"spades": 0x2660,
7406+
"clubs": 0x2663,
7407+
"hearts": 0x2665,
7408+
"diams": 0x2666
7409+
}
71417410
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//// [tsxReactEmitEntities.tsx]
2+
declare module JSX {
3+
interface Element { }
4+
interface IntrinsicElements {
5+
[s: string]: any;
6+
}
7+
}
8+
declare var React: any;
9+
10+
<div>Dot goes here: &middot; &notAnEntity; </div>;
11+
12+
13+
//// [tsxReactEmitEntities.js]
14+
React.createElement("div", null, "Dot goes here: · &notAnEntity; ");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
=== tests/cases/conformance/jsx/tsxReactEmitEntities.tsx ===
2+
declare module JSX {
3+
>JSX : Symbol(JSX, Decl(tsxReactEmitEntities.tsx, 0, 0))
4+
5+
interface Element { }
6+
>Element : Symbol(Element, Decl(tsxReactEmitEntities.tsx, 0, 20))
7+
8+
interface IntrinsicElements {
9+
>IntrinsicElements : Symbol(IntrinsicElements, Decl(tsxReactEmitEntities.tsx, 1, 22))
10+
11+
[s: string]: any;
12+
>s : Symbol(s, Decl(tsxReactEmitEntities.tsx, 3, 3))
13+
}
14+
}
15+
declare var React: any;
16+
>React : Symbol(React, Decl(tsxReactEmitEntities.tsx, 6, 11))
17+
18+
<div>Dot goes here: &middot; &notAnEntity; </div>;
19+
>div : Symbol(JSX.IntrinsicElements, Decl(tsxReactEmitEntities.tsx, 1, 22))
20+
>div : Symbol(JSX.IntrinsicElements, Decl(tsxReactEmitEntities.tsx, 1, 22))
21+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
=== tests/cases/conformance/jsx/tsxReactEmitEntities.tsx ===
2+
declare module JSX {
3+
>JSX : any
4+
5+
interface Element { }
6+
>Element : Element
7+
8+
interface IntrinsicElements {
9+
>IntrinsicElements : IntrinsicElements
10+
11+
[s: string]: any;
12+
>s : string
13+
}
14+
}
15+
declare var React: any;
16+
>React : any
17+
18+
<div>Dot goes here: &middot; &notAnEntity; </div>;
19+
><div>Dot goes here: &middot; &notAnEntity; </div> : JSX.Element
20+
>div : any
21+
>div : any
22+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//@filename: file.tsx
2+
//@jsx: react
3+
declare module JSX {
4+
interface Element { }
5+
interface IntrinsicElements {
6+
[s: string]: any;
7+
}
8+
}
9+
declare var React: any;
10+
11+
<div>Dot goes here: &middot; &notAnEntity; </div>;

0 commit comments

Comments
 (0)