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

Try/improve URI detection in <LinkControl /> #19816

Closed
wants to merge 2 commits into from
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
36 changes: 14 additions & 22 deletions packages/block-editor/src/components/link-control/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ import { useCallback, useState, Fragment } from '@wordpress/element';
import {
safeDecodeURI,
filterURLForDisplay,
isURL,
prependHTTP,
getProtocol,
isValidFragment,
isUri,
} from '@wordpress/url';
import { useInstanceId } from '@wordpress/compose';
import { useSelect } from '@wordpress/data';
Expand Down Expand Up @@ -85,31 +86,22 @@ function LinkControl( {
};

const handleEntitySearch = async ( val, args ) => {
const results = await Promise.all( [
fetchSearchSuggestions( val, {
...( args.isInitialSuggestions ? { perPage: 3 } : {} ),
} ),
handleDirectEntry( val ),
] );

const couldBeURL = ! val.includes( ' ' );

// If it's potentially a URL search then concat on a URL search suggestion
// just for good measure. That way once the actual results run out we always
// have a URL option to fallback on.
return couldBeURL && ! args.isInitialSuggestions ? results[ 0 ].concat( results[ 1 ] ) : results[ 0 ];
return await fetchSearchSuggestions( val, {
...( args.isInitialSuggestions ? { perPage: 3 } : {} ),
} );
};

// Effects
const getSearchHandler = useCallback( ( val, args ) => {
const protocol = getProtocol( val ) || '';
const isMailto = protocol.includes( 'mailto' );
const isInternal = startsWith( val, '#' );
const isTel = protocol.includes( 'tel' );
const couldBeURI = function( val ) {
const isInternal = startsWith( val, '#' ) && isValidFragment( val );
const includesWWW = !! ( val && val.includes( 'www.' ) );

const handleManualEntry = isInternal || isMailto || isTel || isURL( val ) || ( val && val.includes( 'www.' ) );
return isUri( val ) || includesWWW || isInternal;
};

return ( handleManualEntry ) ? handleDirectEntry( val, args ) : handleEntitySearch( val, args );
// Effects
const getSearchHandler = useCallback( ( val, args ) => {
// If it has a fragment and it is valid
return ( couldBeURI( val ) ) ? handleDirectEntry( val, args ) : handleEntitySearch( val, args );
}, [ handleDirectEntry, fetchSearchSuggestions ] );

// Render Components
Expand Down
19 changes: 19 additions & 0 deletions packages/url/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,25 @@ _Returns_

- `boolean`: Whether or not it looks like an email.

<a name="isURI" href="#isURI">#</a> **isURI**

Determines whether the given string looks like a URI (of any type).

_Usage_

```js
const isURI = isURI( 'https://wordpress.org' ); // true
const anotherURI = isURI('mailto:[email protected]'); // true
```

_Parameters_

- _uri_ `string`: The string to scrutinise.

_Returns_

- `boolean`: Whether or not it looks like a URI.

<a name="isURL" href="#isURL">#</a> **isURL**

Determines whether the given string looks like a URL.
Expand Down
3 changes: 2 additions & 1 deletion packages/url/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"dependencies": {
"@babel/runtime": "^7.4.4",
"lodash": "^4.17.15",
"qs": "^6.5.2"
"qs": "^6.5.2",
"valid-url": "^1.0.9"
},
"publishConfig": {
"access": "public"
Expand Down
2 changes: 2 additions & 0 deletions packages/url/src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@

export { isURI } from './is-uri';
export { isURL } from './is-url';
export { isEmail } from './is-email';
export { getProtocol } from './get-protocol';
Expand Down
21 changes: 21 additions & 0 deletions packages/url/src/is-uri.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* External dependencies
*/
import { isUri } from 'valid-url';

/**
* Determines whether the given string looks like a URI (of any type).
*
* @param {string} uri The string to scrutinise.
*
* @example
* ```js
* const isURI = isURI( 'https://wordpress.org' ); // true
* const anotherURI = isURI('mailto:[email protected]'); // true
* ```
*
* @return {boolean} Whether or not it looks like a URI.
*/
export function isURI( uri ) {
return isUri( uri );
}