fix(editor): put inline doc tags back into the text of rendered comments only (#8376)

Rendered doc comments replace `{@link}`, `{@code}` and the other inline tags
with placeholders, sanitize the Markdown, then put the tags' HTML back by
string replacement. A placeholder inside an attribute value
(`<img alt="{@link Foo}">`) got that HTML too: its quotes ended the attribute,
and the rest of the comment's markup became attributes the sanitizer never saw.

The tags now go back on the sanitized DOM: into text nodes as their HTML, and
into attribute values as their plain text.
Цей коміт міститься в:
l0ng-ai authored and GitHub committed 2026-12-06 20:12:39 +08:00
1 джерело 871dbd0a55
коміт 02e4cafa89
2 files changed
+60 -5

No files matched your search

+16 -1
Переглянути файл
@@ -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(["<code>a &#60; b</code>"]);
expect(restoreInlineTags(text, html)).toBe("Use <code>a &#60; b</code>.");
expect(restoreInlineTags(text, html)).toBe("Use <code>a &lt; b</code>.");
});
it("puts a tag's text, never its HTML, into an attribute", () => {
const { text, html } = extractInlineTags(
'See <img alt="{@link Foo}" title=" onerror=alert(1) x=" src="x.png"> 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", () => {
+44 -4
Переглянути файл
@@ -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 (`<img alt="{@link Foo}">`), 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 {