Skip to content

Commit

Permalink
Fixed isURL regex to take account file urls. (#20435)
Browse files Browse the repository at this point in the history
* Fixed isURL regex to take account file urls.

Added unit test to validate isURL when parsing file URLs.

* Change regex to support url with the format file:/

* Check for start and end of line.

* Simplify test now that we test for start and end of line in regex.
  • Loading branch information
SergioEstevao authored Feb 28, 2020
1 parent 5fde188 commit 251bab4
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 8 deletions.
10 changes: 2 additions & 8 deletions packages/url/src/is-url.native.js
Original file line number Diff line number Diff line change
@@ -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.
*
Expand All @@ -13,10 +12,5 @@ const URL_REGEXP = /((([A-Za-z]{3,9}:(?:\/\/)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\
* @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 );
}
3 changes: 3 additions & 0 deletions packages/url/src/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@ describe( 'isURL', () => {
[ 'https://wordpress.org/./foo' ],
[ 'https://wordpress.org/path?query#fragment' ],
[ 'https://localhost/foo#bar' ],
[ 'https:///localhost/foo#bar' ],
[ 'mailto:[email protected]' ],
[ 'ssh://user:[email protected]:8080' ],
[ 'file:///localfolder/file.mov' ],
[ 'file:/localfolder/file.mov' ],
] )( 'valid (true): %s', ( url ) => {
expect( isURL( url ) ).toBe( true );
} );
Expand Down

0 comments on commit 251bab4

Please sign in to comment.