Repository navigation
[lexical-playground] Bug Fix: Safe clipboard permission query in ContextMenuPlugin #9910
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
vishalrajofficial
wants to merge
3
commits into
meta:main
Choose a base branch
from
vishalrajofficial:fix-firefox-context-menu-paste
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
ef4fdf4
[lexical-playground] Bug Fix: Safe clipboard permission query in Cont…
vishalrajofficial 1eaf593
[lexical-playground] Bug Fix: Attach clipboardData after construction…
vishalrajofficial 596ca15
Merge branch 'main' into fix-firefox-context-menu-paste
potatowagon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
250 changes: 250 additions & 0 deletions
250
packages/lexical-playground/__tests__/unit/ContextMenuPlugin.test.tsx
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
| 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(); | ||
| }); | ||
| }); |
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
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.
Uh oh!
There was an error while loading. Please reload this page.