import type { ProtocolConfig } from '@admin/utils/editor/protocol-config' const ATTR_WHITESPACE_PATTERN = '[\\u0000-\\u0020\\u00A0\\u1680\\u180E\\u2000-\\u2029\\u205F\\u3000]' const ATTR_WHITESPACE = new RegExp(ATTR_WHITESPACE_PATTERN, 'g') export function isAllowedUri(uri: string | undefined, protocols?: ProtocolConfig) { const allowedProtocols: string[] = [ 'http', 'https', 'ftp', 'ftps', 'mailto', 'tel', 'callto', 'sms', 'cid', 'xmpp' ] if (protocols) { protocols.forEach((protocol) => { const nextProtocol = typeof protocol === 'string' ? protocol : protocol.scheme if (nextProtocol) { allowedProtocols.push(nextProtocol) } }) } return ( !uri || uri .replace(ATTR_WHITESPACE, '') .match( new RegExp( `^(?:(?:${allowedProtocols.join('|')}):|[^a-z]|[a-z0-9+.-]+(?:[^a-z+.-:]|$))`, 'i' ) ) ) }