Make copy text content respect modified clonedRoot from beforeCutCopy#3356
Merged
Conversation
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>
|
Contributor
There was a problem hiding this comment.
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
clonedRootinto a content model before generatingtextContent. - Adds a unit test covering a
beforeCutCopyhandler that modifiesclonedRoot.
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>
BryanValverdeU
approved these changes
May 29, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
https://outlookweb.visualstudio.com/Outlook%20Web/_workitems/edit/412635
When copying/cutting,
getContentForCopytriggers thebeforeCutCopyevent, which allows handlers to mutate the cloned DOM tree and returnsclonedRoot. ThehtmlContentclipboard payload already used this (possibly modified)clonedRoot, but thetextContentpayload was still derived from the original, pre-eventpasteModel.This meant any modification a
beforeCutCopyhandler made toclonedRootwas reflected in the HTML clipboard data but lost in the plain text clipboard data.Fix
After the
beforeCutCopyevent, re-parse the (possibly modified)clonedRootback into a content model viadomToContentModel, then derive the text from that model. Round-tripping through a content model (rather than readingclonedRoot.textContentdirectly) preserves the existingcontentModelToTextformatting behavior — block separators,<hr>rendering, etc. — while now sourcing the content from the modified DOM.Testing
clonedRootinside thebeforeCutCopyhandler and asserts the resultingtextContentreflects the change.yarn test:fast --testPathPattern=getContentForCopy— all 7 tests pass.yarn eslint— clean.🤖 Generated with Claude Code