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

url: update filterURLForDisplay to include all image, video, and audio file types #54920

Merged
merged 11 commits into from
Oct 11, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
Button,
ExternalLink,
__experimentalText as Text,
Tooltip,
} from '@wordpress/components';
import { filterURLForDisplay, safeDecodeURI } from '@wordpress/url';
import { Icon, globe, info, linkOff, edit } from '@wordpress/icons';
Expand Down Expand Up @@ -87,12 +88,17 @@ export default function LinkPreview( {
<span className="block-editor-link-control__search-item-details">
{ ! isEmptyURL ? (
<>
<ExternalLink
className="block-editor-link-control__search-item-title"
href={ value.url }
<Tooltip
text={ value.url }
placement="bottom-start"
>
{ displayTitle }
</ExternalLink>
<ExternalLink
className="block-editor-link-control__search-item-title"
href={ value.url }
>
{ displayTitle }
</ExternalLink>
</Tooltip>

{ value?.url && displayTitle !== displayURL && (
<span className="block-editor-link-control__search-item-info">
Expand Down
19 changes: 19 additions & 0 deletions packages/block-editor/src/components/link-control/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2056,6 +2056,25 @@ describe( 'Addition Settings UI', () => {
} )
);
} );

it( 'should show tooltip with full URL alongside filtered display', async () => {
const user = userEvent.setup();
const url =
'http://www.wordpress.org/wp-content/uploads/a-document.pdf';
render( <LinkControl value={ { url } } /> );

const link = screen.getByRole( 'link' );

expect( link ).toHaveTextContent( 'a-document.pdf' );

await user.hover( link );

expect( await screen.findByRole( 'tooltip' ) ).toHaveTextContent( url );

await user.unhover( link );

expect( screen.queryByRole( 'tooltip' ) ).not.toBeInTheDocument();
} );
} );

describe( 'Post types', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
ToolbarButton,
Dropdown,
withFilters,
Tooltip,
} from '@wordpress/components';
import { useSelect, withDispatch } from '@wordpress/data';
import { DOWN } from '@wordpress/keycodes';
Expand Down Expand Up @@ -220,19 +219,15 @@ const MediaReplaceFlow = ( {
{ __( 'Current media URL:' ) }
</span>

<Tooltip text={ mediaURL }>
<div>
<LinkControl
value={ { url: mediaURL } }
settings={ [] }
showSuggestions={ false }
onChange={ ( { url } ) => {
onSelectURL( url );
editMediaButtonRef.current.focus();
} }
/>
</div>
</Tooltip>
<LinkControl
value={ { url: mediaURL } }
settings={ [] }
showSuggestions={ false }
onChange={ ( { url } ) => {
onSelectURL( url );
editMediaButtonRef.current.focus();
} }
/>
</form>
) }
</>
Expand Down
5 changes: 3 additions & 2 deletions packages/url/src/filter-url-for-display.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ export function filterURLForDisplay( url, maxLength = null ) {
filteredURL = filteredURL.replace( '/', '' );
}

const mediaRegexp = /([\w|:])*\.(?:jpg|jpeg|gif|png|svg)/;
// capture file name from URL
const fileRegexp = /\/([^\/?]+)\.(?:[\w]+)(?=\?|$)/;

if (
! maxLength ||
filteredURL.length <= maxLength ||
! filteredURL.match( mediaRegexp )
! filteredURL.match( fileRegexp )
) {
return filteredURL;
}
Expand Down
31 changes: 14 additions & 17 deletions packages/url/src/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,23 +254,20 @@ describe( 'isValidPath', () => {
} );

describe( 'getFilename', () => {
it( 'returns the filename part of the URL', () => {
expect( getFilename( 'https://wordpress.org/image.jpg' ) ).toBe(
'image.jpg'
);
expect(
getFilename( 'https://wordpress.org/image.jpg?query=test' )
).toBe( 'image.jpg' );
expect( getFilename( 'https://wordpress.org/image.jpg#anchor' ) ).toBe(
'image.jpg'
);
expect(
getFilename( 'http://localhost:8080/a/path/to/an/image.jpg' )
).toBe( 'image.jpg' );
expect( getFilename( '/path/to/an/image.jpg' ) ).toBe( 'image.jpg' );
expect( getFilename( 'path/to/an/image.jpg' ) ).toBe( 'image.jpg' );
expect( getFilename( '/image.jpg' ) ).toBe( 'image.jpg' );
expect( getFilename( 'image.jpg' ) ).toBe( 'image.jpg' );
it.each( [
[ 'https://wordpress.org/image.jpg', 'image.jpg' ],
[ 'https://wordpress.org/image.jpg?query=test', 'image.jpg' ],
[ 'https://wordpress.org/image.jpg#anchor', 'image.jpg' ],
[ 'http://localhost:8080/a/path/to/an/image.jpg', 'image.jpg' ],
[ '/path/to/an/image.jpg', 'image.jpg' ],
[ 'path/to/an/image.jpg', 'image.jpg' ],
[ '/image.jpg', 'image.jpg' ],
[ 'https://wordpress.org/file.pdf', 'file.pdf' ],
[ 'https://wordpress.org/image.webp?query=test', 'image.webp' ],
[ 'https://wordpress.org/video.mov#anchor', 'video.mov' ],
[ 'http://localhost:8080/a/path/to/audio.mp3', 'audio.mp3' ],
] )( 'returns the filename part of the URL: %s', ( url, filename ) => {
expect( getFilename( url ) ).toBe( filename );
} );

it( 'returns undefined when the provided value does not contain a filename', () => {
Expand Down
Loading