/**
* @module isExternalLink
* @description Tests if an url is pointing to the same domain as the client or not.
*
* @since 1.0.0
*
* @param {string} url The url to test.
*
* @return {boolean} Whether the passed string is an external url or not.
*
* @example
* import { isExternalLink } from "@gravityforms/utils";
*
* function Example() {
* const url = 'https://some-url.com';
* if ( isExternalLink( url ) ) {
* // do something
* }
* }
*
*/
export default function isExternalLink( url = '' ) {
const match = url.match(
/^([^:/?#]+:)?(?:\/\/([^/?#]*))?([^?#]+)?(\?[^#]*)?(#.*)?/
);
if (
typeof match[ 1 ] === 'string' &&
match[ 1 ].length > 0 &&
match[ 1 ].toLowerCase() !== window.location.protocol
) {
return true;
}
return typeof match[ 2 ] === 'string' &&
match[ 2 ].length > 0 &&
match[ 2 ].replace(
new RegExp(
`:(${
{
'http:': 80,
'https:': 443,
}[ window.location.protocol ]
})?$`
),
''
) !== window.location.host;
}