From 99b8ea65430fcb3d0c4cd75083e39b7cfe36a700 Mon Sep 17 00:00:00 2001 From: Ramon Date: Thu, 28 Mar 2024 12:55:47 +1100 Subject: [PATCH] URL: return early in getFilename where url argument is falsy (#60265) * Return early for falsy values so the URL constructor doesn't return `'undefined' ` which could be interpreted by consumers as truthy Co-authored-by: ramonjd Co-authored-by: andrewserong --- packages/url/src/get-filename.js | 5 +++++ packages/url/src/test/index.js | 2 ++ 2 files changed, 7 insertions(+) diff --git a/packages/url/src/get-filename.js b/packages/url/src/get-filename.js index 2941f18fe07b4d..d744a8da6bccfe 100644 --- a/packages/url/src/get-filename.js +++ b/packages/url/src/get-filename.js @@ -13,6 +13,11 @@ */ export function getFilename( url ) { let filename; + + if ( ! url ) { + return; + } + try { filename = new URL( url, 'http://example.com' ).pathname .split( '/' ) diff --git a/packages/url/src/test/index.js b/packages/url/src/test/index.js index e258e112b29875..0051b9b89fc73e 100644 --- a/packages/url/src/test/index.js +++ b/packages/url/src/test/index.js @@ -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 ); } ); } );