improve uri parsing for urls without scheme
This commit is contained in:
@@ -4,7 +4,8 @@ sealed class SharedContent {
|
|||||||
const SharedContent();
|
const SharedContent();
|
||||||
|
|
||||||
factory SharedContent.parse(String content) {
|
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);
|
return SharedUrl(uri);
|
||||||
} else {
|
} else {
|
||||||
return SharedText(content);
|
return SharedText(content);
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ import 'package:flutter_inappwebview/flutter_inappwebview.dart';
|
|||||||
import 'package:flutter_material_design_icons/flutter_material_design_icons.dart';
|
import 'package:flutter_material_design_icons/flutter_material_design_icons.dart';
|
||||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||||
import 'package:share_plus/share_plus.dart';
|
import 'package:share_plus/share_plus.dart';
|
||||||
|
import 'package:bang_navigator/utils/uri_parser.dart' as uri_parser;
|
||||||
|
|
||||||
class LoadingWebPageDialog extends HookConsumerWidget {
|
class LoadingWebPageDialog extends HookConsumerWidget {
|
||||||
final Uri url;
|
final Uri url;
|
||||||
@@ -125,9 +126,10 @@ class WebPageDialog extends HookConsumerWidget {
|
|||||||
await webViewController!.loadUrl(
|
await webViewController!.loadUrl(
|
||||||
urlRequest: URLRequest(
|
urlRequest: URLRequest(
|
||||||
url: WebUri.uri(
|
url: WebUri.uri(
|
||||||
Uri.parse(
|
uri_parser.tryParseUrl(
|
||||||
urlTextController.text,
|
urlTextController.text,
|
||||||
),
|
eagerParsing: true,
|
||||||
|
)!,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
@@ -140,12 +142,9 @@ class WebPageDialog extends HookConsumerWidget {
|
|||||||
: null,
|
: null,
|
||||||
),
|
),
|
||||||
validator: (value) {
|
validator: (value) {
|
||||||
if (value != null) {
|
if (uri_parser.tryParseUrl(value, eagerParsing: true) !=
|
||||||
if (Uri.tryParse(value) case final Uri url) {
|
null) {
|
||||||
if (url.hasScheme && url.hasAuthority) {
|
return null;
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 'Invalid URL';
|
return 'Invalid URL';
|
||||||
|
|||||||
@@ -1,12 +1,22 @@
|
|||||||
Uri? tryParseUrl(String? input) {
|
Uri? tryParseUrl(String? input, {bool eagerParsing = false}) {
|
||||||
if (input != null) {
|
if (input != null) {
|
||||||
final uri = Uri.tryParse(input);
|
var uri = Uri.tryParse(input);
|
||||||
if (uri != null &&
|
if (uri != null) {
|
||||||
uri.hasAuthority &&
|
if (uri.authority.isEmpty && eagerParsing) {
|
||||||
(uri.isScheme('http') || uri.isScheme('https') || !uri.hasScheme)) {
|
//When there is no scheme, there will be no authority/host and all becomes a path
|
||||||
return uri;
|
//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;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user