From f2f89ed68dfc9eab6d2bf4ee591180364db51951 Mon Sep 17 00:00:00 2001
From: Chang Hsing-Yu <56378160+hsingyuc@users.noreply.github.com>
Date: Tue, 22 Dec 2020 14:29:39 -0500
Subject: [PATCH] URL: Enhance the way long URLs are handled in
filterURLForDisplay (#27530)
* Remove css ellipsis.
* Add unit tests for truncating url.
* Add argument to truncate url.
* Update changelog.
---
.../components/link-control/link-preview.js | 3 +-
.../src/components/link-control/style.scss | 1 -
packages/url/CHANGELOG.md | 4 ++
packages/url/README.md | 2 +
packages/url/src/filter-url-for-display.js | 39 +++++++++++++--
packages/url/src/test/index.test.js | 48 +++++++++++++++++++
6 files changed, 91 insertions(+), 6 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 673ba1a7a7449..ef6bd09fcf270 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', () => {