diff --git a/src/editor/core/cm/docRender.test.ts b/src/editor/core/cm/docRender.test.ts
index 3628840cc4..013b5deed6 100644
--- a/src/editor/core/cm/docRender.test.ts
+++ b/src/editor/core/cm/docRender.test.ts
@@ -1,4 +1,5 @@
import { describe, expect, it } from "vitest";
+import { markdownToSafeHtml } from "../../lsp/markdownParser";
import { extractInlineTags, hasContent, renderDoc, restoreInlineTags, splitBody, stripMarkers } from "./docRender";
describe("hasContent", () => {
@@ -106,7 +107,21 @@ describe("extractInlineTags", () => {
it("renders {@code} as escaped code", () => {
const { text, html } = extractInlineTags("Use {@code a < b}.");
expect(html).toEqual(["a < b"]);
- expect(restoreInlineTags(text, html)).toBe("Use a < b.");
+ expect(restoreInlineTags(text, html)).toBe("Use a < b.");
+ });
+
+ it("puts a tag's text, never its HTML, into an attribute", () => {
+ const { text, html } = extractInlineTags(
+ 'See
and {@link Bar}.',
+ );
+ const restored = restoreInlineTags(markdownToSafeHtml(text), html);
+ const root = document.createElement("div");
+ root.innerHTML = restored;
+ const img = root.querySelector("img")!;
+ expect(img.getAttribute("alt")).toBe("Foo");
+ expect(img.hasAttribute("onerror")).toBe(false);
+ expect(root.querySelectorAll("code.cm-openidea-docLink")).toHaveLength(1);
+ expect(root.querySelector("code")?.textContent).toBe("Bar");
});
it("keeps a code span out of the Markdown, so a_b_c is not italicised", () => {
diff --git a/src/editor/core/cm/docRender.ts b/src/editor/core/cm/docRender.ts
index effe4f9dc5..182ced38eb 100644
--- a/src/editor/core/cm/docRender.ts
+++ b/src/editor/core/cm/docRender.ts
@@ -10,8 +10,8 @@
*
* Inline tags become placeholders before the Markdown runs and their HTML goes back
* in afterwards: their text is code, not Markdown, and `{@code a_b_c}` must not come
- * out italicised. The HTML put back is built here from escaped text, so it is safe
- * although it never passes through the sanitizer.
+ * out italicised. The HTML put back is built here from escaped text and goes into text
+ * nodes of the sanitized tree, so it is safe although it never passes through the sanitizer.
*/
/** A doc comment's text, by the markers that open it. */
@@ -267,9 +267,49 @@ function inlineTagHtml(tag: string, body: string): string {
}
}
-/** Puts the inline tags' HTML back where their placeholders are. */
+/**
+ * Puts the inline tags' HTML back where their placeholders are, in the text of the sanitized
+ * HTML only. A placeholder can also land in an attribute (`
`), where the
+ * tag's HTML would end the attribute and start markup the sanitizer never saw: there it
+ * becomes the tag's text.
+ */
export function restoreInlineTags(html: string, tags: string[]): string {
- return html.replace(PLACEHOLDER, (all, index: string) => tags[Number(index)] ?? all);
+ if (!html.includes("\uE000")) return html;
+ const template = document.createElement("template");
+ template.innerHTML = html;
+ const fragment = (tag: string) => {
+ const t = document.createElement("template");
+ t.innerHTML = tag;
+ return t.content;
+ };
+ for (const el of template.content.querySelectorAll("*")) {
+ for (const attr of Array.from(el.attributes)) {
+ if (!attr.value.includes("\uE000")) continue;
+ attr.value = attr.value.replace(PLACEHOLDER, (all, index: string) => {
+ const tag = tags[Number(index)];
+ return tag === undefined ? all : (fragment(tag).textContent ?? "");
+ });
+ }
+ }
+ const walker = document.createTreeWalker(template.content, NodeFilter.SHOW_TEXT);
+ const texts: Text[] = [];
+ for (let node = walker.nextNode(); node; node = walker.nextNode()) {
+ if (node.nodeValue?.includes("\uE000")) texts.push(node as Text);
+ }
+ for (const node of texts) {
+ const value = node.nodeValue ?? "";
+ const parts = document.createDocumentFragment();
+ let last = 0;
+ for (const m of value.matchAll(PLACEHOLDER)) {
+ const tag = tags[Number(m[1])];
+ if (tag === undefined) continue;
+ parts.append(value.slice(last, m.index), fragment(tag));
+ last = m.index + m[0].length;
+ }
+ parts.append(value.slice(last));
+ node.replaceWith(parts);
+ }
+ return template.innerHTML;
}
export interface RenderedDoc {