Skip to content

Commit

Permalink
internal_link: Add decodeHashComponent function
Browse files Browse the repository at this point in the history
  • Loading branch information
sirpengi committed Oct 12, 2023
1 parent 9a5ec69 commit 4beb7bb
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lib/model/internal_link.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:flutter/foundation.dart';

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

/// Decode a dot-encoded string; return null if malformed.
// The Zulip webapp uses this encoding in narrow-links:
// https://github.com/zulip/zulip/blob/1577662a6/static/js/hash_util.js#L18-L25
@visibleForTesting
String? decodeHashComponent(String str) {
try {
return Uri.decodeComponent(str.replaceAll('.', '%'));
} on ArgumentError {
// as with '%1': Invalid argument(s): Truncated URI
return null;
} on FormatException {
// as with '%FF': FormatException: Invalid UTF-8 byte (at offset 0)
return null;
}
}

/// A URL to the given [Narrow], on `store`'s realm.
///
/// To include /near/{messageId} in the link, pass a non-null [nearMessageId].
Expand Down

0 comments on commit 4beb7bb

Please sign in to comment.