bang and search

This commit is contained in:
Fabian Freund
2025-02-13 14:29:37 +01:00
parent fe4baa58f5
commit e09219e07c
21 changed files with 499 additions and 314 deletions
+17 -9
View File
@@ -1,20 +1,28 @@
final _domainRegex = RegExp(r'\.[a-zA-Z]{2,}$');
const _supportedSchemes = {'https', 'http', 'ftp', 'file', 'about'};
Uri? tryParseUrl(String? input, {bool eagerParsing = false}) {
if (input != null) {
var uri = Uri.tryParse(input);
if (uri != null) {
if (uri.authority.isEmpty && eagerParsing) {
//When there is no scheme, there will be no authority/host and all becomes a path
//so we make sure there are at least 2 segments where the first one looks like a domain
if (uri.pathSegments.length > 1 &&
RegExp(r'.[a-z]{2,}$').hasMatch(uri.pathSegments.first)) {
uri = Uri.tryParse('https://$input');
if (uri.pathSegments.isNotEmpty) {
if (_domainRegex.hasMatch(uri.pathSegments.first)) {
uri = Uri.tryParse('https://$input');
}
}
}
if (uri != null &&
uri.authority.isNotEmpty &&
(uri.isScheme('http') || uri.isScheme('https') || !uri.hasScheme)) {
return uri;
if (uri != null) {
if (uri.isScheme('about')) {
return uri;
}
if (uri.authority.isNotEmpty) {
if (_supportedSchemes.contains(uri.scheme) || !uri.hasScheme) {
return uri;
}
}
}
}
}