Skip to content

Commit

Permalink
Refactor URL filtering logic
Browse files Browse the repository at this point in the history
  • Loading branch information
amitraj2203 committed Jun 3, 2024
1 parent 2860362 commit 2bf7afa
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 99 deletions.
21 changes: 15 additions & 6 deletions packages/block-editor/src/components/link-control/link-preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;

Expand Down
12 changes: 0 additions & 12 deletions packages/url/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

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

## Contributing to this package
Expand Down
24 changes: 7 additions & 17 deletions packages/url/src/filter-url-for-display.js
Original file line number Diff line number Diff line change
Expand Up @@ -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( /^[^\/]+\/$/ ) ) {
Expand Down Expand Up @@ -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, '' );
}
1 change: 0 additions & 1 deletion packages/url/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
63 changes: 0 additions & 63 deletions packages/url/src/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import {
prependHTTPS,
removeQueryArgs,
safeDecodeURI,
stripDomainPrefixes,
} from '../';
import wptData from './fixtures/wpt-data';

Expand Down Expand Up @@ -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:[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

0 comments on commit 2bf7afa

Please sign in to comment.