-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Try/improve URI detection in <LinkControl />
#19816
Conversation
Currently @wordpress/url doesn’t have a standard check for “anything that is a valid url”. This packages provides a well tested version
Previously the matching was very loose which lead to a lot of false positives and confusion. Now some stricter criteria are applied to check for valid URIs, internal links or (by request) anything starting with `www.`.
<LinkControl />
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like something that could be achieved using the native URL
interface. I know we can't just use it because of IE11 support, but maybe it would make sense to include the polyfill, e.g. whatwg-url
? It's a pretty useful thing to have at hand.
cc @youknowriad
Edit:
I created an example PR of how this could be achieved using the whatwg-url
package here: #19819.
Coincidentally, I have a local branch on my machine which aims to refactor the existing In how it ties to the needs of this pull request, I do think it's a case where we may not actually need a new function For example, I like this idea for a way to check if a URL is valid: function isURL( value ) {
try {
new URL( value );
return true;
} catch {
return false;
}
} This would accept many more inputs as considered valid URLs, including the one in the example given for the new isURL( 'mailto:[email protected]' );
// true The differences in considering what specifically is and isn't a "URL" vs. a "URI" are a bit confusing, so I'd welcome other thoughts, but my interpretation is that the above is an expected output, at least in normalizing toward one (where "URL" already has precedent of being used). Related:
|
I opened #19823 which includes a polyfill for the Another option is to simply cherry-pick the commit e2c0f74 from that branch. |
@aduth @WunderBart Thanks for your input and PRs. I'm going to close this PR in favour of your improved versions. I agree with @aduth that we ought to be looking at improving I think based on URL Living Standard we avoid URI in favour of URL and move towards better checking. This is critical for #19775 so I'm happy to help move this forward a-pace. |
Description
As part of #19775 it's important that URIs are matched more carefully in
LinkControl
. This PR introduces a new method into@wordpress/url
calledisURI()
(not to be confused withisURL
) which is then utilised inLinkControl
to determine whether to return results for:URI matching is a tricky business so this PR relies on an external package ported from a PEARL module which is well tested:
Note that despite being comprehensive in other areas, the
@wordpress/url
package doesn't have a general util to check for a "valid URI" so this is why we're introducing one.If accepted we will need to write unit tests around URI matching to provide our own coverage against the package.
How has this been tested?
Manual for now. Unit tests will be introduced.
Screenshots
Types of changes
New feature (non-breaking change which adds functionality)
Checklist: