Skip to content

Make copy text content respect modified clonedRoot from beforeCutCopy#3356

Merged
JiuqingSong merged 3 commits into
masterfrom
fix-copy-text-content-respect-clonedroot
May 29, 2026
Merged

Make copy text content respect modified clonedRoot from beforeCutCopy#3356
JiuqingSong merged 3 commits into
masterfrom
fix-copy-text-content-respect-clonedroot

Conversation

@JiuqingSong
Copy link
Copy Markdown
Collaborator

@JiuqingSong JiuqingSong commented May 29, 2026

Summary

https://outlookweb.visualstudio.com/Outlook%20Web/_workitems/edit/412635

When copying/cutting, getContentForCopy triggers the beforeCutCopy event, which allows handlers to mutate the cloned DOM tree and returns clonedRoot. The htmlContent clipboard payload already used this (possibly modified) clonedRoot, but the textContent payload was still derived from the original, pre-event pasteModel.

This meant any modification a beforeCutCopy handler made to clonedRoot was reflected in the HTML clipboard data but lost in the plain text clipboard data.

Fix

After the beforeCutCopy event, re-parse the (possibly modified) clonedRoot back into a content model via domToContentModel, then derive the text from that model. Round-tripping through a content model (rather than reading clonedRoot.textContent directly) preserves the existing contentModelToText formatting behavior — block separators, <hr> rendering, etc. — while now sourcing the content from the modified DOM.

Testing

  • Added a unit test that mutates clonedRoot inside the beforeCutCopy handler and asserts the resulting textContent reflects the change.
  • yarn test:fast --testPathPattern=getContentForCopy — all 7 tests pass.
  • yarn eslint — clean.

🤖 Generated with Claude Code

Build the plain text clipboard payload from the (possibly modified)
clonedRoot DOM tree returned by the beforeCutCopy event instead of the
original pre-event content model, so handler changes are reflected in
both the HTML and text content.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 29, 2026

PR Preview Action v1.8.1
Preview removed because the pull request was closed.
2026-05-29 17:33 UTC

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR updates copy/cut clipboard generation so plain text can reflect beforeCutCopy mutations to the cloned DOM, aligning it with the HTML payload behavior.

Changes:

  • Re-parses the post-event clonedRoot into a content model before generating textContent.
  • Adds a unit test covering a beforeCutCopy handler that modifies clonedRoot.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
packages/roosterjs-content-model-core/lib/command/cutCopy/getContentForCopy.ts Builds plain text from the cloned DOM after beforeCutCopy.
packages/roosterjs-content-model-core/test/command/cutCopy/getContentForCopyTest.ts Adds coverage for modified cloned root text output.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.


// Build the text content from the (possibly modified) cloned root DOM tree so that any
// changes made by beforeCutCopy event handlers are reflected in the plain text result as well
const textModel = domToContentModel(clonedRoot, createDomToModelContext());
Now that copy text content is derived from the modified clonedRoot, an
image-only selection renders to a single space (matching the onImage
callback / real non-mocked behavior) instead of an empty string.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@JiuqingSong JiuqingSong merged commit 48760de into master May 29, 2026
8 checks passed
@JiuqingSong JiuqingSong deleted the fix-copy-text-content-respect-clonedroot branch May 29, 2026 17:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants