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

Ensure only valid URLs or anchors within text are automatically created as links #32663

Merged
merged 4 commits into from
Jun 28, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
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