Skip to content
Merged
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
10 changes: 9 additions & 1 deletion packages/block-editor/src/components/link-control/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ 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 normalizeUrl from './normalize-url';

/**
* Default properties associated with a link control value.
Expand Down Expand Up @@ -363,6 +364,13 @@ function LinkControl( {
setCustomValidity( validation );
return;
}

// Validation passed - normalize the URL
const { url: normalizedUrl } = normalizeUrl( urlToValidate );
updatedValue = {
...updatedValue,
url: normalizedUrl,
};
}

// Preserve the URL for taxonomy entities before binding overrides it
Expand Down Expand Up @@ -444,7 +452,7 @@ function LinkControl( {
onChange( {
...value,
...internalControlValue,
url: currentUrlInputValue,
url: normalizeUrl( currentUrlInputValue ).url,
} );
}
stopEditing();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* WordPress dependencies
*/
import { getProtocol, prependHTTPS } from '@wordpress/url';

/**
* Internal dependencies
*/
import { isHashLink, isRelativePath } from './is-url-like';
import { TEL_TYPE, MAILTO_TYPE, INTERNAL_TYPE, URL_TYPE } from './constants';

/**
* Normalizes a URL string by adding https:// protocol if needed.
* This function determines the final URL that will be saved.
*
* Normalization rules:
* - Bare domains (wordpress.org, www.wordpress.org) → prepend https://
* - URLs with explicit protocols (http://, https://, mailto:, tel:, etc.) → keep as-is
* - Relative paths (/, ./, ../) → keep as-is
* - Hash links (#section) → keep as-is
*
* @param {string} url - The URL to normalize
* @return {Object} An object containing the normalized URL and its type
*/
export default function normalizeUrl( url ) {
const trimmedUrl = url?.trim();

if ( ! trimmedUrl ) {
return { url: trimmedUrl, type: URL_TYPE };
}

let type = URL_TYPE;
const protocol = getProtocol( trimmedUrl ) || '';

// Determine the type based on the URL format
if ( protocol.includes( 'mailto' ) ) {
type = MAILTO_TYPE;
} else if ( protocol.includes( 'tel' ) ) {
type = TEL_TYPE;
} else if ( trimmedUrl?.startsWith( '#' ) ) {
type = INTERNAL_TYPE;
}

// Hash links, relative paths, query parameters, and URLs with protocols should not be modified
if (
isHashLink( trimmedUrl ) ||
isRelativePath( trimmedUrl ) ||
trimmedUrl.startsWith( '?' ) ||
protocol
) {
return { url: trimmedUrl, type };
}

// Bare domains need https:// prepended
return { url: prependHTTPS( trimmedUrl ), type };
}
129 changes: 74 additions & 55 deletions packages/block-editor/src/components/link-control/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2832,14 +2832,10 @@ describe( 'Entity handling', () => {

// Verify that onChange was called with entity metadata cleared.
// Kind should be undefined (no longer an entity).
// Note: Currently when clicking Apply (vs selecting a suggestion),
// type and id are also undefined - that's a separate issue with the
// TODO: Apply button handler not processing URLs through handleDirectEntry,
// so the shape of the data for a custom link can be different depending on
// how it was submitted.
// The URL should be normalized (https:// prepended to bare domain).
expect( onChange ).toHaveBeenCalledWith(
expect.objectContaining( {
url: 'www.wordpress.org',
url: 'https://www.wordpress.org',
kind: undefined,
} )
);
Expand Down Expand Up @@ -3271,80 +3267,102 @@ describe( 'URL validation', () => {
mockOnChange.mockClear();
} );

it( 'should prevent submission for invalid URLs', async () => {
render(
<LinkControl
value={ { url: '' } }
forceIsEditingLink
onChange={ mockOnChange }
/>
);
it.each( [
{
description: 'URLs with spaces',
inputUrl: 'not a url',
},
{
description: 'single words without TLD or protocol',
inputUrl: 'wordpress',
},
] )(
'should prevent submission for $description',
async ( { inputUrl } ) => {
render(
<LinkControl
value={ { url: '' } }
forceIsEditingLink
onChange={ mockOnChange }
/>
);

const searchInput = screen.getByRole( 'combobox' );
// Use a string that is not a valid URL
await user.type( searchInput, 'not a url' );
const searchInput = screen.getByRole( 'combobox' );
await user.type( searchInput, inputUrl );

// Press Enter - this should trigger validation
// Since the value doesn't pass isURLLike, it won't create a suggestion,
// but if it did, validation would prevent submission
triggerEnter( searchInput );
// Press Enter - this should trigger validation
triggerEnter( searchInput );

// For URLs that don't pass isURLLike, no suggestion is created,
// so onChange won't be called (which is the expected behavior)
expect( mockOnChange ).not.toHaveBeenCalled();
} );
// Wait for validation error to appear
await waitFor( () => {
expect(
screen.getByText( 'Please enter a valid URL.' )
).toBeInTheDocument();
} );

// onChange should NOT have been called (submission prevented)
expect( mockOnChange ).not.toHaveBeenCalled();
}
);

it.each( [
{
description: 'valid URLs with protocol',
url: 'https://wordpress.org',
inputUrl: 'https://wordpress.org',
expectedUrl: 'https://wordpress.org',
searchPattern: /https:\/\/wordpress\.org/,
},
{
description: 'valid URLs without protocol (without http://)',
url: 'www.wordpress.org',
inputUrl: 'www.wordpress.org',
expectedUrl: 'https://www.wordpress.org',
searchPattern: /www\.wordpress\.org/,
},
{
description: 'hash links (internal anchor links)',
url: '#section',
inputUrl: '#section',
expectedUrl: '#section',
searchPattern: /#section/,
},
{
description: 'relative paths (URLs starting with /)',
url: '/handbook',
inputUrl: '/handbook',
expectedUrl: '/handbook',
searchPattern: /\/handbook/,
},
] )( 'should accept $description', async ( { url, searchPattern } ) => {
render(
<LinkControl
value={ { url: '' } }
forceIsEditingLink
onChange={ mockOnChange }
/>
);
] )(
'should accept $description',
async ( { inputUrl, expectedUrl, searchPattern } ) => {
render(
<LinkControl
value={ { url: '' } }
forceIsEditingLink
onChange={ mockOnChange }
/>
);

const searchInput = screen.getByRole( 'combobox' );
await user.type( searchInput, url );
const searchInput = screen.getByRole( 'combobox' );
await user.type( searchInput, inputUrl );

// Wait for suggestion to appear and become stable
await screen.findByRole( 'option', {
name: searchPattern,
} );
// Wait for suggestion to appear and become stable
await screen.findByRole( 'option', {
name: searchPattern,
} );

triggerEnter( searchInput );
triggerEnter( searchInput );

// No validation error - should succeed
await waitFor( () => {
expect( mockOnChange ).toHaveBeenCalled();
} );
// No validation error - should succeed
await waitFor( () => {
expect( mockOnChange ).toHaveBeenCalled();
} );

expect( mockOnChange ).toHaveBeenCalledWith(
expect.objectContaining( {
url,
} )
);
} );
expect( mockOnChange ).toHaveBeenCalledWith(
expect.objectContaining( {
url: expectedUrl,
} )
);
}
);

it( 'should skip validation for entity suggestions (posts, pages, categories)', async () => {
const entityLink = {
Expand Down Expand Up @@ -3484,9 +3502,10 @@ describe( 'URL validation', () => {
// a useful URL in practice. However, our validation philosophy is to
// trust the native URL constructor as the authoritative source - if the
// browser accepts it, we accept it.
// Note: The URL gets normalized with https:// prepended since it's a bare domain.
expect( mockOnChange ).toHaveBeenCalledWith(
expect.objectContaining( {
url: 'www.wordpress',
url: 'https://www.wordpress',
} )
);
} );
Expand Down
Loading
Loading