Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LinkControl: refined the display of the link preview title and url when both are same #61819

Merged
merged 16 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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,
stripDomainPrefixes,
} 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';
Expand Down Expand Up @@ -59,6 +63,9 @@ export default function LinkPreview( {
! isEmptyURL &&
stripHTML( richData?.title || value?.title || displayURL );

const isTitleRedundant =
value?.url && stripDomainPrefixes( displayTitle ) === displayURL;

let icon;

if ( richData?.icon ) {
Expand Down Expand Up @@ -112,7 +119,7 @@ export default function LinkPreview( {
{ displayTitle }
</Truncate>
</ExternalLink>
{ value?.url && displayTitle !== displayURL && (
{ ! isTitleRedundant && (
<span className="block-editor-link-control__search-item-info">
<Truncate numberOfLines={ 1 }>
{ displayURL }
Expand Down
12 changes: 12 additions & 0 deletions packages/url/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -543,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.

<!-- END TOKEN(Autogenerated API docs) -->

## Contributing to this package
Expand Down
18 changes: 17 additions & 1 deletion packages/url/src/filter-url-for-display.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
export function filterURLForDisplay( url, maxLength = null ) {
// Remove protocol and www prefixes.
let filteredURL = url.replace( /^(?:https?:)\/\/(?:www\.)?/, '' );
let filteredURL = stripDomainPrefixes( url );

// Ends with / and only has that single slash, strip it.
if ( filteredURL.match( /^[^\/]+\/$/ ) ) {
Expand Down Expand Up @@ -53,3 +53,19 @@ 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, '' );
}
1 change: 1 addition & 0 deletions packages/url/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 { stripDomainPrefixes } from './filter-url-for-display';
63 changes: 63 additions & 0 deletions packages/url/src/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
prependHTTPS,
removeQueryArgs,
safeDecodeURI,
stripDomainPrefixes,
} from '../';
import wptData from './fixtures/wpt-data';

Expand Down Expand Up @@ -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:[email protected]:1020/test-path/file.extension#anchor?query=params&more'
)
).toBe(
'user:[email protected]: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&param2=value2'
)
).toBe( 'example.com?param1=value1&param2=value2' );
} );
} );

describe( 'getAuthority', () => {
it( 'returns the authority part of a URL', () => {
expect(
Expand Down
Loading