Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use URL interface to validate URI #19819

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/block-editor/src/components/link-control/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
prependHTTP,
getProtocol,
isValidFragment,
isUri,
isURI,
} from '@wordpress/url';
import { useInstanceId } from '@wordpress/compose';
import { useSelect } from '@wordpress/data';
Expand Down Expand Up @@ -95,7 +95,7 @@ function LinkControl( {
const isInternal = startsWith( val, '#' ) && isValidFragment( val );
const includesWWW = !! ( val && val.includes( 'www.' ) );

return isUri( val ) || includesWWW || isInternal;
return isURI( val ) || includesWWW || isInternal;
};

// Effects
Expand Down
1 change: 1 addition & 0 deletions packages/url/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ _Returns_
<a name="isURI" href="#isURI">#</a> **isURI**

Determines whether the given string looks like a URI (of any type).
See <https://url.spec.whatwg.org/#url-representation> for more info.

_Usage_

Expand Down
2 changes: 1 addition & 1 deletion packages/url/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"@babel/runtime": "^7.4.4",
"lodash": "^4.17.15",
"qs": "^6.5.2",
"valid-url": "^1.0.9"
"whatwg-url": "^8.0.0"
},
"publishConfig": {
"access": "public"
Expand Down
10 changes: 8 additions & 2 deletions packages/url/src/is-uri.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
/**
* External dependencies
*/
import { isUri } from 'valid-url';
import { URL } from 'whatwg-url';

/**
* Determines whether the given string looks like a URI (of any type).
* See https://url.spec.whatwg.org/#url-representation for more info.
*
* @param {string} uri The string to scrutinise.
*
Expand All @@ -17,5 +18,10 @@ import { isUri } from 'valid-url';
* @return {boolean} Whether or not it looks like a URI.
*/
export function isURI( uri ) {
return isUri( uri );
try {
new URL( uri );
return true;
} catch ( _ ) {
return false;
}
}