From e4d30d0eaaea58794a7c3a9c582d5eed406f8250 Mon Sep 17 00:00:00 2001 From: Tom Cafferkey Date: Mon, 14 Jun 2021 12:39:36 +0100 Subject: [PATCH 1/4] Use isValidHref in addLink function to check validity of selected text --- packages/format-library/src/link/index.js | 3 ++- packages/format-library/src/link/test/utils.js | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/format-library/src/link/index.js b/packages/format-library/src/link/index.js index e56e93f1743c4..5fe0067087dbd 100644 --- a/packages/format-library/src/link/index.js +++ b/packages/format-library/src/link/index.js @@ -23,6 +23,7 @@ import { speak } from '@wordpress/a11y'; * Internal dependencies */ import InlineLinkUI from './inline'; +import { isValidHref } from './utils'; const name = 'core/link'; const title = __( 'Link' ); @@ -40,7 +41,7 @@ function Edit( { function addLink() { const text = getTextContent( slice( value ) ); - if ( text && isURL( text ) ) { + if ( text && isURL( text ) && isValidHref( text ) ) { onChange( applyFormat( value, { type: name, diff --git a/packages/format-library/src/link/test/utils.js b/packages/format-library/src/link/test/utils.js index 7ae7c1b3ad10c..0cad99eed3245 100644 --- a/packages/format-library/src/link/test/utils.js +++ b/packages/format-library/src/link/test/utils.js @@ -74,6 +74,7 @@ describe( 'isValidHref', () => { expect( isValidHref( 'http://test.com/eeee#qwd qwdw' ) ).toBe( false ); + expect( isValidHref( 'this: is invalid' ) ).toBe( false ); } ); } ); From 162715dbcbdd7fffd28076d0d809cfeaec563d57 Mon Sep 17 00:00:00 2001 From: Tom Cafferkey Date: Mon, 14 Jun 2021 12:47:16 +0100 Subject: [PATCH 2/4] Add isValidHref to React Native occurrence of file. --- packages/format-library/src/link/index.native.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/format-library/src/link/index.native.js b/packages/format-library/src/link/index.native.js index 2c49c4a98a577..e79ed696aea37 100644 --- a/packages/format-library/src/link/index.native.js +++ b/packages/format-library/src/link/index.native.js @@ -26,6 +26,7 @@ import { link as linkIcon } from '@wordpress/icons'; * Internal dependencies */ import ModalLinkUI from './modal'; +import { isValidHref } from './utils'; const name = 'core/link'; @@ -58,7 +59,7 @@ export const link = { const { value, onChange } = this.props; const text = getTextContent( slice( value ) ); - if ( text && isURL( text ) ) { + if ( text && isURL( text ) && isValidHref( text ) ) { const newValue = applyFormat( value, { type: name, attributes: { url: text }, From 2b531024039dbfc43bd032d0d116ca2b15e65e28 Mon Sep 17 00:00:00 2001 From: Tom Cafferkey Date: Wed, 16 Jun 2021 15:52:46 +0100 Subject: [PATCH 3/4] Add E2E test coverage for Links when selected text is not a valid URL --- .../specs/editor/various/links.test.js | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/packages/e2e-tests/specs/editor/various/links.test.js b/packages/e2e-tests/specs/editor/various/links.test.js index 2b42291656275..ad7326ac4fb7d 100644 --- a/packages/e2e-tests/specs/editor/various/links.test.js +++ b/packages/e2e-tests/specs/editor/various/links.test.js @@ -81,6 +81,30 @@ describe( 'Links', () => { expect( await getEditedPostContent() ).toMatchSnapshot(); } ); + it( 'will not pre-populate URL input if selected text is not a link', async () => { + // Create a block with some text + await clickBlockAppender(); + await page.keyboard.type( 'This: is not a link' ); + + // Select some text + await pressKeyWithModifier( 'shiftAlt', 'ArrowLeft' ); + + // Click on the Link button + await page.click( 'button[aria-label="Link"]' ); + + // Wait for the URL field to auto-focus + await waitForAutoFocus(); + + const urlInputValue = await page.evaluate( + () => + document.querySelector( + '.components-popover__content .block-editor-link-control .block-editor-url-input__input' + ).value + ); + + expect( urlInputValue ).toBe( '' ); + } ); + it( 'can be created by selecting text and using keyboard shortcuts', async () => { // Create a block with some text await clickBlockAppender(); From 8160b2f853b19c355012f0a6bf1882e8219fd5f4 Mon Sep 17 00:00:00 2001 From: Tom Cafferkey Date: Thu, 17 Jun 2021 08:33:30 +0100 Subject: [PATCH 4/4] Update E2E test --- packages/e2e-tests/specs/editor/various/links.test.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/packages/e2e-tests/specs/editor/various/links.test.js b/packages/e2e-tests/specs/editor/various/links.test.js index ad7326ac4fb7d..1be82dab15034 100644 --- a/packages/e2e-tests/specs/editor/various/links.test.js +++ b/packages/e2e-tests/specs/editor/various/links.test.js @@ -81,7 +81,7 @@ describe( 'Links', () => { expect( await getEditedPostContent() ).toMatchSnapshot(); } ); - it( 'will not pre-populate URL input if selected text is not a link', async () => { + it( 'will not automatically create a link if selected text is not a valid HTTP based URL', async () => { // Create a block with some text await clickBlockAppender(); await page.keyboard.type( 'This: is not a link' ); @@ -96,10 +96,7 @@ describe( 'Links', () => { await waitForAutoFocus(); const urlInputValue = await page.evaluate( - () => - document.querySelector( - '.components-popover__content .block-editor-link-control .block-editor-url-input__input' - ).value + () => document.querySelector( '[aria-label="URL"]' ).value ); expect( urlInputValue ).toBe( '' );