From cc16ac6ac495734774e75b09bf4a0c80bb7f7439 Mon Sep 17 00:00:00 2001 From: "paseo-bot[bot]" <266920839+paseo-bot[bot]@users.noreply.github.com> Date: Thu, 12 Nov 2026 21:52:13 +0000 Subject: [PATCH] fix(app): keep line breaks when copying a selection that crosses part of a code block (#9515) * Preserve partial code blocks in assistant selection copy * test(app): cover partial code copies in the real assistant selection journey Copying from a paragraph into part of a highlighted code block, or from partway through a block out to the next paragraph, now has browser journey coverage. On main both copies join the code lines into one line and drop the fence. Co-authored-by: Michael Wu Co-Authored-By: Claude Opus 5.5 * fix(app): keep indented code lines inside a copied list item Copying a list item that holds a code block indented only the code lines that start with a non-space character, so an indented line fell outside the item and broke the fence. Indent every non-empty line of the item. Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Michael Wu Co-authored-by: paseo-bot[bot] <266920839+paseo-bot[bot]@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 --- .../browser/assistant-selection-copy.spec.ts | 37 ++++++++++++-- .../content.browser.test.ts | 50 +++++++++++++++++++ .../assistant-selection-copy/content.web.ts | 11 +++- 3 files changed, 94 insertions(+), 4 deletions(-) diff --git a/packages/app/e2e/browser/assistant-selection-copy.spec.ts b/packages/app/e2e/browser/assistant-selection-copy.spec.ts index 81f89290c..0703ff134 100644 --- a/packages/app/e2e/browser/assistant-selection-copy.spec.ts +++ b/packages/app/e2e/browser/assistant-selection-copy.spec.ts @@ -644,6 +644,22 @@ test("copying an assistant selection preserves Markdown structure and links", as '```typescript\nconst answer = "yes";\n return answer;\n```\n\nAfter code.', ); + // Crossing into part of the block keeps the selected code lines as a fenced block. + await selectAssistantAcrossCodeLines(page, "not a generated link.", "return"); + await copySelection(page); + + expect((await readRichClipboard(page)).plainText).toBe( + 'not a generated link.\n\n```typescript\nconst answer = "yes";\n return\n```', + ); + + // So does starting partway through the block and leaving it. + await selectAssistantAcrossCodeLines(page, "answer", "After code."); + await copySelection(page); + + expect((await readRichClipboard(page)).plainText).toBe( + '```typescript\nanswer = "yes";\n return answer;\n```\n\nAfter code.', + ); + // Selecting a whole line runs off its end into the newline node. A trailing // newline auto-executes the line when it is pasted into a terminal. await selectAssistantAcrossCodeLines(page, "const", " "); @@ -729,13 +745,25 @@ test.describe("on a touch-capable desktop browser", () => { // The pan accepts touch only, and a mouse drag must still extend the browser's selection. test.use({ hasTouch: true }); - test("a mouse drag selects across assistant paragraphs", async ({ page }) => { + test("a mouse drag selects across assistant list, quote, and code blocks", async ({ page }) => { const agent = await seedMockAgentWorkspace({ repoPrefix: "assistant-selection-drag-", title: "Assistant selection drag", - initialPrompt: "Render three paragraphs.", + initialPrompt: "Render structured text.", featureValues: { - mockAssistantResponse: "First paragraph.\n\nSecond paragraph.\n\nThird paragraph.", + mockAssistantResponse: [ + "First paragraph.", + "", + "- List item.", + "", + "> Quote text.", + "", + "```ts", + "const answer = 1;", + "```", + "", + "Third paragraph.", + ].join("\n"), }, }); @@ -760,6 +788,9 @@ test.describe("on a touch-capable desktop browser", () => { const selected = await page.evaluate(() => window.getSelection()?.toString() ?? ""); expect(selected).toContain("First paragraph."); + expect(selected).toContain("List item."); + expect(selected).toContain("Quote text."); + expect(selected).toContain("const answer = 1;"); expect(selected).toContain("Third paragraph."); } finally { await agent.cleanup(); diff --git a/packages/app/src/assistant-selection-copy/content.browser.test.ts b/packages/app/src/assistant-selection-copy/content.browser.test.ts index 5c4fcfb33..f4e240c69 100644 --- a/packages/app/src/assistant-selection-copy/content.browser.test.ts +++ b/packages/app/src/assistant-selection-copy/content.browser.test.ts @@ -759,6 +759,56 @@ describe("assistant selection copy inside highlighted code", () => { "```typescript\nconst answer = 1;\n if (answer) {\n doThing();\n```\n\nAfter the block.", ); }); + + it("keeps line breaks when a quote selection ends partway through code", () => { + const message = mountHighlighted(); + const quote = document.createElement("div"); + quote.setAttribute("data-paseo-markdown-tag", "blockquote"); + quote.textContent = "Quoted context"; + message.querySelector('[data-paseo-markdown-tag="pre"]')!.before(quote); + + const content = copyBetween( + [tokenText(message, "Quoted context"), 0], + [tokenText(message, " (answer)"), 9], + ); + + expect(content?.plainText).toBe( + "> Quoted context\n\n```typescript\nconst answer = 1;\n if (answer)\n```", + ); + }); + + it("keeps indented code lines inside their list item", () => { + const host = document.createElement("div"); + host.innerHTML = [ + '
', + '
', + '
', + '
1.
', + '
Item text
', + highlightedCodeBlock("typescript"), + "
", + ].join(""); + document.body.append(host); + + const content = copyBetween([tokenText(host, "1."), 0], [tokenText(host, " (answer)"), 9]); + + expect(content?.plainText).toBe( + "1. Item text\n\n ```typescript\n const answer = 1;\n if (answer)\n ```", + ); + }); + + it("keeps line breaks when a selection starts partway through code", () => { + const message = mountHighlighted(); + + const content = copyBetween( + [tokenText(message, " answer"), 1], + [tokenText(message, "After the block."), "After the block.".length], + ); + + expect(content?.plainText).toBe( + "```typescript\nanswer = 1;\n if (answer) {\n doThing();\n```\n\nAfter the block.", + ); + }); }); describe("semantic library output", () => { diff --git a/packages/app/src/assistant-selection-copy/content.web.ts b/packages/app/src/assistant-selection-copy/content.web.ts index 250d60d76..58a2a8fd8 100644 --- a/packages/app/src/assistant-selection-copy/content.web.ts +++ b/packages/app/src/assistant-selection-copy/content.web.ts @@ -81,7 +81,8 @@ turndown.addRule("compactListItem", { const item = content .replace(/^\n+/, "") .replace(/\n+$/, "\n") - .replace(/\n(?=\S)/g, "\n "); + // Every non-empty line, so indented code lines stay inside the item. + .replace(/\n(?=[^\n])/g, "\n "); const parent = node.parentElement; if (parent?.nodeName !== "OL") { return `${options.bulletListMarker} ${item}\n`; @@ -359,6 +360,9 @@ function shouldPreserveSemanticElement(range: Range, element: Element): boolean if (tag === "p" || tag === "img" || isTableStructure(tag)) { return true; } + if (isCrossBoundaryCodeElement(range, element, tag)) { + return true; + } const isSelectableSemantic = tag !== null && tag !== "li" && tag !== "ol" && tag !== "ul"; if ( (isSelectableSemantic || element.tagName === "A") && @@ -386,6 +390,11 @@ function shouldPreserveSemanticElement(range: Range, element: Element): boolean return hasSelectedAllContents(range, element, tag === "li"); } +function isCrossBoundaryCodeElement(range: Range, element: Element, tag: string | null): boolean { + const isCode = tag === "pre" || (tag === "code" && Boolean(element.closest(CODE_BLOCK_SELECTOR))); + return isCode && !selectionStaysInsideElement(range, element); +} + function selectionStaysInsideElement(range: Range, element: Element): boolean { return ( range.startContainer !== element &&