Skip to content

Commit

Permalink
URL: return early in getFilename where url argument is falsy (WordPre…
Browse files Browse the repository at this point in the history
…ss#60265)

* Return early for falsy values so the URL constructor doesn't return `'undefined' <string>` which could be interpreted by consumers as truthy


Co-authored-by: ramonjd <[email protected]>
Co-authored-by: andrewserong <[email protected]>
  • Loading branch information
3 people authored and cbravobernal committed Apr 9, 2024
1 parent eb435ea commit 8709d13
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/url/src/get-filename.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
*/
export function getFilename( url ) {
let filename;

if ( ! url ) {
return;
}

try {
filename = new URL( url, 'http://example.com' ).pathname
.split( '/' )
Expand Down
2 changes: 2 additions & 0 deletions packages/url/src/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,8 @@ describe( 'getFilename', () => {
);
expect( getFilename( 'a/path/' ) ).toBe( undefined );
expect( getFilename( '/' ) ).toBe( undefined );
expect( getFilename( undefined ) ).toBe( undefined );
expect( getFilename( null ) ).toBe( undefined );
} );
} );

Expand Down

0 comments on commit 8709d13

Please sign in to comment.