From 40f556080a7d0559f6fcabfdb6e632e93c8de7b3 Mon Sep 17 00:00:00 2001 From: Sergio Estevao Date: Tue, 25 Feb 2020 13:20:12 +0000 Subject: [PATCH 1/4] Fixed isURL regex to take account file urls. Added unit test to validate isURL when parsing file URLs. --- packages/url/src/is-url.native.js | 2 +- packages/url/src/test/index.test.js | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/url/src/is-url.native.js b/packages/url/src/is-url.native.js index 012a93fb9a2fc0..cbac7a05e2b69f 100644 --- a/packages/url/src/is-url.native.js +++ b/packages/url/src/is-url.native.js @@ -1,4 +1,4 @@ -const URL_REGEXP = /((([A-Za-z]{3,9}:(?:\/\/)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)(?::\d{2,5})?((?:\/[\+~%\/\.\w\-_]*)?\??(?:[\-\+=&;%@\.\w_]*)#?(?:[\.\!\/\\\w]*))?)/i; +const URL_REGEXP = /((([A-Za-z]{3,9}:(?:\/\/?\/)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)(?::\d{2,5})?((?:\/[\+~%\/\.\w\-_]*)?\??(?:[\-\+=&;%@\.\w_]*)#?(?:[\.\!\/\\\w]*))?)/i; /** * Determines whether the given string looks like a URL. diff --git a/packages/url/src/test/index.test.js b/packages/url/src/test/index.test.js index f51192808ac826..c0e634a0a6d2c9 100644 --- a/packages/url/src/test/index.test.js +++ b/packages/url/src/test/index.test.js @@ -37,8 +37,10 @@ describe( 'isURL', () => { [ 'https://wordpress.org/./foo' ], [ 'https://wordpress.org/path?query#fragment' ], [ 'https://localhost/foo#bar' ], + [ 'https:///localhost/foo#bar' ], [ 'mailto:example@example.com' ], [ 'ssh://user:password@127.0.0.1:8080' ], + [ 'file:///localfolder/file.mov' ], ] )( 'valid (true): %s', ( url ) => { expect( isURL( url ) ).toBe( true ); } ); From 024149c0f3c751f36e8bdfcf67f0866fd2f5244f Mon Sep 17 00:00:00 2001 From: Sergio Estevao Date: Thu, 27 Feb 2020 16:17:49 +0000 Subject: [PATCH 2/4] Change regex to support url with the format file:/ --- packages/url/src/is-url.native.js | 2 +- packages/url/src/test/index.test.js | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/url/src/is-url.native.js b/packages/url/src/is-url.native.js index cbac7a05e2b69f..d622b38bdae166 100644 --- a/packages/url/src/is-url.native.js +++ b/packages/url/src/is-url.native.js @@ -1,4 +1,4 @@ -const URL_REGEXP = /((([A-Za-z]{3,9}:(?:\/\/?\/)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)(?::\d{2,5})?((?:\/[\+~%\/\.\w\-_]*)?\??(?:[\-\+=&;%@\.\w_]*)#?(?:[\.\!\/\\\w]*))?)/i; +const URL_REGEXP = /((([A-Za-z]{3,9}:(?:\/[\/]*)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)(?::\d{2,5})?((?:\/[\+~%\/\.\w\-_]*)?\??(?:[\-\+=&;%@\.\w_]*)#?(?:[\.\!\/\\\w]*))?)/i; /** * Determines whether the given string looks like a URL. diff --git a/packages/url/src/test/index.test.js b/packages/url/src/test/index.test.js index c0e634a0a6d2c9..d14572627de149 100644 --- a/packages/url/src/test/index.test.js +++ b/packages/url/src/test/index.test.js @@ -41,6 +41,7 @@ describe( 'isURL', () => { [ 'mailto:example@example.com' ], [ 'ssh://user:password@127.0.0.1:8080' ], [ 'file:///localfolder/file.mov' ], + [ 'file:/localfolder/file.mov' ], ] )( 'valid (true): %s', ( url ) => { expect( isURL( url ) ).toBe( true ); } ); From 7ddfe49c19cfd1345502f32904e048e96c431ed3 Mon Sep 17 00:00:00 2001 From: Sergio Estevao Date: Fri, 28 Feb 2020 14:28:53 +0000 Subject: [PATCH 3/4] Check for start and end of line. --- packages/url/src/is-url.native.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/url/src/is-url.native.js b/packages/url/src/is-url.native.js index d622b38bdae166..ef0d52a593954c 100644 --- a/packages/url/src/is-url.native.js +++ b/packages/url/src/is-url.native.js @@ -1,5 +1,4 @@ -const URL_REGEXP = /((([A-Za-z]{3,9}:(?:\/[\/]*)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)(?::\d{2,5})?((?:\/[\+~%\/\.\w\-_]*)?\??(?:[\-\+=&;%@\.\w_]*)#?(?:[\.\!\/\\\w]*))?)/i; - +const URL_REGEXP = /^((([A-Za-z]{3,9}:(?:\/[\/]*)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)(?::\d{2,5})?((?:\/[\+~%\/\.\w\-_]*)?\??(?:[\-\+=&;%@\.\w_]*)#?(?:[\.\!\/\\\w]*))?)$/i; /** * Determines whether the given string looks like a URL. * From f9be985253a7c24dc7184a3227c07768213d2111 Mon Sep 17 00:00:00 2001 From: Sergio Estevao Date: Fri, 28 Feb 2020 14:37:04 +0000 Subject: [PATCH 4/4] Simplify test now that we test for start and end of line in regex. --- packages/url/src/is-url.native.js | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/packages/url/src/is-url.native.js b/packages/url/src/is-url.native.js index ef0d52a593954c..71222cf7e74f36 100644 --- a/packages/url/src/is-url.native.js +++ b/packages/url/src/is-url.native.js @@ -12,10 +12,5 @@ const URL_REGEXP = /^((([A-Za-z]{3,9}:(?:\/[\/]*)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z * @return {boolean} Whether or not it looks like a URL. */ export function isURL( url ) { - const match = url.match( URL_REGEXP ); - // The current REGEX pattern will match strings where a valid url is part of it, - // so things like 'this is https://wordpress.com' will return true using `URL_REGEXP.test( url );`. - // This check will ensure that the matched url (the url found) is the same as the original string, - // so the given example will return false. - return match !== null && match.length >= 1 && match[ 0 ] === url; + return URL_REGEXP.test( url ); }