openableLink static method

String? openableLink(
  1. String raw
)

The validated URL for raw, or null. Strips C0 controls, DEL and C1 (0x00-0x1F, 0x7F, 0x80-0x9F) first, so the check and the open decide on the same canonical string. Never throws.

Implementation

static String? openableLink(String raw) {
  try {
    final canonical = raw
        .split('')
        .where((c) {
          final code = c.codeUnitAt(0);
          if (code < 0x20) return false;
          if (code == 0x7F) return false;
          if (code >= 0x80 && code <= 0x9F) return false;
          return true;
        })
        .join()
        .trim();
    if (canonical.isEmpty) return null;
    final uri = Uri.tryParse(canonical);
    if (uri == null) return null;
    final scheme = uri.scheme.toLowerCase();
    if (scheme.isEmpty) return null;
    switch (scheme) {
      case 'http':
      case 'https':
        if (uri.host.isEmpty) return null;
        return canonical;
      default:
        if (blockedLinkSchemes.contains(scheme)) return null;
        return canonical;
    }
  } catch (_) {
    return null;
  }
}