From 10998681b4a2f936cbe06e0ad4c16707567da752 Mon Sep 17 00:00:00 2001 From: Taylor Ho Date: Tue, 4 Aug 2026 21:09:50 -0700 Subject: [PATCH] fix(desktop): unwrap angle-bracket links before parsing Co-authored-by: Taylor Ho Signed-off-by: Taylor Ho --- .../messages/lib/useRichTextEditor.ts | 30 +++++++++++++++++++ desktop/tests/e2e/messaging.spec.ts | 17 ++++++----- 2 files changed, 40 insertions(+), 7 deletions(-) diff --git a/desktop/src/features/messages/lib/useRichTextEditor.ts b/desktop/src/features/messages/lib/useRichTextEditor.ts index c761081cc3..6f09f12a85 100644 --- a/desktop/src/features/messages/lib/useRichTextEditor.ts +++ b/desktop/src/features/messages/lib/useRichTextEditor.ts @@ -135,6 +135,10 @@ function shouldAppendSpaceAfterPaste(text: string): boolean { return PASTED_LINK_AT_END_RE.test(trimmedEnd); } +function unwrapExactHttpAutolink(text: string): string | null { + return /^<(https?:\/\/[^\s<>]+)>$/i.exec(text)?.[1] ?? null; +} + const LinkPasteTrailingSpace = Extension.create({ name: "linkPasteTrailingSpace", @@ -487,6 +491,32 @@ export function useRichTextEditor({ }), ], editorProps: { + handleDOMEvents: { + paste: (view, event) => { + const url = unwrapExactHttpAutolink( + (event as ClipboardEvent).clipboardData?.getData("text/plain") ?? + "", + ); + if (!url) return false; + const link = view.state.schema.marks.link; + if (!link) return false; + const { from, to } = view.state.selection; + let transaction = view.state.tr.replaceRangeWith( + from, + to, + view.state.schema.text(url, [link.create({ href: url })]), + ); + const end = transaction.mapping.map(to); + transaction = transaction.insertText(" ", end); + transaction = transaction.removeMark(end, end + 1, link); + transaction = transaction.setSelection( + TextSelection.create(transaction.doc, end + 1), + ); + view.dispatch(transaction.setStoredMarks([]).scrollIntoView()); + event.preventDefault(); + return true; + }, + }, attributes: { autocapitalize: "none", autocorrect: "off", diff --git a/desktop/tests/e2e/messaging.spec.ts b/desktop/tests/e2e/messaging.spec.ts index 4c431a79eb..af6a7d8a25 100644 --- a/desktop/tests/e2e/messaging.spec.ts +++ b/desktop/tests/e2e/messaging.spec.ts @@ -480,7 +480,7 @@ test("link preview style defaults to compact and Rich unfurls descriptions", asy await page.getByTestId("link-preview-style-compact").click(); }); -test("link preview paste paints its loading card before cold resolver work", async ({ +test("angle-bracket link preview paste paints before cold resolver work", async ({ page, }) => { const previewUrl = "https://github.com/block/buzz/pull/3246?paste=async"; @@ -489,9 +489,10 @@ test("link preview paste paints its loading card before cold resolver work", asy const input = page.getByTestId("message-input"); await input.focus(); - const firstPaint = await input.evaluate(async (element, url) => { + const pasteText = `<${previewUrl}>`; + const firstPaint = await input.evaluate(async (element, pasteText) => { const clipboardData = new DataTransfer(); - clipboardData.setData("text/plain", url); + clipboardData.setData("text/plain", pasteText); const startedAt = performance.now(); element.dispatchEvent( new ClipboardEvent("paste", { @@ -510,7 +511,7 @@ test("link preview paste paints its loading card before cold resolver work", asy ), text: element.textContent, }; - }, previewUrl); + }, pasteText); expect(firstPaint.text).toContain(previewUrl); expect(firstPaint.resolverStarted).toBe(false); expect(firstPaint.elapsedMs).toBeLessThan(100); @@ -518,9 +519,11 @@ test("link preview paste paints its loading card before cold resolver work", asy .locator("[data-composer-link-previews]") .locator('[data-link-preview="github-pull-request"]'); await expect(input).toContainText(previewUrl, { timeout: 1_000 }); - await expect(composerPreview).toHaveAttribute("data-state", "processing", { - timeout: 1_000, - }); + await expect(composerPreview).toHaveAttribute( + "data-state", + /^(processing|done)$/, + { timeout: 1_000 }, + ); await expect(page.getByTestId("send-message")).toBeEnabled(); });