Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,250 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/

import {LexicalComposer} from '@lexical/react/LexicalComposer';
import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext';
import {ContentEditable} from '@lexical/react/LexicalContentEditable';
import {RichTextPlugin} from '@lexical/react/LexicalRichTextPlugin';
import {
COMMAND_PRIORITY_CRITICAL,
type LexicalEditor,
PASTE_COMMAND,
type PasteCommandType,
} from 'lexical';
import * as React from 'react';
import {act} from 'react';
import {createRoot, type Root} from 'react-dom/client';
import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest';

import ContextMenuPlugin from '../../src/plugins/ContextMenuPlugin';

describe('ContextMenuPlugin Clipboard', () => {
let container: HTMLDivElement;
let reactRoot: Root;
let editor: LexicalEditor;

function CaptureEditor() {
const [contextEditor] = useLexicalComposerContext();
editor = contextEditor;
return null;
}

beforeEach(() => {
container = document.createElement('div');
document.body.appendChild(container);
reactRoot = createRoot(container);
vi.stubGlobal('alert', vi.fn());
});

afterEach(async () => {
await act(async () => {
reactRoot.unmount();
});
container.remove();
vi.restoreAllMocks();
vi.unstubAllGlobals();
});

async function renderPlugin(): Promise<void> {
await act(async () => {
reactRoot.render(
<LexicalComposer
initialConfig={{
namespace: 'context-menu-test',
onError: (error: Error) => {
throw error;
},
}}>
<CaptureEditor />
<RichTextPlugin
contentEditable={<ContentEditable />}
ErrorBoundary={({children}) => <>{children}</>}
/>
<ContextMenuPlugin shouldPreserveNewLinesInMarkdown={false} />
</LexicalComposer>,
);
});
}

async function openContextMenu(): Promise<void> {
const rootElement = container.querySelector(
'[contenteditable="true"]',
) as HTMLElement;
const event = new MouseEvent('contextmenu', {
bubbles: true,
cancelable: true,
clientX: 50,
clientY: 50,
});
await act(async () => {
rootElement.dispatchEvent(event);
});
}

function getMenuItem(text: string): HTMLElement | null {
const items = Array.from(
document.querySelectorAll('.PlaygroundEditorTheme__contextMenuItem'),
);
return (
(items.find(item => item.textContent?.includes(text)) as HTMLElement) ??
null
);
}

it('pastes successfully when navigator.permissions.query throws (e.g. Firefox)', async () => {
const mockPasteHandler = vi.fn((_event: PasteCommandType) => true);

const mockClipboardItem = {
getType: vi.fn(
async () => new Blob(['hello world'], {type: 'text/plain'}),
),
types: ['text/plain'],
};

vi.stubGlobal('navigator', {
clipboard: {
read: vi.fn(async () => [mockClipboardItem]),
readText: vi.fn(async () => 'hello world'),
},
permissions: {
query: vi.fn(async () => {
throw new TypeError(
"'clipboard-read' (value of 'name' member of PermissionDescriptor) is not a valid PermissionName.",
);
}),
},
});

await renderPlugin();
editor.registerCommand(
PASTE_COMMAND,
mockPasteHandler,
COMMAND_PRIORITY_CRITICAL,
);

await openContextMenu();
const pasteOption = getMenuItem('Paste');
expect(pasteOption).not.toBeNull();

await act(async () => {
pasteOption!.click();
});

expect(mockPasteHandler).toHaveBeenCalledTimes(1);
const dispatchedEvent = mockPasteHandler.mock.calls[0][0] as ClipboardEvent;
expect(dispatchedEvent.clipboardData?.getData('text/plain')).toBe(
'hello world',
);
expect(alert).not.toHaveBeenCalled();
});

it('pastes plain text successfully when navigator.permissions.query throws (e.g. Firefox)', async () => {
const mockPasteHandler = vi.fn((_event: PasteCommandType) => true);

vi.stubGlobal('navigator', {
clipboard: {
readText: vi.fn(async () => 'plain text content'),
},
permissions: {
query: vi.fn(async () => {
throw new TypeError('unsupported PermissionName');
}),
},
});

await renderPlugin();
editor.registerCommand(
PASTE_COMMAND,
mockPasteHandler,
COMMAND_PRIORITY_CRITICAL,
);

await openContextMenu();
const pastePlainTextOption = getMenuItem('Paste as Plain Text');
expect(pastePlainTextOption).not.toBeNull();

await act(async () => {
pastePlainTextOption!.click();
});

expect(mockPasteHandler).toHaveBeenCalledTimes(1);
const dispatchedEvent = mockPasteHandler.mock.calls[0][0] as ClipboardEvent;
expect(dispatchedEvent.clipboardData?.getData('text/plain')).toBe(
'plain text content',
);
expect(alert).not.toHaveBeenCalled();
});

it('shows alert and blocks paste when clipboard-read permission is denied', async () => {
const mockPasteHandler = vi.fn((_event: PasteCommandType) => true);

vi.stubGlobal('navigator', {
clipboard: {
read: vi.fn(),
},
permissions: {
query: vi.fn(async () => ({state: 'denied'})),
},
});

await renderPlugin();
editor.registerCommand(
PASTE_COMMAND,
mockPasteHandler,
COMMAND_PRIORITY_CRITICAL,
);

await openContextMenu();
const pasteOption = getMenuItem('Paste');
expect(pasteOption).not.toBeNull();

await act(async () => {
pasteOption!.click();
});

expect(alert).toHaveBeenCalledWith('Not allowed to paste from clipboard.');
expect(mockPasteHandler).not.toHaveBeenCalled();
});

it('handles NotAllowedError when clipboard.read() rejects with permission denial', async () => {
const mockPasteHandler = vi.fn((_event: PasteCommandType) => true);

vi.stubGlobal('navigator', {
clipboard: {
read: vi.fn(async () => {
const err = new Error('The request is not allowed');
err.name = 'NotAllowedError';
throw err;
}),
},
permissions: {
query: vi.fn(async () => {
throw new TypeError('unsupported');
}),
},
});

await renderPlugin();
editor.registerCommand(
PASTE_COMMAND,
mockPasteHandler,
COMMAND_PRIORITY_CRITICAL,
);

await openContextMenu();
const pasteOption = getMenuItem('Paste');
expect(pasteOption).not.toBeNull();

await act(async () => {
pasteOption!.click();
});

expect(alert).toHaveBeenCalledWith('Not allowed to paste from clipboard.');
expect(mockPasteHandler).not.toHaveBeenCalled();
});
});
103 changes: 60 additions & 43 deletions packages/lexical-playground/src/plugins/ContextMenuPlugin/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,25 @@ import {type JSX, useMemo} from 'react';

