Open platform to collect and prioritize feedback
fix(ui): hide "<" with a placeholder instead of disabling HTML tokenizers
Matt Robertscb9de449b59c
diff --git a/public/services/markdown.spec.ts b/public/services/markdown.spec.tsindex d773423e0..872d0cef2 100644--- a/public/services/markdown.spec.ts+++ b/public/services/markdown.spec.ts@@ -238,10 +238,28 @@ describe("text is shown exactly as typed", () => {expect(shown(markdown.plainText("`List<String>` and `a && b`"))).toEqual("List<String> and a && b")expect(markdown.toText("`List<String>` and `a && b`")).toEqual("List<String> and a && b")})+})++describe("text that starts with < keeps its block structure", () => {+ // "<" must not reach marked's HTML-block rules, which would end paragraphs and list items early+ const cases: [string, string][] = [+ ["- Use a map\n<String, Int> as the key", "<ul>\n<li>Use a map<br><String, Int> as the key</li>\n</ul>"],+ ["a\n<div>b</div>", "<p>a<br><div>b</div></p>"],+ ["Steps:\n<p>para</p>", "<p>Steps:<br><p>para</p></p>"],+ ["- item\n<!-- c -->", "<ul>\n<li>item<br><!-- c --></li>\n</ul>"],+ ]++ cases.forEach(([input, expected]) => {+ test(`full: ${JSON.stringify(input)}`, () => {+ expect(markdown.full(input)).toEqual(expected)+ })+ })- test("angle-bracket autolinks become links, as in the editor", () => {- expect(markdown.full("<https://example.com>")).toEqual(- '<p><a class="text-link" href="https://example.com" rel="noopener nofollow" target="_blank">https://example.com</a></p>'+ test("angle brackets stay visible text, not <...> autolinks", () => {+ // (the bare email is still linked by GFM's email autolinking, as before)+ expect(markdown.full("<scheme:foo> and <[email protected]>")).toEqual(+ '<p><scheme:foo> and <<a class="text-link" href="mailto:[email protected]" rel="noopener nofollow" target="_blank">[email protected]</a>></p>')+ expect(markdown.toText("Contact <[email protected]> about X")).toEqual("Contact <[email protected]> about X")})})diff --git a/public/services/markdown.ts b/public/services/markdown.tsindex 502da22c6..a121d966a 100644--- a/public/services/markdown.ts+++ b/public/services/markdown.ts@@ -69,19 +69,9 @@ const fullRenderer: RendererObject = {const rendered = "tokens" in token && token.tokens ? this.parser.parseInline(token.tokens) : escapeText(token.text)return renderMentions(rendered)},- html({ text }) {- // Not reached (raw HTML isn't tokenized, see noRawHTML), but never emit it unescaped.- return escapeText(text)- },}--// Raw HTML is never rendered: with its tokenizers off, "<b>" is just text and is escaped once-// by the text/code renderers. (Escaping "<" before parsing instead made code escape it twice,-// showing `List<String>` as `List<String>`, and broke <https://...> autolinks.)-const noRawHTML = { tokenizer: { html: () => undefined, tag: () => undefined } }-const markedFull = new Marked({ gfm: true, breaks: true })-markedFull.use(noRawHTML, { renderer: fullRenderer })+markedFull.use({ renderer: fullRenderer })// Plain-text renderer: strip all formatting down to readable text.const plainTextRenderer: RendererObject = {@@ -123,7 +113,17 @@ const plainTextRenderer: RendererObject = {},}const markedPlainText = new Marked({ gfm: true, breaks: true })-markedPlainText.use(noRawHTML, { renderer: plainTextRenderer })+markedPlainText.use({ renderer: plainTextRenderer })++// Raw HTML is never rendered: "<" is swapped for a placeholder before parsing, so marked never+// sees tags, HTML blocks or <...> autolinks (and they can't change where paragraphs and list items+// end), then the placeholder becomes "<" after rendering. Swapping in "<" itself made code+// escape it a second time, showing `List<String>` as `List<String>`. (A placeholder character+// already in the input is replaced with U+FFFD so it can't be mistaken for an escaped "<".)+const LT_PLACEHOLDER = "\uE000"+const hideLessThan = (s: string) => s.replace(/\uE000/g, "\uFFFD").replace(/</g, LT_PLACEHOLDER)+const restoreLessThan = (html: string) => html.replace(/\uE000/g, "<")+const render = (marked: Marked, input: string) => restoreLessThan(marked.parse(hideLessThan(input)) as string).trim()const stripTags = (input: string) => input.replace(/<[^>]*>/g, "")const sanitize = (input: string) => (DOMPurify.isSupported ? DOMPurify.sanitize(input) : stripTags(input))@@ -143,12 +143,12 @@ const decodeHtmlEntities = (text: string): string => {}export const full = (input: string): string => {- return sanitize((markedFull.parse(input) as string).trim())+ return sanitize(render(markedFull, input))}// HTML with the formatting stripped, safe to insert as HTML (<Markdown style="plainText" />)export const plainText = (input: string): string => {- return sanitize((markedPlainText.parse(input) as string).trim())+ return sanitize(render(markedPlainText, input))}// Plain text with entities decoded (e.g. auto-generated titles). Never insert the result as HTML