Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/stream_core_flutter/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

- Fixed component text ignoring the app's font family (`ThemeData.fontFamily`).
- Fixed `StreamSheet` painting lighter than `backgroundElevation1` in dark mode under Material 3.
- Fixed `StreamMessageText` showing nothing when a mention's id is not valid percent-encoding. The mention now
shows, and `onTapMention` receives the id as written.

### 🔄 Changed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ extension type const StreamMentionType(String _) implements String {
/// [displayText] is the raw display text from the link
/// (e.g., `'@Alice'` from `[@Alice](mention:user123)`).
/// [id] is the mention identifier (the URL-decoded portion after the
/// `mention:` scheme).
/// `mention:` scheme, or that portion as written if it isn't valid
/// percent-encoding).
///
/// Only invoked for user-type mentions. To handle the full set of mention
/// kinds (channel / here / role / group) in a single callback, use
Expand All @@ -65,7 +66,8 @@ typedef MarkdownTapMentionCallback = void Function(String displayText, String id
///
/// [displayText] is the raw display text from the link.
/// [type] is the mention kind decoded from the URL scheme.
/// [id] is the URL-decoded payload following the scheme.
/// [id] is the URL-decoded payload following the scheme, or the payload as
/// written if it isn't valid percent-encoding.
typedef MarkdownTapAnyMentionCallback =
void Function(
String displayText,
Expand Down Expand Up @@ -262,7 +264,7 @@ class StreamMessageTextProps {
/// Called when a mention of any kind is tapped.
///
/// Receives the [StreamMentionType] decoded from the URL scheme along with the
/// display text and the URL-decoded id payload. Takes precedence over
/// display text and the id payload. Takes precedence over
/// [onTapMention] when both are set.
final MarkdownTapAnyMentionCallback? onTapAnyMention;

Expand Down Expand Up @@ -520,11 +522,24 @@ class _StreamMentionSyntax extends md.InlineSyntax {
final rawId = match.group(3)!;

final el = md.Element.text('mention', displayText);
el.attributes['id'] = Uri.decodeComponent(rawId);
el.attributes['id'] = _decodeId(rawId);
el.attributes['type'] = type;
parser.addNode(el);
return true;
}

static final _malformedEscape = RegExp('%(?![0-9A-Fa-f]{2})');

// The percent-decoded [rawId], or [rawId] itself if it isn't valid
// percent-encoded UTF-8.
static String _decodeId(String rawId) {
if (rawId.contains(_malformedEscape)) return rawId;
try {
return Uri.decodeComponent(rawId);
} on FormatException {
return rawId;
}
}
}

// Links the web addresses found by [findLinkAt], with or without a scheme,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:stream_core_flutter/chat.dart';

// Shows [text] and taps [displayText], returning the id the mention reported.
Future<String?> _tapMention(WidgetTester tester, String text, String displayText) async {
String? tappedId;
await tester.pumpWidget(
MaterialApp(
home: Theme(
data: ThemeData(extensions: [StreamTheme()]),
child: StreamMessageLayout(
data: const StreamMessageLayoutData(),
child: StreamMessageText(text, onTapMention: (_, id) => tappedId = id),
),
),
),
);

await tester.tapOnText(find.textRange.ofSubstring(displayText));
return tappedId;
}

void main() {
testWidgets('StreamMessageText reports the decoded id of a mention', (tester) async {
expect(await _tapMention(tester, 'Hi [@Alice](mention:user%20123)', '@Alice'), 'user 123');
});

for (final rawId in ['%ZZ', '%', 'user%F', '%FF', '%C3', 'a%20%ZZ']) {
testWidgets('StreamMessageText shows a mention whose id is not valid percent-encoding: $rawId', (tester) async {
expect(await _tapMention(tester, 'Hi [@Alice](mention:$rawId)', '@Alice'), rawId);
expect(tester.takeException(), isNull);
});
}

testWidgets('StreamMessageText shows a role mention whose id is not valid percent-encoding', (tester) async {
(StreamMentionType, String)? tapped;
await tester.pumpWidget(
MaterialApp(
home: Theme(
data: ThemeData(extensions: [StreamTheme()]),
child: StreamMessageLayout(
data: const StreamMessageLayoutData(),
child: StreamMessageText(
'Hi [@admins](mention-role:%FF)',
onTapAnyMention: (_, type, id) => tapped = (type, id),
),
),
),
),
);

await tester.tapOnText(find.textRange.ofSubstring('@admins'));

expect(tapped, (StreamMentionType.role, '%FF'));
expect(tester.takeException(), isNull);
});
}
Loading