Skip to content

Commit

Permalink
Fix paste emulator
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed May 21, 2024
1 parent d7e81d0 commit 3bed7b6
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
17 changes: 16 additions & 1 deletion packages/e2e-test-utils-playwright/src/page-utils/press-keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export function setClipboardData(
}

async function emulateClipboard( page: Page, type: 'copy' | 'cut' | 'paste' ) {
clipboardDataHolder = await page.evaluate(
const output = await page.evaluate(
( [ _type, _clipboardData ] ) => {
const canvasDoc =
// @ts-ignore
Expand Down Expand Up @@ -99,6 +99,10 @@ async function emulateClipboard( page: Page, type: 'copy' | 'cut' | 'paste' ) {

canvasDoc.activeElement.dispatchEvent( event );

if ( _type === 'paste' ) {
return event.defaultPrevented;
}

return {
'text/plain': event.clipboardData.getData( 'text/plain' ),
'text/html': event.clipboardData.getData( 'text/html' ),
Expand All @@ -107,6 +111,17 @@ async function emulateClipboard( page: Page, type: 'copy' | 'cut' | 'paste' ) {
},
[ type, clipboardDataHolder ] as const
);

if ( typeof output === 'object' ) {
clipboardDataHolder = output;
}

if ( output === false ) {
// Emulate paste by typing the clipboard content, which works across all
// elements and documents (keyboard.type does uses the nested active
// element automatically).
await page.keyboard.type( clipboardDataHolder[ 'text/plain' ] );
}
}

const isAppleOS = () => process.platform === 'darwin';
Expand Down
31 changes: 31 additions & 0 deletions test/e2e/specs/editor/blocks/rss.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* WordPress dependencies
*/
const { test, expect } = require( '@wordpress/e2e-test-utils-playwright' );

test.describe( 'RSS', () => {
test.beforeEach( async ( { admin } ) => {
await admin.createNewPost();
} );

// See: https://github.com/WordPress/gutenberg/pull/61389.
test( 'should retain native copy/paste behavior for input fields', async ( {
editor,
pageUtils,
} ) => {
await editor.insertBlock( { name: 'core/rss' } );
pageUtils.setClipboardData( {
plainText: 'https://developer.wordpress.org/news/feed/',
} );
await pageUtils.pressKeys( 'primary+v' );

await expect.poll( editor.getBlocks ).toMatchObject( [
{
name: 'core/rss',
attributes: {
feedURL: 'https://developer.wordpress.org/news/feed/',
},
},
] );
} );
} );

0 comments on commit 3bed7b6

Please sign in to comment.