From 28026793d9fe5b1a1a0bb88e5dbc634957f22a70 Mon Sep 17 00:00:00 2001 From: Amit Raj Date: Thu, 30 May 2024 13:07:49 +0530 Subject: [PATCH] Changed regex for stripDomainPrefixes to handle all protocols --- .../components/link-control/link-preview.js | 4 ++-- packages/url/README.md | 24 +++++++++---------- packages/url/src/filter-url-for-display.js | 17 ++++++++----- packages/url/src/index.js | 2 +- 4 files changed, 26 insertions(+), 21 deletions(-) diff --git a/packages/block-editor/src/components/link-control/link-preview.js b/packages/block-editor/src/components/link-control/link-preview.js index c22f0388797249..4afda7358d4f70 100644 --- a/packages/block-editor/src/components/link-control/link-preview.js +++ b/packages/block-editor/src/components/link-control/link-preview.js @@ -16,7 +16,7 @@ import { useCopyToClipboard } from '@wordpress/compose'; import { filterURLForDisplay, safeDecodeURI, - removeProtocol, + stripDomainPrefixes, } from '@wordpress/url'; import { Icon, globe, info, linkOff, edit, copySmall } from '@wordpress/icons'; import { __unstableStripHTML as stripHTML } from '@wordpress/dom'; @@ -64,7 +64,7 @@ export default function LinkPreview( { stripHTML( richData?.title || value?.title || displayURL ); const isTitleRedundant = - value?.url && removeProtocol( displayTitle ) === displayURL; + value?.url && stripDomainPrefixes( displayTitle ) === displayURL; let icon; diff --git a/packages/url/README.md b/packages/url/README.md index 18622427b4b966..818b4af5a53944 100644 --- a/packages/url/README.md +++ b/packages/url/README.md @@ -490,18 +490,6 @@ _Returns_ - `string`: The updated URL. -### removeProtocol - -Removes the protocol from a URL. - -_Parameters_ - -- _url_ `string`: - The URL to remove the protocol from. - -_Returns_ - -- `string`: The URL without the protocol. - ### removeQueryArgs Removes arguments from the query string of the url @@ -555,6 +543,18 @@ _Returns_ - `string`: Decoded URI component if possible. +### stripDomainPrefixes + +Removes domain prefixes from a URL. + +_Parameters_ + +- _url_ `string`: The URL to strip domain prefixes from. + +_Returns_ + +- `string`: The URL without domain prefixes. + ## Contributing to this package diff --git a/packages/url/src/filter-url-for-display.js b/packages/url/src/filter-url-for-display.js index f2add88d4e375e..24a847c9beae4e 100644 --- a/packages/url/src/filter-url-for-display.js +++ b/packages/url/src/filter-url-for-display.js @@ -14,7 +14,7 @@ */ export function filterURLForDisplay( url, maxLength = null ) { // Remove protocol and www prefixes. - let filteredURL = removeProtocol( url ); + let filteredURL = stripDomainPrefixes( url ); // Ends with / and only has that single slash, strip it. if ( filteredURL.match( /^[^\/]+\/$/ ) ) { @@ -55,12 +55,17 @@ export function filterURLForDisplay( url, maxLength = null ) { } /** - * Removes the protocol from a URL. + * Removes domain prefixes from a URL. * - * @param {string} url - The URL to remove the protocol from. + * @param {string} url The URL to strip domain prefixes from. * - * @return {string} The URL without the protocol. + * @return {string} The URL without domain prefixes. */ -export function removeProtocol( url ) { - return url.replace( /^(?:https?:)\/\/(?:www\.)?/, '' ); +export function stripDomainPrefixes( url ) { + if ( ! url ) { + return ''; + } + return url + .replace( /^[a-z\-.\+]+[0-9]*:(\/\/)?/i, '' ) + .replace( /^www\./i, '' ); } diff --git a/packages/url/src/index.js b/packages/url/src/index.js index de2bb35628bd04..868abc1c468062 100644 --- a/packages/url/src/index.js +++ b/packages/url/src/index.js @@ -25,4 +25,4 @@ export { cleanForSlug } from './clean-for-slug'; export { getFilename } from './get-filename'; export { normalizePath } from './normalize-path'; export { prependHTTPS } from './prepend-https'; -export { removeProtocol } from './filter-url-for-display'; +export { stripDomainPrefixes } from './filter-url-for-display';