Skip to content

Commit 03e98ab

Browse files
committed
fix(webview): keep mentions literal in code and keyboard accessible
Addresses the updated CodeRabbit merge-risk notes on PR Zoo-Code-Org#1257: rehypeMentions now skips text inside code elements (mention patterns in code blocks rendered verbatim and no longer vanished from CodeBlock text extraction), and mention spans are keyboard operable via role=button, tabIndex and Enter/Space key handling. Adds regression tests for both behaviors.
1 parent 99986b3 commit 03e98ab

2 files changed

Lines changed: 62 additions & 0 deletions

File tree

‎webview-ui/src/components/common/MarkdownBlock.tsx‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ function rehypeMentions() {
2525
return
2626
}
2727

28+
// Code is literal content: never interactive-ify mention patterns inside it.
29+
// Wrapping them would also corrupt the CodeBlock text extraction, which only
30+
// keeps string children (the mention text would silently disappear).
31+
if (parent?.tagName === "code") {
32+
return
33+
}
34+
2835
const originalValue = String(node.value)
2936
const matches = Array.from(originalValue.matchAll(mentionRegexGlobal))
3037

@@ -49,12 +56,23 @@ function rehypeMentions() {
4956
tagName: "span",
5057
properties: {
5158
className: ["mention-context-highlight", "text-[0.9em]", "cursor-pointer"],
59+
role: "button",
60+
tabIndex: 0,
5261
onClick: (event: React.MouseEvent<HTMLSpanElement>) => {
5362
// Keep mention clicks from bubbling to the TaskHeader toggle, which
5463
// would collapse the expanded panel right after opening the mention.
5564
event.stopPropagation()
5665
vscode.postMessage({ type: "openMention", text: mentionValue })
5766
},
67+
// Keyboard parity with the click handler (a role=button span is not a
68+
// native button, so Enter/Space must be handled explicitly).
69+
onKeyDown: (event: React.KeyboardEvent<HTMLSpanElement>) => {
70+
if (event.key !== "Enter" && event.key !== " ") {
71+
return
72+
}
73+
event.stopPropagation()
74+
vscode.postMessage({ type: "openMention", text: mentionValue })
75+
},
5876
},
5977
children: [{ type: "text", value: mentionText }],
6078
})

‎webview-ui/src/components/common/__tests__/MarkdownBlock.spec.tsx‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,50 @@ describe("MarkdownBlock", () => {
304304
expect(mentions.length).toBe(0)
305305
})
306306

307+
it("keeps mention patterns literal inside fenced code blocks", async () => {
308+
const markdown = "```bash\necho hello @problems\n```"
309+
const { container } = render(<MarkdownBlock markdown={markdown} />)
310+
311+
await screen.findByText(/echo/, { exact: false })
312+
313+
// Code is literal content: the mention must stay plain text, not become a
314+
// clickable span (which would also make the text vanish from CodeBlock).
315+
expect(container.querySelector("code")?.textContent).toBe("echo hello @problems\n")
316+
expect(container.querySelectorAll("span.mention-context-highlight").length).toBe(0)
317+
})
318+
319+
it("keeps mention patterns literal inside inline code", async () => {
320+
const markdown = "Use `@problems` carefully."
321+
const { container } = render(<MarkdownBlock markdown={markdown} />)
322+
323+
await screen.findByText(/Use/, { exact: false })
324+
325+
expect(container.querySelector("code")?.textContent).toBe("@problems")
326+
expect(container.querySelectorAll("span.mention-context-highlight").length).toBe(0)
327+
})
328+
329+
it("makes mentions keyboard operable (role=button, tabIndex, Enter/Space)", async () => {
330+
const markdown = "See @terminal."
331+
const { container } = render(<MarkdownBlock markdown={markdown} />)
332+
333+
await screen.findByText(/See/, { exact: false })
334+
335+
const mention = container.querySelector("span.mention-context-highlight")!
336+
expect(mention.getAttribute("role")).toBe("button")
337+
expect(mention.getAttribute("tabindex")).toBe("0")
338+
339+
fireEvent.keyDown(mention, { key: "Enter" })
340+
expect(mockPostMessage).toHaveBeenCalledWith({ type: "openMention", text: "terminal" })
341+
342+
mockPostMessage.mockClear()
343+
fireEvent.keyDown(mention, { key: " " })
344+
expect(mockPostMessage).toHaveBeenCalledWith({ type: "openMention", text: "terminal" })
345+
346+
mockPostMessage.mockClear()
347+
fireEvent.keyDown(mention, { key: "a" })
348+
expect(mockPostMessage).not.toHaveBeenCalled()
349+
})
350+
307351
it("preserves regular text around mentions", async () => {
308352
const markdown = "Before @problems middle after"
309353
const { container } = render(<MarkdownBlock markdown={markdown} />)

0 commit comments

Comments
 (0)