Skip to content

Commit

Permalink
Use isValidHref in addLink function to check validity of selected text (
Browse files Browse the repository at this point in the history
#32663)

* Use isValidHref in addLink function to check validity of selected text

* Add isValidHref to React Native occurrence of file.

* Add E2E test coverage for Links when selected text is not a valid URL

* Update E2E test
  • Loading branch information
tjcafferkey authored Jun 28, 2021
1 parent 98d1fd7 commit 96a8781
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
21 changes: 21 additions & 0 deletions packages/e2e-tests/specs/editor/various/links.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,27 @@ describe( 'Links', () => {
expect( await getEditedPostContent() ).toMatchSnapshot();
} );

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' );

// 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( '[aria-label="URL"]' ).value
);

expect( urlInputValue ).toBe( '' );
} );

it( 'can be created by selecting text and using keyboard shortcuts', async () => {
// Create a block with some text
await clickBlockAppender();
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 @@ -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' );
Expand All @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion packages/format-library/src/link/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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 },
Expand Down
1 change: 1 addition & 0 deletions packages/format-library/src/link/test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ describe( 'isValidHref', () => {
expect( isValidHref( 'http://test.com/eeee#qwd qwdw' ) ).toBe(
false
);
expect( isValidHref( 'this: is invalid' ) ).toBe( false );
} );
} );

Expand Down

0 comments on commit 96a8781

Please sign in to comment.