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 673ba1a7a7449a..ef6bd09fcf270f 100644 --- a/packages/block-editor/src/components/link-control/link-preview.js +++ b/packages/block-editor/src/components/link-control/link-preview.js @@ -17,7 +17,8 @@ import { ViewerSlot } from './viewer-slot'; export default function LinkPreview( { value, onEditClick } ) { const displayURL = - ( value && filterURLForDisplay( safeDecodeURI( value.url ) ) ) || ''; + ( value && filterURLForDisplay( safeDecodeURI( value.url ), 16 ) ) || + ''; return (
{ const url = filterURLForDisplay( 'http://www.wordpress.org/something' ); expect( url ).toBe( 'wordpress.org/something' ); } ); + it( 'should preserve the original url if no argument max length', () => { + const url = filterURLForDisplay( + 'http://www.wordpress.org/wp-content/uploads/myimage.jpg' + ); + expect( url ).toBe( 'wordpress.org/wp-content/uploads/myimage.jpg' ); + } ); + it( 'should preserve the original url if the url is short enough', () => { + const url = filterURLForDisplay( + 'http://www.wordpress.org/ig.jpg', + 20 + ); + 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( + 'http://www.wordpress.org/wp-content/uploads/myimage.jpg', + 20 + ); + expect( url ).toBe( '…/uploads/myimage.jpg' ); + } ); + it( 'should return filename split by ellipsis plus three characters when filename is long enough', () => { + const url = filterURLForDisplay( + 'http://www.wordpress.org/wp-content/uploads/superlongtitlewithextension.jpeg', + 20 + ); + expect( url ).toBe( 'superlongti…ion.jpeg' ); + } ); + it( 'should remove query arguments', () => { + const url = filterURLForDisplay( + 'http://www.wordpress.org/wp-content/uploads/myimage.jpeg?query_args=a', + 20 + ); + expect( url ).toBe( '…uploads/myimage.jpeg' ); + } ); + it( 'should preserve the original url when it is not a file', () => { + const url = filterURLForDisplay( + 'http://www.wordpress.org/wp-content/url/', + 20 + ); + 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( + 'http://www.wordpress.org/wp-content/uploads/filename.2020.12.20.png', + 20 + ); + expect( url ).toBe( 'filename.202….20.png' ); + } ); } ); describe( 'cleanForSlug', () => {