diff --git a/app/lib/features/share_intent/domain/entities/shared_content.dart b/app/lib/features/share_intent/domain/entities/shared_content.dart index 84886ad4..b94905aa 100644 --- a/app/lib/features/share_intent/domain/entities/shared_content.dart +++ b/app/lib/features/share_intent/domain/entities/shared_content.dart @@ -4,7 +4,8 @@ sealed class SharedContent { const SharedContent(); factory SharedContent.parse(String content) { - if (uri_parser.tryParseUrl(content) case final Uri uri) { + if (uri_parser.tryParseUrl(content, eagerParsing: true) + case final Uri uri) { return SharedUrl(uri); } else { return SharedText(content); diff --git a/app/lib/features/web_view/presentation/widgets/web_page_dialog.dart b/app/lib/features/web_view/presentation/widgets/web_page_dialog.dart index 76d97834..0cee9867 100644 --- a/app/lib/features/web_view/presentation/widgets/web_page_dialog.dart +++ b/app/lib/features/web_view/presentation/widgets/web_page_dialog.dart @@ -20,6 +20,7 @@ import 'package:flutter_inappwebview/flutter_inappwebview.dart'; import 'package:flutter_material_design_icons/flutter_material_design_icons.dart'; import 'package:hooks_riverpod/hooks_riverpod.dart'; import 'package:share_plus/share_plus.dart'; +import 'package:bang_navigator/utils/uri_parser.dart' as uri_parser; class LoadingWebPageDialog extends HookConsumerWidget { final Uri url; @@ -125,9 +126,10 @@ class WebPageDialog extends HookConsumerWidget { await webViewController!.loadUrl( urlRequest: URLRequest( url: WebUri.uri( - Uri.parse( + uri_parser.tryParseUrl( urlTextController.text, - ), + eagerParsing: true, + )!, ), ), ); @@ -140,12 +142,9 @@ class WebPageDialog extends HookConsumerWidget { : null, ), validator: (value) { - if (value != null) { - if (Uri.tryParse(value) case final Uri url) { - if (url.hasScheme && url.hasAuthority) { - return null; - } - } + if (uri_parser.tryParseUrl(value, eagerParsing: true) != + null) { + return null; } return 'Invalid URL'; diff --git a/app/lib/utils/uri_parser.dart b/app/lib/utils/uri_parser.dart index 9be02828..e710b5d2 100644 --- a/app/lib/utils/uri_parser.dart +++ b/app/lib/utils/uri_parser.dart @@ -1,12 +1,22 @@ -Uri? tryParseUrl(String? input) { +Uri? tryParseUrl(String? input, {bool eagerParsing = false}) { if (input != null) { - final uri = Uri.tryParse(input); - if (uri != null && - uri.hasAuthority && - (uri.isScheme('http') || uri.isScheme('https') || !uri.hasScheme)) { - return uri; + 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 != null && + uri.authority.isNotEmpty && + (uri.isScheme('http') || uri.isScheme('https') || !uri.hasScheme)) { + return uri; + } } } - return null; }