Skip to content

Commit

Permalink
Use relaxed isURLLike and ensure https is always prepended on submit
Browse files Browse the repository at this point in the history
  • Loading branch information
getdave committed Aug 28, 2024
1 parent 1586c6b commit 0014fde
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
15 changes: 12 additions & 3 deletions packages/block-library/src/social-link/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { keyboardReturn } from '@wordpress/icons';
import { isURL, isEmail } from '@wordpress/url';
import { isEmail, prependHTTPS } from '@wordpress/url';

/**
* Internal dependencies
Expand All @@ -36,6 +36,8 @@ import {
getMatchingService,
} from './social-list';

import isURLLike from './is-url-like';

const SocialLinkURLPopover = ( {
url,
setAttributes,
Expand All @@ -50,6 +52,11 @@ const SocialLinkURLPopover = ( {
className="block-editor-url-popover__link-editor"
onSubmit={ ( event ) => {
event.preventDefault();

if ( isURLLike( url ) ) {
// Append https if user did not include it.
setAttributes( { url: prependHTTPS( url ) } );
}
popoverAnchor?.focus();
onClose( false );
} }
Expand All @@ -63,10 +70,12 @@ const SocialLinkURLPopover = ( {
service: undefined,
};

if ( isURL( nextURL ) || isEmail( nextURL ) ) {
if ( isURLLike( nextURL ) || isEmail( nextURL ) ) {
const matchingService = isEmail( nextURL )
? 'mail'
: getMatchingService( nextURL );
: getMatchingService(
prependHTTPS( nextURL )
);

nextAttributes.service =
matchingService ?? 'chain';
Expand Down
39 changes: 39 additions & 0 deletions packages/block-library/src/social-link/is-url-like.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* WordPress dependencies
*/
import { getProtocol, isValidProtocol } from '@wordpress/url';

// Please see packages/block-editor/src/components/link-control/is-url-like.js
export default function isURLLike( val ) {
const hasSpaces = val.includes( ' ' );

if ( hasSpaces ) {
return false;
}

const protocol = getProtocol( val );
const protocolIsValid = isValidProtocol( protocol );

const mayBeTLD = hasPossibleTLD( val );

const isWWW = val?.startsWith( 'www.' );

return protocolIsValid || isWWW || mayBeTLD;
}

// Please see packages/block-editor/src/components/link-control/is-url-like.js
function hasPossibleTLD( url, maxLength = 6 ) {
// Clean the URL by removing anything after the first occurrence of "?" or "#".
const cleanedURL = url.split( /[?#]/ )[ 0 ];

// Regular expression explanation:
// - (?<=\S) : Positive lookbehind assertion to ensure there is at least one non-whitespace character before the TLD
// - \. : Matches a literal dot (.)
// - [a-zA-Z_]{2,maxLength} : Matches 2 to maxLength letters or underscores, representing the TLD
// - (?:\/|$) : Non-capturing group that matches either a forward slash (/) or the end of the string
const regex = new RegExp(
`(?<=\\S)\\.(?:[a-zA-Z_]{2,${ maxLength }})(?:\\/|$)`
);

return regex.test( cleanedURL );
}

0 comments on commit 0014fde

Please sign in to comment.