Bug: [lexical-plain-text][lexical-rich-text] A copy, cut or paste inside a decorator's input acts on the editor's selection
Lexical version: 0.52.0 (the current latest), Chromium 153 on Linux. The code is unchanged on main at f0fefed.
Steps To Reproduce
- Run the code below: a plain-text editor holding a decorator that renders an
<input>. - Select
helloin the editor's text. - Click into the field and select
field. - Press Ctrl+C (Cmd+C on a Mac), then paste into any other text box.
- Do steps 2 and 3 again, then press Ctrl+X.
- Do steps 2 and 3 again, with some text on the clipboard, then press Ctrl+V.
Link to code example:
import {registerPlainText} from '@lexical/plain-text';
import {
$createParagraphNode,
$createTextNode,
$getRoot,
createEditor,
DecoratorNode,
} from 'lexical';
// An inline decorator whose DOM holds a text field of its own.
class FieldNode extends DecoratorNode {
static getType() {
return 'field';
}
static clone(node) {
return new FieldNode(node.__key);
}
static importJSON() {
return new FieldNode();
}
createDOM() {
const span = document.createElement('span');
const input = document.createElement('input');
input.value = 'field text';
span.append(input);
return span;
}
updateDOM() {
return false;
}
decorate() {
return null;
}
isInline() {
return true;
}
}
const root = document.createElement('div');
root.contentEditable = 'true';
document.body.append(root);
const editor = createEditor({
namespace: 'repro',
nodes: [FieldNode],
onError: (error) => {
throw error;
},
});
editor.setRootElement(root);
registerPlainText(editor);
editor.update(() => {
$getRoot().append(
$createParagraphNode().append($createTextNode('hello world '), new FieldNode()),
);
});The current behavior
- Step 4 pastes
hello. The copy is prevented, and the clipboard gets the editor's selection instead of the field's. - Step 5 deletes
hellofrom the editor, and the field keepsfield text. The cut is prevented, and Lexical cuts the editor's selection instead. - Step 6 writes the clipboard's text into the editor over
hello, and nothing goes into the field.
Nothing on screen shows that the editor still holds hello: the browser's selection is in the field.
In a rich-text editor, copy and paste leave the field alone. A cut does half of the same: the field cuts field itself, and hello is deleted from the editor as well.
These are all six cases, measured with real key presses in Chromium, with hello selected in the editor and field selected in the field:
| plain text | rich text | |
|---|---|---|
| Ctrl+C | prevented; the clipboard gets hello |
the field's own copy |
| Ctrl+X | prevented; hello is deleted from the editor; the field is unchanged |
the field's own cut, and hello is deleted from the editor |
| Ctrl+V | prevented; the text is written into the editor over hello; the field is unchanged |
the field's own paste |
Why:
- A copy, cut or paste in the field bubbles to the root element. Its listeners dispatch
COPY_COMMAND,CUT_COMMANDandPASTE_COMMANDwhatever the target is (LexicalEvents.ts#L2094-L2116). - When the field took the focus, the selection-change handler returned early (LexicalEvents.ts#L371-L373), since
isSelectionWithinEditoris false while a decorator's input has the focus (LexicalUtils.ts#L223-L289). So$getSelection()still returns the range the user left in the editor. - Plain text's listeners check only that the selection is a range (index.ts#L384-L425). Then:
- Rich text is guarded in two of the three places:
- its copy goes through
$copyToClipboardEvent, which returns without touching the event when the DOM selection isn't the editor's (clipboard.ts#L815-L824); - its paste listener checks
$isSelectionCapturedInDecoratorInput(event.target)(index.ts#L2001-L2007); - but
onCutForRichTextremoves the selection aftercopyToClipboard, whatever that returned (index.ts#L597-L616).
- its copy goes through
#3717 is related: copy and cut from a field in the playground's poll used to throw.
The expected behavior
A copy, cut or paste inside a decorator's own input belongs to the input. Lexical should leave the event to the browser, and touch neither the editor's selection nor the document.
Rich text's paste listener already does this. The same check in plain text's three listeners and in rich text's cut listener fixes every case above. I checked it against patched copies of both packages' 0.52.0 builds:
- each of the six cases then goes to the field, and a copy in the field leaves
fieldon the clipboard; - the editor keeps
hello world; - a copy, cut or paste in the editor's own text behaves as before.
--- a/packages/lexical-plain-text/src/index.ts
+++ b/packages/lexical-plain-text/src/index.ts
@@ import {
$getSelection,
$getSlotFrame,
$isRangeSelection,
+ $isSelectionCapturedInDecoratorInput,
$selectAll,
@@
IS_IOS,
IS_SAFARI,
+ isDOMNode,
KEY_ARROW_LEFT_COMMAND,
@@
+/** Whether a clipboard event comes from a decorator's own input, which the browser serves. */
+function $isFromDecoratorInput(
+ event: ClipboardEvent | KeyboardEvent | InputEvent | null,
+): boolean {
+ return (
+ event !== null &&
+ isDOMNode(event.target) &&
+ $isSelectionCapturedInDecoratorInput(event.target)
+ );
+}
+
function onCopyForPlainText(
@@ (the COPY_COMMAND, CUT_COMMAND and PASTE_COMMAND listeners alike)
const selection = $getSelection();
- if (!$isRangeSelection(selection)) {
+ if (!$isRangeSelection(selection) || $isFromDecoratorInput(event)) {
return false;
}--- a/packages/lexical-rich-text/src/index.ts
+++ b/packages/lexical-rich-text/src/index.ts
@@ export function registerRichText(editor: LexicalEditor): () => void {
editor.registerCommand(
CUT_COMMAND,
event => {
+ if (
+ event !== null &&
+ isDOMNode(event.target) &&
+ $isSelectionCapturedInDecoratorInput(event.target)
+ ) {
+ return false;
+ }
onCutForRichText(event, editor);
return true;
},There's also a core-level option, which I haven't checked. The root's clipboard listeners could skip an event whose target is in a subtree that isDOMCapturingSelection reports. #8740 does this for beforeinput and input, and it would cover clipboard listeners outside these two packages too.
Impact of fix
It affects any editor whose decorators render an input or textarea of their own, such as a caption, a form, or the source field of an inline math node.
- In plain text, copying from the field copies the wrong text. Cutting silently deletes document text the user no longer sees as selected, and pasting writes into the document instead of the field.
- In rich text, the cut deletes that text as well.
Source: facebook/lexical