Skip to content

Commit

Permalink
fix(pacmak): xmldom error when generating packages (#2713)
Browse files Browse the repository at this point in the history
In some cases, an `xmldom` error would occur when trying to render C#
XML-style documentation blocks, where inline html within a Markdown
document is not valid as far as the `xmldom` parser is considered. This
introduces a safer fall-back strategy.
  • Loading branch information
RomainMuller authored Mar 16, 2021
1 parent a950282 commit 6b2bbe8
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions packages/jsii-rosetta/lib/markdown/xml-comment-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,24 @@ export class CSharpXmlCommentRenderer extends MarkdownRenderer {
*/
public html_inline(node: cm.Node, _context: RendererContext) {
const html = node.literal ?? '';
const doc = new DOMParser().parseFromString(html, 'text/html');
return new XMLSerializer().serializeToString(doc);
try {
const doc = new DOMParser().parseFromString(html, 'text/html');
return new XMLSerializer().serializeToString(doc);
} catch {
// Could not parse - we'll escape unsafe XML entities here...
return html.replace(/[<>&]/g, (char: string) => {
switch (char) {
case '&':
return '&amp;';
case '<':
return '&lt;';
case '>':
return '&gt;';
default:
return char;
}
});
}
}

/**
Expand All @@ -89,9 +105,7 @@ export class CSharpXmlCommentRenderer extends MarkdownRenderer {
* If we don't do this, the parser will reject the whole XML block once it seens an unclosed
* <img> tag.
*/
public html_block(node: cm.Node, _context: RendererContext) {
const html = node.literal ?? '';
const doc = new DOMParser().parseFromString(html, 'text/html');
return new XMLSerializer().serializeToString(doc);
public html_block(node: cm.Node, context: RendererContext) {
return this.html_inline(node, context);
}
}

0 comments on commit 6b2bbe8

Please sign in to comment.