diff --git a/packages/block-editor/src/components/inserter/media-tab/test/file-preview.jsdom.test.tsx b/packages/block-editor/src/components/inserter/media-tab/test/file-preview.jsdom.test.tsx new file mode 100644 index 00000000000000..dde6bf02cac15c --- /dev/null +++ b/packages/block-editor/src/components/inserter/media-tab/test/file-preview.jsdom.test.tsx @@ -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( + + ); + + expect( screen.getByText( 'wave.mp3' ) ).toBeVisible(); + } ); + + it( 'shows a filename with non-ASCII characters as written, not percent-encoded', () => { + render( + + ); + + 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( + + ); + + // The icon has no text, so nothing should render a name. + expect( + within( container ).queryByText( /\S/ ) + ).not.toBeInTheDocument(); + } ); +} ); diff --git a/packages/url/CHANGELOG.md b/packages/url/CHANGELOG.md index 6be9a3e9f2b0c3..98840cafc4e1fd 100644 --- a/packages/url/CHANGELOG.md +++ b/packages/url/CHANGELOG.md @@ -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) diff --git a/packages/url/README.md b/packages/url/README.md index dbd9bfc538928a..2cc53dd8025775 100644 --- a/packages/url/README.md +++ b/packages/url/README.md @@ -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_ diff --git a/packages/url/src/get-filename.ts b/packages/url/src/get-filename.ts index e9d187266be305..4fa714135659c9 100644 --- a/packages/url/src/get-filename.ts +++ b/packages/url/src/get-filename.ts @@ -1,5 +1,9 @@ +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. * @@ -7,6 +11,7 @@ * ```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. @@ -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 ); } } diff --git a/packages/url/src/test/index.jsdom.test.ts b/packages/url/src/test/index.jsdom.test.ts index 6dca967930c167..b6994624a6c0f1 100644 --- a/packages/url/src/test/index.jsdom.test.ts +++ b/packages/url/src/test/index.jsdom.test.ts @@ -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(