Skip to content

Commit 4beb7bb

Browse files
committed
internal_link: Add decodeHashComponent function
1 parent 9a5ec69 commit 4beb7bb

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

lib/model/internal_link.dart

+17
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'package:flutter/foundation.dart';
12

23
import '../api/model/narrow.dart';
34
import 'narrow.dart';
@@ -19,6 +20,22 @@ String _encodeHashComponent(String str) {
1920
.replaceAllMapped(_encodeHashComponentRegex, (Match m) => _hashReplacements[m[0]!]!);
2021
}
2122

23+
/// Decode a dot-encoded string; return null if malformed.
24+
// The Zulip webapp uses this encoding in narrow-links:
25+
// https://github.com/zulip/zulip/blob/1577662a6/static/js/hash_util.js#L18-L25
26+
@visibleForTesting
27+
String? decodeHashComponent(String str) {
28+
try {
29+
return Uri.decodeComponent(str.replaceAll('.', '%'));
30+
} on ArgumentError {
31+
// as with '%1': Invalid argument(s): Truncated URI
32+
return null;
33+
} on FormatException {
34+
// as with '%FF': FormatException: Invalid UTF-8 byte (at offset 0)
35+
return null;
36+
}
37+
}
38+
2239
/// A URL to the given [Narrow], on `store`'s realm.
2340
///
2441
/// To include /near/{messageId} in the link, pass a non-null [nearMessageId].

0 commit comments

Comments
 (0)