-
Notifications
You must be signed in to change notification settings - Fork 247
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(rosetta): properly escape C# comments as XML (#1184)
The C# compiler will completely drop comment blocks that aren't well-formed XML; the <img> tag we were outputting wasn't, and so namespace headings weren't being generated. At the same time, pay more attention to escaping of arbitrary HTML and the text inside attributes to minimize the chances of things going wrong in the future.
- Loading branch information
Showing
7 changed files
with
182 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
export interface Escaper { | ||
/** | ||
* Escape for use in XML/HTML text | ||
*/ | ||
text(x: string | null): string; | ||
|
||
/** | ||
* Escape for use in XML/HTML attributes | ||
*/ | ||
attribute(x: string | null): string; | ||
|
||
/** | ||
* Re-escape a string that has been escaped for text to be escaped for attributes | ||
* | ||
* Conceptually this unescapes text back to raw and re-escapes for attributes, | ||
* but for speed in practice we just do the additional escapes. | ||
*/ | ||
text2attr(x: string | null): string; | ||
} | ||
|
||
/** | ||
* Make a generic XML escaper | ||
*/ | ||
export function makeXmlEscaper(): Escaper { | ||
const attr: Escapes = [...TEXT, ...ATTR_ADDL]; | ||
|
||
return { | ||
text: (x) => escapeText(TEXT, x), | ||
attribute: (x) => escapeText(attr, x), | ||
text2attr: (x) => escapeText(ATTR_ADDL, x) | ||
}; | ||
} | ||
|
||
/** | ||
* Make a Java specific escaper | ||
* | ||
* This one also escapes '@' because that triggers parsing of comment directives | ||
* in Java. | ||
*/ | ||
export function makeJavaEscaper(): Escaper { | ||
const javaText: Escapes = [...TEXT, [new RegExp('@', 'g'), '@']]; | ||
const javaAttr: Escapes = [...javaText, ...ATTR_ADDL]; | ||
|
||
return { | ||
text: (x) => escapeText(javaText, x), | ||
attribute: (x) => escapeText(javaAttr, x), | ||
text2attr: (x) => escapeText(ATTR_ADDL, x) | ||
}; | ||
} | ||
|
||
type Escapes = Array<[RegExp, string]>; | ||
|
||
const TEXT: Escapes = [ | ||
[new RegExp('&', 'g'), '&'], | ||
[new RegExp('<', 'g'), '<'], | ||
[new RegExp('>', 'g'), '>'], | ||
]; | ||
|
||
// Additional escapes (in addition to the text escapes) which need to be escaped inside attributes. | ||
const ATTR_ADDL: Escapes = [ | ||
[new RegExp('"', 'g'), '"'], | ||
[new RegExp("'", 'g'), '''], | ||
]; | ||
|
||
function escapeText(set: Escapes, what: string | null): string { | ||
if (!what) { return ''; } | ||
|
||
for (const [re, repl] of set) { | ||
what = what.replace(re, repl); | ||
} | ||
|
||
return what; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters