From 330fda5f236334d3d3f8bddf27dbb332ece4999c Mon Sep 17 00:00:00 2001 From: eToledo Date: Tue, 11 Feb 2020 14:12:57 +0100 Subject: [PATCH 1/6] Fix mobile version of is-url. Changes on this PR: https://github.com/WordPress/gutenberg/pull/19871 broke the native `is-url` version since it won't throw if the given string is not a url. --- packages/url/src/is-url.native.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 packages/url/src/is-url.native.js diff --git a/packages/url/src/is-url.native.js b/packages/url/src/is-url.native.js new file mode 100644 index 00000000000000..0a7b83ea4dbe7c --- /dev/null +++ b/packages/url/src/is-url.native.js @@ -0,0 +1,17 @@ +const URL_REGEXP = /^(?:(?:(?:https?|ftp):)?\/\/)(?:(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:[/?#]\S*)?$/i; + +/** + * Determines whether the given string looks like a URL. + * + * @param {string} url The string to scrutinise. + * + * @example + * ```js + * const isURL = isURL( 'https://wordpress.org' ); // true + * ``` + * + * @return {boolean} Whether or not it looks like a URL. + */ +export function isURL( url ) { + return URL_REGEXP.test( url ); +} From ae81762608e15c9d7860e7486feb22dfbfee834f Mon Sep 17 00:00:00 2001 From: eToledo Date: Tue, 11 Feb 2020 15:08:23 +0100 Subject: [PATCH 2/6] Mobile unit tests to is-url + better REGEXP --- .../src/link/test/index.native.js | 0 packages/url/src/is-url.native.js | 5 ++- packages/url/src/test/index.native.js | 40 +++++++++++++++++++ 3 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 packages/format-library/src/link/test/index.native.js create mode 100644 packages/url/src/test/index.native.js diff --git a/packages/format-library/src/link/test/index.native.js b/packages/format-library/src/link/test/index.native.js new file mode 100644 index 00000000000000..e69de29bb2d1d6 diff --git a/packages/url/src/is-url.native.js b/packages/url/src/is-url.native.js index 0a7b83ea4dbe7c..14be1dc34e0687 100644 --- a/packages/url/src/is-url.native.js +++ b/packages/url/src/is-url.native.js @@ -1,4 +1,4 @@ -const URL_REGEXP = /^(?:(?:(?:https?|ftp):)?\/\/)(?:(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:[/?#]\S*)?$/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. @@ -13,5 +13,6 @@ const URL_REGEXP = /^(?:(?:(?:https?|ftp):)?\/\/)(?:(?:[1-9]\d?|1\d\d|2[01]\d|22 * @return {boolean} Whether or not it looks like a URL. */ export function isURL( url ) { - return URL_REGEXP.test( url ); + const match = url.match(URL_REGEXP);// URL_REGEXP.match + return match !== null && match.length >= 1 && match[0] === url; } diff --git a/packages/url/src/test/index.native.js b/packages/url/src/test/index.native.js new file mode 100644 index 00000000000000..025f2309ad47d6 --- /dev/null +++ b/packages/url/src/test/index.native.js @@ -0,0 +1,40 @@ +/** + * External dependencies + */ +import { every } from 'lodash'; + +/** + * Internal dependencies + */ +import { + isURL, +} from '../'; + +describe( 'isURL', () => { + it.each( [ + [ 'http://wordpress.org' ], + [ 'https://wordpress.org' ], + [ 'HTTPS://WORDPRESS.ORG' ], + [ 'https://wordpress.org/./foo' ], + [ 'https://wordpress.org/path?query#fragment' ], + [ 'https://localhost/foo#bar' ], + [ 'mailto:example@example.com' ], + [ 'ssh://user:password@127.0.0.1:8080' ], + ] )( 'valid (true): %s', ( url ) => { + expect( isURL( url ) ).toBe( true ); + } ); + + it.each( [ + [ 'http://word press.org' ], + [ 'http://wordpress.org:port' ], + [ 'http://[wordpress.org]/' ], + [ 'HTTP: HyperText Transfer Protocol' ], + [ 'URLs begin with a http:// prefix' ], + [ 'Go here: http://wordpress.org' ], + [ 'http://' ], + [ 'hello' ], + [ '' ], + ] )( 'invalid (false): %s', ( url ) => { + expect( isURL( url ) ).toBe( false ); + } ); +} ); \ No newline at end of file From b013020bc63621f119b28a8cd514c4b21343053e Mon Sep 17 00:00:00 2001 From: eToledo Date: Tue, 11 Feb 2020 18:47:08 +0100 Subject: [PATCH 3/6] Fix lint issues --- packages/format-library/src/link/test/index.native.js | 0 packages/url/src/is-url.native.js | 6 +++--- packages/url/src/test/index.native.js | 11 ++--------- 3 files changed, 5 insertions(+), 12 deletions(-) delete mode 100644 packages/format-library/src/link/test/index.native.js diff --git a/packages/format-library/src/link/test/index.native.js b/packages/format-library/src/link/test/index.native.js deleted file mode 100644 index e69de29bb2d1d6..00000000000000 diff --git a/packages/url/src/is-url.native.js b/packages/url/src/is-url.native.js index 14be1dc34e0687..996bb3e7f9bd71 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. @@ -13,6 +13,6 @@ 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);// URL_REGEXP.match - return match !== null && match.length >= 1 && match[0] === url; + const match = url.match( URL_REGEXP ); // URL_REGEXP.match + return match !== null && match.length >= 1 && match[ 0 ] === url; } diff --git a/packages/url/src/test/index.native.js b/packages/url/src/test/index.native.js index 025f2309ad47d6..2a498a2fed0d32 100644 --- a/packages/url/src/test/index.native.js +++ b/packages/url/src/test/index.native.js @@ -1,14 +1,7 @@ -/** - * External dependencies - */ -import { every } from 'lodash'; - /** * Internal dependencies */ -import { - isURL, -} from '../'; +import { isURL } from '../'; describe( 'isURL', () => { it.each( [ @@ -37,4 +30,4 @@ describe( 'isURL', () => { ] )( 'invalid (false): %s', ( url ) => { expect( isURL( url ) ).toBe( false ); } ); -} ); \ No newline at end of file +} ); From 6512ab834dcf90947f4988c2c79f3cacb8640137 Mon Sep 17 00:00:00 2001 From: eToledo Date: Thu, 13 Feb 2020 16:22:31 +0100 Subject: [PATCH 4/6] Remove not-needed comment --- packages/url/src/is-url.native.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/url/src/is-url.native.js b/packages/url/src/is-url.native.js index 996bb3e7f9bd71..cf63f38e61ef2b 100644 --- a/packages/url/src/is-url.native.js +++ b/packages/url/src/is-url.native.js @@ -13,6 +13,6 @@ 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 ); // URL_REGEXP.match + const match = url.match( URL_REGEXP ); return match !== null && match.length >= 1 && match[ 0 ] === url; } From c16b34e959914e2bacc3e0daa1db52f0a5641104 Mon Sep 17 00:00:00 2001 From: eToledo Date: Thu, 13 Feb 2020 18:19:40 +0100 Subject: [PATCH 5/6] Adding inline comments to explain `.match()` decision over `.test()`. --- packages/url/src/is-url.native.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/url/src/is-url.native.js b/packages/url/src/is-url.native.js index cf63f38e61ef2b..012a93fb9a2fc0 100644 --- a/packages/url/src/is-url.native.js +++ b/packages/url/src/is-url.native.js @@ -14,5 +14,9 @@ const URL_REGEXP = /((([A-Za-z]{3,9}:(?:\/\/)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\ */ 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; } From fa15b578c2598f2d4b4a02821e22bf32cc3932e6 Mon Sep 17 00:00:00 2001 From: eToledo Date: Fri, 14 Feb 2020 09:58:41 +0100 Subject: [PATCH 6/6] Sharing test suit with web. --- packages/url/src/test/index.native.js | 31 +-------------------------- 1 file changed, 1 insertion(+), 30 deletions(-) diff --git a/packages/url/src/test/index.native.js b/packages/url/src/test/index.native.js index 2a498a2fed0d32..75247a7b9223cc 100644 --- a/packages/url/src/test/index.native.js +++ b/packages/url/src/test/index.native.js @@ -1,33 +1,4 @@ /** * Internal dependencies */ -import { isURL } from '../'; - -describe( 'isURL', () => { - it.each( [ - [ 'http://wordpress.org' ], - [ 'https://wordpress.org' ], - [ 'HTTPS://WORDPRESS.ORG' ], - [ 'https://wordpress.org/./foo' ], - [ 'https://wordpress.org/path?query#fragment' ], - [ 'https://localhost/foo#bar' ], - [ 'mailto:example@example.com' ], - [ 'ssh://user:password@127.0.0.1:8080' ], - ] )( 'valid (true): %s', ( url ) => { - expect( isURL( url ) ).toBe( true ); - } ); - - it.each( [ - [ 'http://word press.org' ], - [ 'http://wordpress.org:port' ], - [ 'http://[wordpress.org]/' ], - [ 'HTTP: HyperText Transfer Protocol' ], - [ 'URLs begin with a http:// prefix' ], - [ 'Go here: http://wordpress.org' ], - [ 'http://' ], - [ 'hello' ], - [ '' ], - ] )( 'invalid (false): %s', ( url ) => { - expect( isURL( url ) ).toBe( false ); - } ); -} ); +import './index.test';