fix(app): copy only the code when a selection touches the block beside it (#6138)

* fix(app): copy a code block without its fence when the drag starts beside it

A drag that starts or ends in the gap beside a code block anchors the
selection at the edge of the neighbouring block. Only the code is
highlighted, but the range crossed the block boundary, so the copy took
the Markdown path and wrapped the code in its fence.

A selection now crosses a code block only when it selects content
outside it. Message rows the range touches without selecting anything
are dropped, and a selection whose content all lies inside one code
block copies as that code.

* fix(app): keep a selected image when copying it beside a code block

An image selected next to a code block has no text, so the selection
counted it as empty and copied only the code. Count images as selected
content so the copy keeps the image and the fence, as it did before.

---------

Co-authored-by: paseo-bot[bot] <266920839+paseo-bot[bot]@users.noreply.github.com>
This commit is contained in:
paseo-bot[bot]
2026-10-05 19:21:46 +02:00
committed by GitHub
parent bc6ddbfa97
commit 18412604dd
3 changed files with 159 additions and 7 deletions
@@ -120,6 +120,41 @@ async function selectAssistantElement(page: Page, selector: string): Promise<voi
});
}
/**
* Select an element from the end of the text before it. A drag that starts in the gap
* above a block anchors there, so nothing outside the element is highlighted.
*/
async function selectAssistantElementFromEndOf(
page: Page,
startText: string,
selector: string,
): Promise<void> {
await assistantMessageBlocks(page).evaluateAll(
(blocks, selected) => {
let startNode: Text | null = null;
for (const block of blocks) {
const walker = document.createTreeWalker(block, NodeFilter.SHOW_TEXT);
while (walker.nextNode()) {
if (walker.currentNode.textContent?.endsWith(selected.startText)) {
startNode = walker.currentNode as Text;
}
}
}
const end = blocks.map((block) => block.querySelector(selected.selector)).find(Boolean);
if (!startNode || !end) {
throw new Error(`Could not find ${selected.startText} — ${selected.selector}`);
}
const range = document.createRange();
range.setStart(startNode, startNode.length);
range.setEnd(end, end.childNodes.length);
const selection = window.getSelection();
selection?.removeAllRanges();
selection?.addRange(range);
},
{ startText, selector },
);
}
async function selectAssistantText(page: Page, text: string): Promise<void> {
await selectAssistantTextRange(page, text, text);
}
@@ -565,6 +600,19 @@ test("copying an assistant selection preserves Markdown structure and links", as
const completeCodeClipboard = await readRichClipboard(page);
expect(completeCodeClipboard.plainText).toBe('const answer = "yes";\n return answer;');
// Touching the block before it without selecting any of its text is still a
// selection inside the code.
await selectAssistantElementFromEndOf(
page,
"not a generated link.",
'[data-paseo-markdown-language="typescript"] [data-paseo-markdown-tag="code"]',
);
await copySelection(page);
expect((await readRichClipboard(page)).plainText).toBe(
'const answer = "yes";\n return answer;',
);
// Crossing the block boundary expresses an intent to carry its Markdown structure.
await selectAssistantAcrossCodeLines(page, "const", "After code.");
await copySelection(page);
@@ -405,10 +405,19 @@ describe("assistant selection copy ranges", () => {
* Turndown, so those cases need a fixture shaped like the real thing.
*/
function highlightedFixture(language: string | null): string {
return [
'<div data-testid="assistant-message">',
'<div data-paseo-markdown-tag="p"><span>Before the block.</span></div>',
highlightedCodeBlock(language),
'<div data-paseo-markdown-tag="p"><span>After the block.</span></div>',
"</div>",
].join("");
}
function highlightedCodeBlock(language: string | null): string {
const languageAttribute =
language === null ? "" : ` data-paseo-markdown-language="${escapeAttribute(language)}"`;
return [
'<div data-testid="assistant-message">',
`<div data-paseo-markdown-tag="pre"${languageAttribute}>`,
'<span data-paseo-markdown-tag="code">',
"<span>const</span><span> answer</span><span> = 1;</span>",
@@ -419,8 +428,6 @@ function highlightedFixture(language: string | null): string {
"</span>",
'<div data-paseo-markdown-ignore="true"><span>Copy</span></div>',
"</div>",
'<div data-paseo-markdown-tag="p"><span>After the block.</span></div>',
"</div>",
].join("");
}
@@ -598,6 +605,63 @@ describe("assistant selection copy inside highlighted code", () => {
);
});
// A drag that starts or ends in the gap beside a block anchors the selection at the
// edge of the neighbouring block. Nothing outside the code is highlighted or selected.
it("copies code without a fence when the selection only touches the end of the block before it", () => {
const message = mountHighlighted();
const before = tokenText(message, "Before the block.");
const content = copyBetween(
[before, before.length],
[tokenText(message, " doThing();"), 14],
);
expect(content?.plainText).toBe("const answer = 1;\n if (answer) {\n doThing();");
});
it("copies code without a fence when the selection only touches the start of the block after it", () => {
const message = mountHighlighted();
const content = copyBetween(
[tokenText(message, "const"), 0],
[tokenText(message, "After the block."), 0],
);
expect(content?.plainText).toBe("const answer = 1;\n if (answer) {\n doThing();");
});
it("copies code without a fence when the selection only touches the row before the block", () => {
const transcript = mountTranscript([{ messageId: "message-1", blocks: ["Run this:", "code"] }]);
const codeRow = transcript.querySelectorAll('[data-testid="assistant-message"]')[1]!;
codeRow.innerHTML = highlightedCodeBlock("typescript");
const before = tokenText(transcript, "Run this:");
const content = copyBetween(
[before, before.length],
[tokenText(transcript, " doThing();"), 14],
);
expect(content?.plainText).toBe("const answer = 1;\n if (answer) {\n doThing();");
});
it("keeps an image selected beside the code in the copy", () => {
const message = mountHighlighted();
const image = document.createElement("div");
image.setAttribute("data-paseo-markdown-tag", "p");
image.innerHTML = '<div><img src="https://example.test/chart.png" alt="chart"></div>';
message.querySelector('[data-paseo-markdown-tag="pre"]')!.before(image);
const before = tokenText(message, "Before the block.");
const content = copyBetween(
[before, before.length],
[tokenText(message, " doThing();"), 14],
);
expect(content?.plainText).toBe(
"![chart](https://example.test/chart.png)\n\n```typescript\nconst answer = 1;\n if (answer) {\n doThing();\n```",
);
});
it("retains the fence when a complete code block is selected across its boundary", () => {
const message = mountHighlighted();
@@ -67,8 +67,8 @@ export function createAssistantSelectionClipboardContent(
}
const range = selection.getRangeAt(0);
const parts = selectedMessageParts(range);
if (!parts) {
const parts = selectedMessageParts(range)?.filter((part) => selectsContent(part.range));
if (!parts?.length) {
return null;
}
@@ -103,10 +103,12 @@ export function createAssistantSelectionClipboardContent(
*
* A selection contained inside code always copies as code, even when it contains every
* character. A selection that crosses the code boundary stays on the Markdown path so
* a complete block retains its fence.
* a complete block retains its fence. Crossing means selecting content outside the code:
* a drag that starts or ends in the gap beside a block anchors at the edge of the
* neighbouring block, which selects nothing there.
*/
function createPartialCodeContent(range: Range, message: Element): MarkdownClipboardContent | null {
const region = closestCodeRegion(range.commonAncestorContainer, message);
const region = selectedCodeRegion(range, message);
if (!region) {
return null;
}
@@ -130,6 +132,18 @@ function createPartialCodeContent(range: Range, message: Element): MarkdownClipb
});
}
function selectedCodeRegion(range: Range, message: Element): Element | null {
const edgeRegions = [range.startContainer, range.endContainer].map((node) =>
closestCodeRegion(node, message),
);
return (
edgeRegions.find(
// Measure against the whole block, so its own hover Copy button is not outside it.
(edge) => edge && selectsNothingOutside(range, edge.closest(CODE_BLOCK_SELECTOR) ?? edge),
) ?? null
);
}
function closestCodeRegion(node: Node, message: Element): Element | null {
const element = node instanceof Element ? node : node.parentElement;
const region = element?.closest(CODE_REGION_SELECTOR) ?? null;
@@ -384,6 +398,31 @@ function hasSelectedAllContents(range: Range, element: Element, includeIgnored =
return true;
}
function selectsContent(range: Range): boolean {
return hasMarkdownContent(range.cloneContents(), true);
}
function selectsNothingOutside(range: Range, element: Element): boolean {
const contents = document.createRange();
contents.selectNodeContents(element);
if (range.compareBoundaryPoints(Range.START_TO_START, contents) < 0) {
const before = range.cloneRange();
before.setEnd(contents.startContainer, contents.startOffset);
if (selectsContent(before)) {
return false;
}
}
if (range.compareBoundaryPoints(Range.END_TO_END, contents) > 0) {
const after = range.cloneRange();
after.setStart(contents.endContainer, contents.endOffset);
if (selectsContent(after)) {
return false;
}
}
return true;
}
function hasMarkdownContent(fragment: DocumentFragment, includeIgnored: boolean): boolean {
if (!includeIgnored) {
for (const ignored of fragment.querySelectorAll(`[${MARKDOWN_COPY_IGNORE_ATTRIBUTE}]`)) {
@@ -395,6 +434,7 @@ function hasMarkdownContent(fragment: DocumentFragment, includeIgnored: boolean)
}
const visibleVoidSelector = ["br", "hr"]
.map((tag) => `[${MARKDOWN_COPY_TAG_ATTRIBUTE}="${tag}"]`)
.concat("img")
.join(",");
return Boolean(fragment.querySelector(visibleVoidSelector));
}