From 889b940570cdfaa47dec85c2e034c26f4651bb7c Mon Sep 17 00:00:00 2001 From: Shu Chen Date: Tue, 3 Oct 2023 16:53:10 +0100 Subject: [PATCH] internal_link: Add tryParseToRealmUrl --- lib/model/internal_link.dart | 11 +++++++++++ lib/widgets/content.dart | 8 +++----- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/lib/model/internal_link.dart b/lib/model/internal_link.dart index 1f0845618f2..998c7cd1b12 100644 --- a/lib/model/internal_link.dart +++ b/lib/model/internal_link.dart @@ -78,3 +78,14 @@ Uri narrowLink(PerAccountStore store, Narrow narrow, {int? nearMessageId}) { return store.account.realmUrl.replace(fragment: fragment.toString()); } + +/// Create a new `Uri` object in relation to a given realmUrl. +/// +/// Returns `null` if `urlString` could not be parsed as a `Uri`. +Uri? tryResolveOnRealmUrl(String urlString, Uri realmUrl) { + try { + return realmUrl.resolve(urlString); + } on FormatException { + return null; + } +} diff --git a/lib/widgets/content.dart b/lib/widgets/content.dart index 92bc193cb10..9dd59afafca 100644 --- a/lib/widgets/content.dart +++ b/lib/widgets/content.dart @@ -7,6 +7,7 @@ import '../api/core.dart'; import '../api/model/model.dart'; import '../model/binding.dart'; import '../model/content.dart'; +import '../model/internal_link.dart'; import '../model/store.dart'; import 'code_block.dart'; import 'dialog.dart'; @@ -661,12 +662,9 @@ void _launchUrl(BuildContext context, String urlString) async { } final store = PerAccountStoreWidget.of(context); - final Uri url; - try { - url = store.account.realmUrl.resolve(urlString); - } on FormatException { // TODO(log) + final url = tryResolveOnRealmUrl(urlString, store.account.realmUrl); + if (url == null) { // TODO(log) await showError(context, null); - if (!context.mounted) return; // TODO(dart): redundant for sake of lint return; }