From a217e574155bf722be160c94f85bb54e41439745 Mon Sep 17 00:00:00 2001 From: Amit Raj Date: Tue, 21 May 2024 17:47:18 +0530 Subject: [PATCH 01/11] Add removeProtocol function to URL package --- packages/url/README.md | 12 ++++++++++++ packages/url/src/filter-url-for-display.js | 13 ++++++++++++- packages/url/src/index.js | 1 + 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/packages/url/README.md b/packages/url/README.md index 70e8579acc518..18622427b4b96 100644 --- a/packages/url/README.md +++ b/packages/url/README.md @@ -490,6 +490,18 @@ _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 diff --git a/packages/url/src/filter-url-for-display.js b/packages/url/src/filter-url-for-display.js index eede264e8e801..f2add88d4e375 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 = url.replace( /^(?:https?:)\/\/(?:www\.)?/, '' ); + let filteredURL = removeProtocol( url ); // Ends with / and only has that single slash, strip it. if ( filteredURL.match( /^[^\/]+\/$/ ) ) { @@ -53,3 +53,14 @@ export function filterURLForDisplay( url, maxLength = null ) { truncatedFile ); } + +/** + * Removes the protocol from a URL. + * + * @param {string} url - The URL to remove the protocol from. + * + * @return {string} The URL without the protocol. + */ +export function removeProtocol( url ) { + return url.replace( /^(?:https?:)\/\/(?:www\.)?/, '' ); +} diff --git a/packages/url/src/index.js b/packages/url/src/index.js index 141e19d3fa55f..de2bb35628bd0 100644 --- a/packages/url/src/index.js +++ b/packages/url/src/index.js @@ -25,3 +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'; From e9347409c177d07be160f8e82184194fda8766e0 Mon Sep 17 00:00:00 2001 From: Amit Raj Date: Tue, 21 May 2024 17:48:56 +0530 Subject: [PATCH 02/11] Fix redundant link preview display when link text includes protocol in LinkControl. --- .../src/components/link-control/link-preview.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 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 867b69356eb9d..c22f038879724 100644 --- a/packages/block-editor/src/components/link-control/link-preview.js +++ b/packages/block-editor/src/components/link-control/link-preview.js @@ -13,7 +13,11 @@ import { __experimentalTruncate as Truncate, } from '@wordpress/components'; import { useCopyToClipboard } from '@wordpress/compose'; -import { filterURLForDisplay, safeDecodeURI } from '@wordpress/url'; +import { + filterURLForDisplay, + safeDecodeURI, + removeProtocol, +} from '@wordpress/url'; import { Icon, globe, info, linkOff, edit, copySmall } from '@wordpress/icons'; import { __unstableStripHTML as stripHTML } from '@wordpress/dom'; import { useDispatch, useSelect } from '@wordpress/data'; @@ -59,6 +63,9 @@ export default function LinkPreview( { ! isEmptyURL && stripHTML( richData?.title || value?.title || displayURL ); + const isTitleRedundant = + value?.url && removeProtocol( displayTitle ) === displayURL; + let icon; if ( richData?.icon ) { @@ -112,7 +119,7 @@ export default function LinkPreview( { { displayTitle } - { value?.url && displayTitle !== displayURL && ( + { ! isTitleRedundant && ( { displayURL } From 28026793d9fe5b1a1a0bb88e5dbc634957f22a70 Mon Sep 17 00:00:00 2001 From: Amit Raj Date: Thu, 30 May 2024 13:07:49 +0530 Subject: [PATCH 03/11] 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 c22f038879724..4afda7358d4f7 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 18622427b4b96..818b4af5a5394 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 f2add88d4e375..24a847c9beae4 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 de2bb35628bd0..868abc1c46806 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'; From 28603624cd75756b3270282a5595d8afaaa0c0bc Mon Sep 17 00:00:00 2001 From: Amit Raj Date: Thu, 30 May 2024 13:11:06 +0530 Subject: [PATCH 04/11] Add unit tests for stripDomainPrefixes function --- packages/url/src/test/index.js | 63 ++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/packages/url/src/test/index.js b/packages/url/src/test/index.js index 0051b9b89fc73..ad059529bc514 100644 --- a/packages/url/src/test/index.js +++ b/packages/url/src/test/index.js @@ -27,6 +27,7 @@ import { prependHTTPS, removeQueryArgs, safeDecodeURI, + stripDomainPrefixes, } from '../'; import wptData from './fixtures/wpt-data'; @@ -127,6 +128,68 @@ describe( 'isValidProtocol', () => { } ); } ); +describe( 'stripDomainPrefixes', () => { + it( 'should remove the protocol (http, https, file, tel, etc.) from the URL', () => { + expect( stripDomainPrefixes( 'http://wordpress.org' ) ).toBe( + 'wordpress.org' + ); + expect( stripDomainPrefixes( 'https://wordpress.org' ) ).toBe( + 'wordpress.org' + ); + expect( stripDomainPrefixes( 'https://localhost:8080' ) ).toBe( + 'localhost:8080' + ); + expect( stripDomainPrefixes( 'file:///folder/file.txt' ) ).toBe( + '/folder/file.txt' + ); + expect( stripDomainPrefixes( 'ftp://wordpress.org' ) ).toBe( + 'wordpress.org' + ); + expect( stripDomainPrefixes( 'tel:0123456789' ) ).toBe( '0123456789' ); + expect( stripDomainPrefixes( 'blob:data' ) ).toBe( 'data' ); + } ); + + it( 'should remove both the protocol and www prefix if both are present', () => { + expect( stripDomainPrefixes( 'http://www.google.com' ) ).toBe( + 'google.com' + ); + expect( stripDomainPrefixes( 'https://www.google.com' ) ).toBe( + 'google.com' + ); + expect( stripDomainPrefixes( 'ftp://www.google.com' ) ).toBe( + 'google.com' + ); + } ); + + it( 'should handle URLs without a protocol', () => { + expect( stripDomainPrefixes( 'www.example.com' ) ).toBe( + 'example.com' + ); + expect( stripDomainPrefixes( 'example.com' ) ).toBe( 'example.com' ); + expect( stripDomainPrefixes( 'example.com/path' ) ).toBe( + 'example.com/path' + ); + } ); + + it( 'should handle URLs with both parameters and fragments correctly', () => { + expect( + stripDomainPrefixes( + 'https://user:password@www.test-this.com:1020/test-path/file.extension#anchor?query=params&more' + ) + ).toBe( + 'user:password@www.test-this.com:1020/test-path/file.extension#anchor?query=params&more' + ); + expect( stripDomainPrefixes( 'https://www.example.com#section' ) ).toBe( + 'example.com#section' + ); + expect( + stripDomainPrefixes( + 'https://www.example.com?param1=value1¶m2=value2' + ) + ).toBe( 'example.com?param1=value1¶m2=value2' ); + } ); +} ); + describe( 'getAuthority', () => { it( 'returns the authority part of a URL', () => { expect( From 2bf7afae3b95066f03c449d544719814cd15c660 Mon Sep 17 00:00:00 2001 From: Amit Raj Date: Mon, 3 Jun 2024 18:35:54 +0530 Subject: [PATCH 05/11] Refactor URL filtering logic --- .../components/link-control/link-preview.js | 21 +++++-- packages/url/README.md | 12 ---- packages/url/src/filter-url-for-display.js | 24 +++---- packages/url/src/index.js | 1 - packages/url/src/test/index.js | 63 ------------------- 5 files changed, 22 insertions(+), 99 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 4afda7358d4f7..11d963f41633f 100644 --- a/packages/block-editor/src/components/link-control/link-preview.js +++ b/packages/block-editor/src/components/link-control/link-preview.js @@ -13,11 +13,7 @@ import { __experimentalTruncate as Truncate, } from '@wordpress/components'; import { useCopyToClipboard } from '@wordpress/compose'; -import { - filterURLForDisplay, - safeDecodeURI, - stripDomainPrefixes, -} from '@wordpress/url'; +import { filterURLForDisplay, safeDecodeURI } from '@wordpress/url'; import { Icon, globe, info, linkOff, edit, copySmall } from '@wordpress/icons'; import { __unstableStripHTML as stripHTML } from '@wordpress/dom'; import { useDispatch, useSelect } from '@wordpress/data'; @@ -63,8 +59,21 @@ export default function LinkPreview( { ! isEmptyURL && stripHTML( richData?.title || value?.title || displayURL ); + /** + * Filters the title for display. Removes the protocol and www prefix. + * + * @param {string} title - The title to be filtered. + * + * @return {string} The filtered title. + */ + function filterTitleForDisplay( title ) { + return title + .replace( /^[a-z\-.\+]+[0-9]*:(\/\/)?/i, '' ) + .replace( /^www\./i, '' ); + } + const isTitleRedundant = - value?.url && stripDomainPrefixes( displayTitle ) === displayURL; + value?.url && filterTitleForDisplay( displayTitle ) === displayURL; let icon; diff --git a/packages/url/README.md b/packages/url/README.md index 818b4af5a5394..70e8579acc518 100644 --- a/packages/url/README.md +++ b/packages/url/README.md @@ -543,18 +543,6 @@ _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 24a847c9beae4..436070b667a3e 100644 --- a/packages/url/src/filter-url-for-display.js +++ b/packages/url/src/filter-url-for-display.js @@ -13,8 +13,14 @@ * @return {string} Displayed URL. */ export function filterURLForDisplay( url, maxLength = null ) { + if ( ! url ) { + return ''; + } + // Remove protocol and www prefixes. - let filteredURL = stripDomainPrefixes( url ); + let filteredURL = url + .replace( /^[a-z\-.\+]+[0-9]*:(\/\/)?/i, '' ) + .replace( /^www\./i, '' ); // Ends with / and only has that single slash, strip it. if ( filteredURL.match( /^[^\/]+\/$/ ) ) { @@ -53,19 +59,3 @@ export function filterURLForDisplay( url, maxLength = null ) { truncatedFile ); } - -/** - * Removes domain prefixes from a URL. - * - * @param {string} url The URL to strip domain prefixes from. - * - * @return {string} The URL without domain prefixes. - */ -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 868abc1c46806..141e19d3fa55f 100644 --- a/packages/url/src/index.js +++ b/packages/url/src/index.js @@ -25,4 +25,3 @@ export { cleanForSlug } from './clean-for-slug'; export { getFilename } from './get-filename'; export { normalizePath } from './normalize-path'; export { prependHTTPS } from './prepend-https'; -export { stripDomainPrefixes } from './filter-url-for-display'; diff --git a/packages/url/src/test/index.js b/packages/url/src/test/index.js index ad059529bc514..0051b9b89fc73 100644 --- a/packages/url/src/test/index.js +++ b/packages/url/src/test/index.js @@ -27,7 +27,6 @@ import { prependHTTPS, removeQueryArgs, safeDecodeURI, - stripDomainPrefixes, } from '../'; import wptData from './fixtures/wpt-data'; @@ -128,68 +127,6 @@ describe( 'isValidProtocol', () => { } ); } ); -describe( 'stripDomainPrefixes', () => { - it( 'should remove the protocol (http, https, file, tel, etc.) from the URL', () => { - expect( stripDomainPrefixes( 'http://wordpress.org' ) ).toBe( - 'wordpress.org' - ); - expect( stripDomainPrefixes( 'https://wordpress.org' ) ).toBe( - 'wordpress.org' - ); - expect( stripDomainPrefixes( 'https://localhost:8080' ) ).toBe( - 'localhost:8080' - ); - expect( stripDomainPrefixes( 'file:///folder/file.txt' ) ).toBe( - '/folder/file.txt' - ); - expect( stripDomainPrefixes( 'ftp://wordpress.org' ) ).toBe( - 'wordpress.org' - ); - expect( stripDomainPrefixes( 'tel:0123456789' ) ).toBe( '0123456789' ); - expect( stripDomainPrefixes( 'blob:data' ) ).toBe( 'data' ); - } ); - - it( 'should remove both the protocol and www prefix if both are present', () => { - expect( stripDomainPrefixes( 'http://www.google.com' ) ).toBe( - 'google.com' - ); - expect( stripDomainPrefixes( 'https://www.google.com' ) ).toBe( - 'google.com' - ); - expect( stripDomainPrefixes( 'ftp://www.google.com' ) ).toBe( - 'google.com' - ); - } ); - - it( 'should handle URLs without a protocol', () => { - expect( stripDomainPrefixes( 'www.example.com' ) ).toBe( - 'example.com' - ); - expect( stripDomainPrefixes( 'example.com' ) ).toBe( 'example.com' ); - expect( stripDomainPrefixes( 'example.com/path' ) ).toBe( - 'example.com/path' - ); - } ); - - it( 'should handle URLs with both parameters and fragments correctly', () => { - expect( - stripDomainPrefixes( - 'https://user:password@www.test-this.com:1020/test-path/file.extension#anchor?query=params&more' - ) - ).toBe( - 'user:password@www.test-this.com:1020/test-path/file.extension#anchor?query=params&more' - ); - expect( stripDomainPrefixes( 'https://www.example.com#section' ) ).toBe( - 'example.com#section' - ); - expect( - stripDomainPrefixes( - 'https://www.example.com?param1=value1¶m2=value2' - ) - ).toBe( 'example.com?param1=value1¶m2=value2' ); - } ); -} ); - describe( 'getAuthority', () => { it( 'returns the authority part of a URL', () => { expect( From d0f9da355ccbfbccb9711aa53b961ce63c9d7da4 Mon Sep 17 00:00:00 2001 From: Amit Raj Date: Mon, 3 Jun 2024 18:52:51 +0530 Subject: [PATCH 06/11] Change variable name --- .../block-editor/src/components/link-control/link-preview.js | 4 ++-- 1 file changed, 2 insertions(+), 2 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 11d963f41633f..35097c7fbdb84 100644 --- a/packages/block-editor/src/components/link-control/link-preview.js +++ b/packages/block-editor/src/components/link-control/link-preview.js @@ -72,7 +72,7 @@ export default function LinkPreview( { .replace( /^www\./i, '' ); } - const isTitleRedundant = + const isUrlRedundant = value?.url && filterTitleForDisplay( displayTitle ) === displayURL; let icon; @@ -128,7 +128,7 @@ export default function LinkPreview( { { displayTitle } - { ! isTitleRedundant && ( + { ! isUrlRedundant && ( { displayURL } From adb7a1fc698d934bb725b246e128d2ae8f543990 Mon Sep 17 00:00:00 2001 From: Amit Raj Date: Tue, 4 Jun 2024 17:24:35 +0530 Subject: [PATCH 07/11] Moved filterTitleForDisplay outside of component --- .../components/link-control/link-preview.js | 27 ++++++++++--------- 1 file changed, 14 insertions(+), 13 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 35097c7fbdb84..3a9ced64d9d61 100644 --- a/packages/block-editor/src/components/link-control/link-preview.js +++ b/packages/block-editor/src/components/link-control/link-preview.js @@ -27,6 +27,20 @@ import { ViewerSlot } from './viewer-slot'; import useRichUrlData from './use-rich-url-data'; +/** + * Filters the title for display. Removes the protocol and www prefix. + * + * @param {string} title The title to be filtered. + * + * @return {string} The filtered title. + */ +function filterTitleForDisplay( title ) { + // Dervied from `filterURLForDisplay` in `@wordpress/url`. + return title + .replace( /^[a-z\-.\+]+[0-9]*:(\/\/)?/i, '' ) + .replace( /^www\./i, '' ); +} + export default function LinkPreview( { value, onEditClick, @@ -59,19 +73,6 @@ export default function LinkPreview( { ! isEmptyURL && stripHTML( richData?.title || value?.title || displayURL ); - /** - * Filters the title for display. Removes the protocol and www prefix. - * - * @param {string} title - The title to be filtered. - * - * @return {string} The filtered title. - */ - function filterTitleForDisplay( title ) { - return title - .replace( /^[a-z\-.\+]+[0-9]*:(\/\/)?/i, '' ) - .replace( /^www\./i, '' ); - } - const isUrlRedundant = value?.url && filterTitleForDisplay( displayTitle ) === displayURL; From 44f30194245f3f89aa097b2017ab0222a7c6d080 Mon Sep 17 00:00:00 2001 From: Amit Raj Date: Tue, 4 Jun 2024 17:25:06 +0530 Subject: [PATCH 08/11] Adds test cases for the updated regex --- packages/url/src/test/index.js | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/packages/url/src/test/index.js b/packages/url/src/test/index.js index 0051b9b89fc73..1094947308e14 100644 --- a/packages/url/src/test/index.js +++ b/packages/url/src/test/index.js @@ -997,6 +997,12 @@ describe( 'filterURLForDisplay', () => { expect( url ).toBe( 'wordpress.org' ); url = filterURLForDisplay( 'https://wordpress.org' ); expect( url ).toBe( 'wordpress.org' ); + url = filterURLForDisplay( 'file:///folder/file.txt' ); + expect( url ).toBe( '/folder/file.txt' ); + url = filterURLForDisplay( 'tel:0123456789' ); + expect( url ).toBe( '0123456789' ); + url = filterURLForDisplay( 'blob:data' ); + expect( url ).toBe( 'data' ); } ); it( 'should remove www subdomain', () => { const url = filterURLForDisplay( 'http://www.wordpress.org' ); @@ -1030,18 +1036,28 @@ describe( 'filterURLForDisplay', () => { expect( url ).toBe( 'wordpress.org/ig.jpg' ); } ); it( 'should return ellipsis, upper level pieces url, and filename when the url is long enough but filename is short enough', () => { - const url = filterURLForDisplay( + let url = filterURLForDisplay( 'http://www.wordpress.org/wp-content/uploads/myimage.jpg', 20 ); expect( url ).toBe( '…/uploads/myimage.jpg' ); + url = filterURLForDisplay( + 'file:///home/user/documents/project/reports/2024/myfile.pdf', + 20 + ); + expect( url ).toBe( '…orts/2024/myfile.pdf' ); } ); it( 'should return filename split by ellipsis plus three characters when filename is long enough', () => { - const url = filterURLForDisplay( + let url = filterURLForDisplay( 'http://www.wordpress.org/wp-content/uploads/superlongtitlewithextension.jpeg', 20 ); expect( url ).toBe( 'superlongti…ion.jpeg' ); + url = filterURLForDisplay( + 'file:///home/user/documents/project/reports/2024/superlongtitlewithextension.pdf', + 20 + ); + expect( url ).toBe( 'superlongtit…ion.pdf' ); } ); it( 'should remove query arguments', () => { const url = filterURLForDisplay( @@ -1058,11 +1074,16 @@ describe( 'filterURLForDisplay', () => { expect( url ).toBe( 'wordpress.org/wp-content/url/' ); } ); it( 'should return file split by ellipsis when the file name has multiple periods', () => { - const url = filterURLForDisplay( + let url = filterURLForDisplay( 'http://www.wordpress.org/wp-content/uploads/filename.2020.12.20.png', 20 ); expect( url ).toBe( 'filename.202….20.png' ); + url = filterURLForDisplay( + 'file:///home/user/documents/project/reports/2024/filename.2020.12.20.png', + 20 + ); + expect( url ).toBe( 'filename.202….20.png' ); } ); } ); From a0c42c878cc9eeccb333b979f5c76af78575928b Mon Sep 17 00:00:00 2001 From: Amit Raj Date: Thu, 6 Jun 2024 16:20:21 +0530 Subject: [PATCH 09/11] Fix typos and remove redundant tests for filterURLForDisplay --- .../components/link-control/link-preview.js | 2 +- packages/url/src/test/index.js | 21 +++---------------- 2 files changed, 4 insertions(+), 19 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 44751351d30f2..6ee90630d6d94 100644 --- a/packages/block-editor/src/components/link-control/link-preview.js +++ b/packages/block-editor/src/components/link-control/link-preview.js @@ -35,7 +35,7 @@ import useRichUrlData from './use-rich-url-data'; * @return {string} The filtered title. */ function filterTitleForDisplay( title ) { - // Dervied from `filterURLForDisplay` in `@wordpress/url`. + // Derived from `filterURLForDisplay` in `@wordpress/url`. return title .replace( /^[a-z\-.\+]+[0-9]*:(\/\/)?/i, '' ) .replace( /^www\./i, '' ); diff --git a/packages/url/src/test/index.js b/packages/url/src/test/index.js index 1094947308e14..950746f0aa9e5 100644 --- a/packages/url/src/test/index.js +++ b/packages/url/src/test/index.js @@ -1036,28 +1036,18 @@ describe( 'filterURLForDisplay', () => { expect( url ).toBe( 'wordpress.org/ig.jpg' ); } ); it( 'should return ellipsis, upper level pieces url, and filename when the url is long enough but filename is short enough', () => { - let url = filterURLForDisplay( + const url = filterURLForDisplay( 'http://www.wordpress.org/wp-content/uploads/myimage.jpg', 20 ); expect( url ).toBe( '…/uploads/myimage.jpg' ); - url = filterURLForDisplay( - 'file:///home/user/documents/project/reports/2024/myfile.pdf', - 20 - ); - expect( url ).toBe( '…orts/2024/myfile.pdf' ); } ); it( 'should return filename split by ellipsis plus three characters when filename is long enough', () => { - let url = filterURLForDisplay( + const url = filterURLForDisplay( 'http://www.wordpress.org/wp-content/uploads/superlongtitlewithextension.jpeg', 20 ); expect( url ).toBe( 'superlongti…ion.jpeg' ); - url = filterURLForDisplay( - 'file:///home/user/documents/project/reports/2024/superlongtitlewithextension.pdf', - 20 - ); - expect( url ).toBe( 'superlongtit…ion.pdf' ); } ); it( 'should remove query arguments', () => { const url = filterURLForDisplay( @@ -1074,16 +1064,11 @@ describe( 'filterURLForDisplay', () => { expect( url ).toBe( 'wordpress.org/wp-content/url/' ); } ); it( 'should return file split by ellipsis when the file name has multiple periods', () => { - let url = filterURLForDisplay( + const url = filterURLForDisplay( 'http://www.wordpress.org/wp-content/uploads/filename.2020.12.20.png', 20 ); expect( url ).toBe( 'filename.202….20.png' ); - url = filterURLForDisplay( - 'file:///home/user/documents/project/reports/2024/filename.2020.12.20.png', - 20 - ); - expect( url ).toBe( 'filename.202….20.png' ); } ); } ); From a8291bf7f9363619ee8839a501033bd012c0b851 Mon Sep 17 00:00:00 2001 From: Amit Raj Date: Thu, 6 Jun 2024 16:46:18 +0530 Subject: [PATCH 10/11] Fix isUrlRedundant check in LinkPreview component --- .../block-editor/src/components/link-control/link-preview.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 6ee90630d6d94..d06c9971c680b 100644 --- a/packages/block-editor/src/components/link-control/link-preview.js +++ b/packages/block-editor/src/components/link-control/link-preview.js @@ -74,7 +74,7 @@ export default function LinkPreview( { stripHTML( richData?.title || value?.title || displayURL ); const isUrlRedundant = - value?.url && filterTitleForDisplay( displayTitle ) === displayURL; + ! value?.url || filterTitleForDisplay( displayTitle ) === displayURL; let icon; From 6a974d37a0942872d657df1f2e7ff5245da20d02 Mon Sep 17 00:00:00 2001 From: Amit Raj Date: Fri, 7 Jun 2024 17:21:26 +0530 Subject: [PATCH 11/11] Add test case for empty or falsy URL in filterURLForDisplay function --- packages/url/src/test/index.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/url/src/test/index.js b/packages/url/src/test/index.js index 950746f0aa9e5..bd48105baed96 100644 --- a/packages/url/src/test/index.js +++ b/packages/url/src/test/index.js @@ -992,6 +992,12 @@ describe( 'safeDecodeURI', () => { } ); describe( 'filterURLForDisplay', () => { + it( 'should return an empty string if the url is empty or falsy', () => { + let url = filterURLForDisplay( '' ); + expect( url ).toBe( '' ); + url = filterURLForDisplay( null ); + expect( url ).toBe( '' ); + } ); it( 'should remove protocol', () => { let url = filterURLForDisplay( 'http://wordpress.org' ); expect( url ).toBe( 'wordpress.org' );