Skip to content

Commit

Permalink
Ensure that filterURLForDisplay always receives a string as an arg
Browse files Browse the repository at this point in the history
  • Loading branch information
getdave committed Sep 19, 2023
1 parent dd9e6c4 commit 724bffc
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions packages/block-editor/src/components/link-control/search-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ function SearchItemIcon( { isURL, suggestion } ) {
* @return {string} the url with a leading slash.
*/
function addLeadingSlash( url ) {
const trimmedURL = url?.trim();

if ( ! trimmedURL?.length ) return url;

return url?.replace( /^\/?/, '/' );
}

Expand All @@ -59,6 +63,10 @@ const partialRight =
( ...args ) =>
fn( ...args, ...partialArgs );

const defaultTo = ( d ) => ( v ) => {
return v === null || v === undefined || v !== v ? d : v;
};

/**
* Prepares a URL for display in the UI.
* - decodes the URL.
Expand All @@ -68,10 +76,13 @@ const partialRight =
* @param {string} url the url.
* @return {string} the processed url to display.
*/
function getURLForDisplay( url = '' ) {
function getURLForDisplay( url ) {
if ( ! url ) return url;

return compose(
addLeadingSlash,
partialRight( filterURLForDisplay, 24 ),
defaultTo( '' ),
getPath,
safeDecodeURI
)( url );
Expand All @@ -87,7 +98,7 @@ export const LinkControlSearchItem = ( {
} ) => {
const info = isURL
? __( 'Press ENTER to add this link' )
: getURLForDisplay( suggestion?.url );
: getURLForDisplay( suggestion.url );

return (
<MenuItem
Expand Down

0 comments on commit 724bffc

Please sign in to comment.