Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
@@ -0,0 +1,42 @@
import { describe, expect, it } from 'vitest';
import { render, screen, within } from '@testing-library/react';
import FilePreview from '../file-preview';

describe( 'FilePreview', () => {
it( 'shows the filename of the media', () => {
render(
<FilePreview
url="https://example.com/wp-content/uploads/2026/11/wave.mp3"
mediaType="audio"
/>
);

expect( screen.getByText( 'wave.mp3' ) ).toBeVisible();
} );

it( 'shows a filename with non-ASCII characters as written, not percent-encoded', () => {
render(
<FilePreview
url="https://example.com/wp-content/uploads/2026/11/%E7%94%9F%E6%88%90%E6%B3%A2%E3%81%AE%E9%9F%B31.mp3"
mediaType="audio"
/>
);

expect( screen.getByText( '生成波の音1.mp3' ) ).toBeVisible();
expect( screen.queryByText( /%E7/ ) ).not.toBeInTheDocument();
} );

it( 'renders only the icon when the URL has no filename', () => {
const { container } = render(
<FilePreview
url="https://example.com/wp-content/uploads/"
mediaType="audio"
/>
);

// The icon has no text, so nothing should render a name.
expect(
within( container ).queryByText( /\S/ )
).not.toBeInTheDocument();
} );
} );
4 changes: 4 additions & 0 deletions packages/url/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Bug Fixes

- `getFilename`: Decode the filename so non-ASCII names read as written ([#86422](https://github.com/WordPress/gutenberg/pull/86422)).

## 5.1.0 (2026-11-04)

## 5.0.0 (2026-10-20)
Expand Down
3 changes: 2 additions & 1 deletion packages/url/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,14 @@ _Returns_

### getFilename

Returns the filename part of the URL.
Returns the filename part of the URL, decoded so that percent-encoded characters read as they would in a file manager. A filename that can't be decoded is returned as it appears in the URL.

_Usage_

```js
const filename1 = getFilename( 'http://localhost:8080/this/is/a/test.jpg' ); // 'test.jpg'
const filename2 = getFilename( '/this/is/a/test.png' ); // 'test.png'
const filename3 = getFilename( '/uploads/%E9%9F%B3.mp3' ); // '音.mp3'
```

_Parameters_
Expand Down
11 changes: 9 additions & 2 deletions packages/url/src/get-filename.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { safeDecodeURIComponent } from './safe-decode-uri-component';

/**
* Returns the filename part of the URL.
* Returns the filename part of the URL, decoded so that percent-encoded
* characters read as they would in a file manager. A filename that can't be
* decoded is returned as it appears in the URL.
*
* @param url The full URL.
*
* @example
* ```js
* const filename1 = getFilename( 'http://localhost:8080/this/is/a/test.jpg' ); // 'test.jpg'
* const filename2 = getFilename( '/this/is/a/test.png' ); // 'test.png'
* const filename3 = getFilename( '/uploads/%E9%9F%B3.mp3' ); // '音.mp3'
* ```
*
* @return The filename part of the URL.
Expand All @@ -25,6 +30,8 @@ export function getFilename( url: string ): string | void {
} catch {}

if ( filename ) {
return filename;
// Decode after splitting the path, so an encoded slash (`%2F`) stays
// part of the filename instead of being treated as a separator.
return safeDecodeURIComponent( filename );
}
}
23 changes: 23 additions & 0 deletions packages/url/src/test/index.jsdom.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,29 @@ describe( 'getFilename', () => {
expect( getFilename( url ) ).toBe( filename );
} );

it.each( [
[
'https://wordpress.org/uploads/%E7%94%9F%E6%88%90%E6%B3%A2%E3%81%AE%E9%9F%B31.mp3',
'生成波の音1.mp3',
],
[ 'https://wordpress.org/uploads/my%20photo.jpg', 'my photo.jpg' ],
[ '/uploads/caf%C3%A9.png?query=%E9%9F%B3#%E9%9F%B3', 'café.png' ],
[ 'https://wordpress.org/uploads/生成波の音2.wav', '生成波の音2.wav' ],
[ 'https://wordpress.org/uploads/a%2Fb.jpg', 'a/b.jpg' ],
[ 'https://wordpress.org/uploads/100%25.jpg', '100%.jpg' ],
] )(
'decodes percent-encoded characters in the filename: %s',
( url, filename ) => {
expect( getFilename( url ) ).toBe( filename );
}
);

it( 'returns the filename as is when it can not be decoded', () => {
expect(
getFilename( 'https://wordpress.org/uploads/%E9%9F.mp3' )
).toBe( '%E9%9F.mp3' );
} );

it( 'returns undefined when the provided value does not contain a filename', () => {
expect( getFilename( 'http://localhost:8080/' ) ).toBe( undefined );
expect( getFilename( 'http://localhost:8080/a/path/' ) ).toBe(
Expand Down
Loading