Skip to content

Commit

Permalink
Fix paste emulate
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Nov 14, 2022
1 parent 16c0fc1 commit 075e831
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ export default function useInput() {
: anchorNode.parentElement
).closest( '[contenteditable]' );

if ( ! anchorElement ) {
return;
}

const init = {};

for ( const key in event ) {
Expand Down
21 changes: 19 additions & 2 deletions packages/e2e-test-utils/src/press-key-with-modifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,11 @@ export async function setClipboardData( { plainText = '', html = '' } ) {

async function emulateClipboard( type ) {
await page.evaluate( ( _type ) => {
const selection = window.getSelection();

if ( _type !== 'paste' ) {
window._clipboardData = new DataTransfer();

const selection = window.getSelection();
const plainText = selection.toString();
let html = plainText;

Expand All @@ -123,7 +124,23 @@ async function emulateClipboard( type ) {
window._clipboardData.setData( 'text/html', html );
}

document.activeElement.dispatchEvent(
const { anchorNode } = selection;

if ( ! anchorNode ) {
return;
}

const anchorElement = (
anchorNode.nodeType === anchorNode.ELEMENT_NODE
? anchorNode
: anchorNode.parentElement
).closest( '[contenteditable]' );

if ( ! anchorElement ) {
return;
}

anchorElement.dispatchEvent(
new ClipboardEvent( _type, {
bubbles: true,
cancelable: true,
Expand Down

0 comments on commit 075e831

Please sign in to comment.