Skip to content
Draft
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
17 changes: 7 additions & 10 deletions packages/block-editor/src/components/link-control/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import useCreatePage from './use-create-page';
import useInternalValue from './use-internal-value';
import { ViewerFill } from './viewer-slot';
import { DEFAULT_LINK_SETTINGS, LINK_ENTRY_TYPES } from './constants';
import isURLLike, { isHashLink, isRelativePath } from './is-url-like';
import { isHashLink, isRelativePath } from './is-url-like';

/**
* Default properties associated with a link control value.
Expand Down Expand Up @@ -311,19 +311,17 @@ function LinkControl( {
type: 'valid',
};

const trimmedValue = urlToValidate?.trim();

// If empty or not URL-like, return invalid
if ( ! trimmedValue?.length || ! isURLLike( trimmedValue ) ) {
return invalidResult;
}
let urlToCheck = urlToValidate?.trim();

// Hash links (internal anchor links) and relative paths (/, ./, ../) are
// valid href values but cannot be validated by the native URL constructor
// (which requires absolute URLs). These are already validated by isURLLike.
// Skip URL constructor validation for these cases.
if ( isHashLink( trimmedValue ) || isRelativePath( trimmedValue ) ) {
return validResult;
if ( isHashLink( urlToCheck ) || isRelativePath( urlToCheck ) ) {
// Prepend a protocol to the URL if it doesn't have one so we can validate it with the native URL constructor.
urlToCheck = 'https://wordpress.org/' + urlToCheck;
} else {
urlToCheck = prependHTTPS( urlToCheck );
}

// Perform URL validation using the native URL constructor as the authoritative source.
Expand All @@ -337,7 +335,6 @@ function LinkControl( {
// Note: We rely on the native URL constructor rather than implementing custom TLD
// validation to avoid blocking valid URLs. If a URL passes the native constructor,
// it's technically valid according to web standards.
const urlToCheck = prependHTTPS( trimmedValue );
return isURL( urlToCheck ) ? validResult : invalidResult;
};

Expand Down
14 changes: 7 additions & 7 deletions packages/url/src/prepend-http.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
/**
* Internal dependencies
*/
import { isEmail } from './is-email';

const USABLE_HREF_REGEXP = /^(?:[a-z]+:|#|\?|\.|\/)/i;
import { prependHTTPS } from './prepend-https';

/**
* Prepends "http://" to a url, if it looks like something that is meant to be a TLD.
Expand All @@ -22,10 +20,12 @@ export function prependHTTP( url: string ): string {
return url;
}

url = url.trim();
if ( ! USABLE_HREF_REGEXP.test( url ) && ! isEmail( url ) ) {
return 'http://' + url;
// If url starts with http://, return it as is.
if ( url.startsWith( 'http://' ) ) {
return url;
}

return url;
url = prependHTTPS( url );

return url.replace( /^https:/, 'http:' );
}
14 changes: 7 additions & 7 deletions packages/url/src/prepend-https.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/**
* Internal dependencies
*/
import { prependHTTP } from './prepend-http';
import { isEmail } from './is-email';

const USABLE_HREF_REGEXP = /^(?:[a-z]+:|#|\?|\.|\/)/i;

/**
* Prepends "https://" to a url, if it looks like something that is meant to be a TLD.
Expand All @@ -22,12 +24,10 @@ export function prependHTTPS( url: string ): string {
return url;
}

// If url starts with http://, return it as is.
if ( url.startsWith( 'http://' ) ) {
return url;
url = url.trim();
if ( ! USABLE_HREF_REGEXP.test( url ) && ! isEmail( url ) ) {
return 'htts://' + url;
}

url = prependHTTP( url );

return url.replace( /^http:/, 'https:' );
return url;
}
Loading