import {PLAYGROUND_TRANSFORMERS} from '../MarkdownTransformers';

async function isClipboardReadDenied(): Promise<boolean> {
if (
navigator.permissions &&
typeof navigator.permissions.query === 'function'
) {
try {
const permission = await navigator.permissions.query({
// @ts-expect-error These types are incorrect.
name: 'clipboard-read',
});
return permission.state === 'denied';
} catch {
// navigator.permissions.query with 'clipboard-read' is not supported in all browsers (e.g. Firefox)
return false;
}
}
return false;
}

export default function ContextMenuPlugin({
shouldPreserveNewLinesInMarkdown,
}: {
Expand Down Expand Up @@ -81,69 +100,67 @@ export default function ContextMenuPlugin({
),
}),
new NodeContextMenuOption(`Paste`, {
$onSelect: () => {
navigator.clipboard.read().then(async function (...args) {
const data = new DataTransfer();
$onSelect: async () => {
if (await isClipboardReadDenied()) {
alert('Not allowed to paste from clipboard.');
return;
}

try {
const readClipboardItems = await navigator.clipboard.read();
const item = readClipboardItems[0];
if (!item) {
return;
}

if (navigator.permissions) {
const permission = await navigator.permissions.query({
// @ts-expect-error These types are incorrect.
name: 'clipboard-read',
});
if (permission.state === 'denied') {
alert('Not allowed to paste from clipboard.');
return;
}

for (const type of item.types) {
const dataString = await (await item.getType(type)).text();
data.setData(type, dataString);
}
const data = new DataTransfer();
for (const type of item.types) {
const dataString = await (await item.getType(type)).text();
data.setData(type, dataString);
}

const event = new ClipboardEvent('paste', {
clipboardData: data,
});
const event = new ClipboardEvent('paste');
// Firefox ignores clipboardData in ClipboardEventInit.
Object.defineProperty(event, 'clipboardData', {value: data});

editor.dispatchCommand(PASTE_COMMAND, event);
editor.dispatchCommand(PASTE_COMMAND, event);
} catch (error: unknown) {
if (error instanceof Error && error.name === 'NotAllowedError') {
Comment thread
vishalrajofficial marked this conversation as resolved.
Resolved
alert('Not allowed to paste from clipboard.');
} else {
alert('Your browser does not support navigator.permissions');
console.error(error);
}
});
}
},
disabled: false,
icon: (
<i className="PlaygroundEditorTheme__contextMenuItemIcon paste" />
),
}),
new NodeContextMenuOption(`Paste as Plain Text`, {
$onSelect: () => {
navigator.clipboard.read().then(async function (...args) {
if (navigator.permissions) {
const permission = await navigator.permissions.query({
// @ts-expect-error These types are incorrect.
name: 'clipboard-read',
});
$onSelect: async () => {
if (await isClipboardReadDenied()) {
alert('Not allowed to paste from clipboard.');
return;
}

if (permission.state === 'denied') {
alert('Not allowed to paste from clipboard.');
return;
}
try {
const clipboardText = await navigator.clipboard.readText();
const data = new DataTransfer();
data.setData('text/plain', clipboardText);

const data = new DataTransfer();
const clipboardText = await navigator.clipboard.readText();
data.setData('text/plain', clipboardText);
const event = new ClipboardEvent('paste');
// Firefox ignores clipboardData in ClipboardEventInit.
Object.defineProperty(event, 'clipboardData', {value: data});

const event = new ClipboardEvent('paste', {
clipboardData: data,
});
editor.dispatchCommand(PASTE_COMMAND, event);
editor.dispatchCommand(PASTE_COMMAND, event);
} catch (error: unknown) {
if (error instanceof Error && error.name === 'NotAllowedError') {
alert('Not allowed to paste from clipboard.');
} else {
alert('Your browser does not support navigator.permissions');
console.error(error);
}
});
}
},
disabled: false,
icon: (
Expand Down