Skip to content

webview: preserve text selection before clipboard commands - #344428

Merged
Dmitriy Vasyura (dmitrivMS) merged 1 commit into
mainfrom
agents/fix-vscode-issue-343489
Oct 26, 2026
Merged

Dmitriy Vasyura (dmitrivMS) merged 1 commit into
mainfrom
agents/fix-vscode-issue-343489

Conversation

@dmitrivMS

@dmitrivMS Dmitriy Vasyura (dmitrivMS) commented Oct 26, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #343489.

The focus restoration introduced by #341552 can refocus a previously used textarea before Copy runs, clearing a selection in non-focusable rendered webview content.

  • Skip focus restoration while a non-collapsed document selection exists; retain restoration for empty or collapsed selections.
  • Update the preload script CSP hash.
  • Add regression coverage for Copy, Cut, and Paste with body/document-element focus, and preserve existing input-focus behavior.

Avoid restoring the last focused input when a non-collapsed document selection exists, so copying rendered webview text does not clear the selection. Preserve focus restoration for empty and collapsed selections.

Fixes #343489

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings October 26, 2026 21:06
@dmitrivMS Dmitriy Vasyura (dmitrivMS) added the webview Webview issues label Oct 26, 2026
@dmitrivMS Dmitriy Vasyura (dmitrivMS) added the ~release-cherry-pick Trigger: cherry-pick this PR to the latest release branch label Oct 26, 2026
@vs-code-engineering

Copy link
Copy Markdown
Contributor

This PR will be automatically cherry-picked to release/1.144 when merged.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The narrow fix addresses selection loss, preserves existing focus safeguards, and includes targeted regression coverage with no unresolved findings.

No open issues 🎉

What changed in this PR

Fixes the webview clipboard regression introduced by #341552 by preserving selected rendered text instead of refocusing a previously used input.

Changes:

  • Skip focus restoration for non-collapsed document selections while retaining existing focus safeguards.
  • Update the preload script’s Content Security Policy hash.
  • Add Copy, Cut, and Paste regression coverage for body/document-element focus, plus collapsed-selection coverage.
File Description
src/​vs/​workbench/​contrib/​webview/​test/​node/​webviewShellMessaging.test.ts Tests selection preservation and retained focus restoration.
src/​vs/​workbench/​contrib/​webview/​browser/​pre/​index.html Guards focus restoration and updates the script hash.

Review effort: Balanced

@dmitrivMS
Dmitriy Vasyura (dmitrivMS) marked this pull request as ready for review October 26, 2026 21:35
@dmitrivMS
Dmitriy Vasyura (dmitrivMS) merged commit b0f2816 into main Oct 26, 2026
88 of 90 checks passed
@dmitrivMS
Dmitriy Vasyura (dmitrivMS) deleted the agents/fix-vscode-issue-343489 branch October 26, 2026 21:38
@vs-code-engineering vs-code-engineering Bot added this to the 1.144.0 milestone Oct 26, 2026
@vs-code-engineering vs-code-engineering Bot added release-cherry-pick Automated cherry-pick between release and main branches and removed ~release-cherry-pick Trigger: cherry-pick this PR to the latest release branch labels Oct 26, 2026
Supan Adit Pratama (supanadit) added a commit to supanadit/morse that referenced this pull request Oct 31, 2026
VS Code 1.143 restores focus to the webview's last focused element right
before running the clipboard command. When that element is the composer
textarea, focusing it collapses a selection made in the transcript, so
Ctrl+C and the context-menu Copy both copy nothing.

Land plain pointer presses on the non-editable shell instead, so the
webview's activeElement is not body and the restore is skipped. The
guard only runs in a webview; the browser host keeps its focus behavior.

Upstream fix is in VS Code 1.144 (microsoft/vscode#344428); this keeps
1.143 usable.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release-cherry-pick Automated cherry-pick between release and main branches webview Webview issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cmd+C copy shortcut freezes

3 participants