@devries-cameron20 / t3code Mirror · read-only / commits / 093c317858
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 13:30 UTC

fix(web): copying from a later list item keeps its numbers (#36915)

Co-authored-by: Julius Marminge <julius0216@outlook.com>

Wout Stiens committed Nov 14, 2026 at 21:55 UTC 093c317858f89e93899a9d3e34738a153295cc3e
3 files changed +138 -7
apps/web/src/components/ChatMarkdown.browser.test.tsx
+47
@@ -712,6 +712,53 @@ describe("ChatMarkdown copy", () => {
712 selection.addRange(range);
713 expect(chatMarkdownClipboardPayload(selection)?.text).toBe("items[0]*_");
714 });
715 +
716 + it.each([
717 + {
718 + source: "10. First\n11. Second\n12. Third\n\nAfter.",
719 + from: "Second",
720 + text: "11. Second\n12. Third\n\nAfter.",
721 + },
722 + {
723 + source: "1. A\n2. B\n7. C\n8. D\n\nAfter.",
724 + from: "D",
725 + userMessage: true,
726 + text: "8. D\n\nAfter.",
727 + },
728 + {
729 + source: "1. Outer\n2. Next\n 1. Inner\n 2. Deeper\n\nAfter.",
730 + from: "Deeper",
731 + text: "2. 2. Deeper\n\nAfter.",
732 + },
733 + ])(
734 + "keeps the shown numbers when a copy starts at $from and ends past the list",
735 + async ({ source, from, userMessage, text }) => {
736 + setup();
737 + await using screen = await renderText(
738 + source,
739 + userMessage ? { extraRemarkPlugins: SOURCE_NUMBER_PLUGINS } : {},
740 + );
741 + const start = [...screen.container.querySelectorAll("li")]
742 + .flatMap((item) => [...item.childNodes])
743 + .find((node) => node.nodeType === Node.TEXT_NODE && node.textContent === from)!;
744 + const after = [...screen.container.querySelectorAll("p")].at(-1)!.firstChild!;
745 + const range = document.createRange();
746 + range.setStart(start, 0);
747 + range.setEnd(after, after.textContent!.length);
748 + const selection = window.getSelection()!;
749 + selection.removeAllRanges();
750 + selection.addRange(range);
751 + const payload = chatMarkdownClipboardPayload(selection);
752 + expect(payload?.text).toBe(text);
753 + // Rich paste numbers its list from the same start.
754 + expect(
755 + new DOMParser()
756 + .parseFromString(payload!.html, "text/html")
757 + .querySelector("li")
758 + ?.closest("ol")?.start,
759 + ).toBe(Number(text.split(".")[0]));
760 + },
761 + );
762 });
763
764 describe("ChatMarkdown file chip tooltips", () => {
apps/web/src/markdown-clipboard.ts
+45 -7
@@ -152,17 +152,24 @@ function serializeListItem(
152 ].join("\n");
153 }
154
155 -function serializeList(list: Element, ordered: boolean, inTableCell: boolean): string {
155 +/** A list's items with the numbers an ordered list shows: from `start`, reset by each `value`. */
156 +function numberedListItems(list: Element): { item: Element; number: number }[] {
157 const start = Number.parseInt(list.getAttribute("start") ?? "1", 10);
157 - const items = [...list.children].filter((child) => child.tagName === "LI");
158 - if (items.length === 0) return "";
158 let next = Number.isNaN(start) ? 1 : start;
160 - return `${items
159 + return [...list.children]
160 + .filter((child) => child.tagName === "LI")
161 .map((item) => {
162 const value = Number.parseInt(item.getAttribute("value") ?? "", 10);
163 - if (ordered && !Number.isNaN(value)) next = value;
164 - return serializeListItem(item, ordered, next++, inTableCell);
165 - })
163 + if (!Number.isNaN(value)) next = value;
164 + return { item, number: next++ };
165 + });
166 +}
167 +
168 +function serializeList(list: Element, ordered: boolean, inTableCell: boolean): string {
169 + const items = numberedListItems(list);
170 + if (items.length === 0) return "";
171 + return `${items
172 + .map(({ item, number }) => serializeListItem(item, ordered, number, inTableCell))
173 .join("\n")}\n\n`;
174 }
175
@@ -497,6 +504,36 @@ function sanitizedHtmlFrom(container: Element): string {
504 return `<meta charset="utf-8">${container.innerHTML}`;
505 }
506
507 +/** A list cloned from a later item would renumber from its start; restart it at the shown number. */
508 +function keepStartListNumbers(range: Range, container: Element): void {
509 + // The start's ancestors below the common ancestor clone as the fragment's first-child chain.
510 + const chain: Node[] = [];
511 + for (
512 + let node: Node | null = range.startContainer;
513 + node && node !== range.commonAncestorContainer;
514 + node = node.parentNode
515 + ) {
516 + chain.unshift(node);
517 + }
518 + let clone: Node | null = container;
519 + for (const [depth, source] of chain.entries()) {
520 + clone = clone?.firstChild ?? null;
521 + if (!(clone instanceof Element) || !(source instanceof Element) || source.tagName !== "OL")
522 + continue;
523 + const firstCopied = chain[depth + 1] ?? source.childNodes[range.startOffset];
524 + if (!firstCopied) continue;
525 + const shown = numberedListItems(source).find(
526 + ({ item }) =>
527 + item === firstCopied ||
528 + firstCopied.compareDocumentPosition(item) & Node.DOCUMENT_POSITION_FOLLOWING,
529 + );
530 + const cloned = numberedListItems(clone)[0];
531 + if (shown && cloned && shown.number !== cloned.number) {
532 + clone.setAttribute("start", String(shown.number));
533 + }
534 + }
535 +}
536 +
537 export function chatMarkdownClipboardPayload(
538 selection: Selection,
539 ): MarkdownClipboardPayload | null {
@@ -507,6 +544,7 @@ export function chatMarkdownClipboardPayload(
544 if (range.collapsed) continue;
545 const container = document.createElement("div");
546 container.appendChild(range.cloneContents());
547 + keepStartListNumbers(range, container);
548 const ancestor = range.commonAncestorContainer;
549 const ancestorElement =
550 ancestor.nodeType === Node.ELEMENT_NODE ? (ancestor as Element) : ancestor.parentElement;
scripts/scenarios/copied-list-tail-keeps-its-numbers.e2e.ts new
+46
@@ -0,0 +1,46 @@
1 +/// <reference lib="dom" />
2 +// The DOM lib types the function this scenario runs in the page.
3 +import { expect } from "e2e";
4 +
5 +import { test, WEB } from "./t3.ts";
6 +
7 +test(
8 + "A copy that starts at a later list item and ends past the list keeps the shown numbers",
9 + WEB,
10 + async ({ t3, app, browser, screen }) => {
11 + const project = await t3.project("demo");
12 + const thread = await t3.thread({
13 + project,
14 + title: "Steps",
15 + messages: ["Steps:\n\n1. Install\n2. Configure\n3. Deploy\n\nThen report back."],
16 + });
17 + await t3.open(thread);
18 + // A message sent through the CLI shows collapsed under its sender.
19 + await screen.getByRole("button", /^Agent Steps/).tap();
20 + await expect(screen.getByText("Configure")).toBeVisible();
21 +
22 + // Selects from a list item's text to the end of the paragraph after its list.
23 + await browser.evaluate(() => {
24 + const item = [...document.querySelectorAll("li")].find(
25 + (candidate) => candidate.textContent === "Configure",
26 + );
27 + const after = item?.closest("ol")?.nextElementSibling?.firstChild;
28 + if (!item?.firstChild || !after) throw new Error("No rendered list item Configure");
29 + const range = document.createRange();
30 + range.setStart(item.firstChild, 0);
31 + range.setEnd(after, after.textContent?.length ?? 0);
32 + window.getSelection()?.removeAllRanges();
33 + window.getSelection()?.addRange(range);
34 + return null;
35 + });
36 + await browser.keyboard.press("ControlOrMeta+C");
37 + await screen.getByRole("textbox").tap();
38 + await browser.keyboard.press("ControlOrMeta+V");
39 +
40 + // The composer parses the pasted Markdown, so the copied number becomes the list's start.
41 + const pasted = browser.locator('[role="textbox"] ol');
42 + await expect(pasted).toHaveText(/Configure\s*Deploy/);
43 + await app.screenshot("pasted");
44 + await expect(pasted).toHaveAttribute("start", "2");
45 + },
46 +);