Skip to content
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

Paste: only link selection if URL protocol is http(s) #53000

Merged
merged 1 commit into from
Oct 10, 2023
Merged
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
Expand Up @@ -139,10 +139,14 @@ export function usePasteHandler( props ) {

let mode = onReplace && onSplit ? 'AUTO' : 'INLINE';

const trimmedPlainText = plainText.trim();

if (
__unstableEmbedURLOnPaste &&
isEmpty( value ) &&
isURL( plainText.trim() )
isURL( trimmedPlainText ) &&
// For the link pasting feature, allow only http(s) protocols.
/^https?:/.test( trimmedPlainText )
) {
mode = 'BLOCKS';
}
Expand Down
3 changes: 2 additions & 1 deletion packages/format-library/src/link/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ export const link = {
.trim();

// A URL was pasted, turn the selection into a link.
if ( ! isURL( pastedText ) ) {
// For the link pasting feature, allow only http(s) protocols.
if ( ! isURL( pastedText ) || ! /^https?:/.test( pastedText ) ) {
return value;
}

Expand Down
49 changes: 49 additions & 0 deletions test/e2e/specs/editor/various/copy-cut-paste.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -496,4 +496,53 @@ test.describe( 'Copy/cut/paste', () => {
await page.keyboard.type( 'y' );
expect( await editor.getEditedPostContent() ).toMatchSnapshot();
} );

test( 'should link selection', async ( { pageUtils, editor } ) => {
await editor.insertBlock( {
name: 'core/paragraph',
attributes: {
content: 'a',
},
} );
await pageUtils.pressKeys( 'primary+a' );
pageUtils.setClipboardData( {
plainText: 'https://wordpress.org/gutenberg',
html: '<a href="https://wordpress.org/gutenberg">https://wordpress.org/gutenberg</a>',
} );
await pageUtils.pressKeys( 'primary+v' );
expect( await editor.getBlocks() ).toMatchObject( [
{
name: 'core/paragraph',
attributes: {
content: '<a href="https://wordpress.org/gutenberg">a</a>',
},
},
] );
} );

test( 'should not link selection for non http(s) protocol', async ( {
pageUtils,
editor,
} ) => {
await editor.insertBlock( {
name: 'core/paragraph',
attributes: {
content: 'a',
},
} );
await pageUtils.pressKeys( 'primary+a' );
pageUtils.setClipboardData( {
plainText: 'movie: b',
html: 'movie: b',
} );
await pageUtils.pressKeys( 'primary+v' );
expect( await editor.getBlocks() ).toMatchObject( [
{
name: 'core/paragraph',
attributes: {
content: 'movie: b',
},
},
] );
} );
} );