From e6aefba93ad2b1691c363a348e3891050cffd569 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=F0=9F=91=A8=F0=9F=8F=BB=E2=80=8D=F0=9F=92=BB=20Romain=20M?= =?UTF-8?q?arcadier?= Date: Tue, 16 Mar 2021 16:38:38 +0100 Subject: [PATCH] fix(pacmak): xmldom error when generating packages 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. --- .../lib/markdown/xml-comment-renderer.ts | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/packages/jsii-rosetta/lib/markdown/xml-comment-renderer.ts b/packages/jsii-rosetta/lib/markdown/xml-comment-renderer.ts index 3cb3f927e2..72564376b1 100644 --- a/packages/jsii-rosetta/lib/markdown/xml-comment-renderer.ts +++ b/packages/jsii-rosetta/lib/markdown/xml-comment-renderer.ts @@ -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 '&'; + case '<': + return '<'; + case '>': + return '>'; + default: + return char; + } + }); + } } /** @@ -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 * 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); } }