diff --git a/mobile/lib/features/channels/message_content/token_pill.dart b/mobile/lib/features/channels/message_content/token_pill.dart index 3f1705f1fc..8a3b5accd2 100644 --- a/mobile/lib/features/channels/message_content/token_pill.dart +++ b/mobile/lib/features/channels/message_content/token_pill.dart @@ -118,6 +118,11 @@ RegExp _buildPrefixPattern({ final escapedPrefix = RegExp.escape(prefix); const leadingBoundary = r'(? mentionOccurrences( String text, Iterable labels, @@ -14,11 +21,12 @@ List<({int start, int end, String label})> mentionOccurrences( // candidate binds it. Never fall back to a shorter, different recipient. final suffix = RegExp(r' \([0-9a-f]{64}\)$', caseSensitive: false).hasMatch(label) - ? r'(?! (?:[2-9]|[1-9][0-9]+)(?=[\s,;.!?:)\]}*_]|$))' + ? '(?! (?:[2-9]|[1-9][0-9]+)(?!$_nameCharacter))' : ''; final pattern = RegExp( - '(?:^|[\\s(\\[{]|[*_]{1,3}|\\|\\|)(@${RegExp.escape(label)})(?! \\([0-9a-f]{64}\\))$suffix(?=\\|\\||[\\s,;.!?:)\\]}*_]|\$)', + '(?:^|[\\s(\\[{]|[*_]{1,3}|\\|\\|)(@${RegExp.escape(label)})(?! \\([0-9a-f]{64}\\))$suffix(?!$_nameCharacter)', caseSensitive: false, + unicode: true, ); for (final match in pattern.allMatches(text)) { final start = match.end - match.group(1)!.length; diff --git a/mobile/test/features/channels/message_content/preview_text_test.dart b/mobile/test/features/channels/message_content/preview_text_test.dart index 79922f677a..93e3428c3a 100644 --- a/mobile/test/features/channels/message_content/preview_text_test.dart +++ b/mobile/test/features/channels/message_content/preview_text_test.dart @@ -140,6 +140,26 @@ void main() { ); }); + test('a bound mention keeps its chip before a suffix', () { + expect( + _pieces( + "@alice's idea", + mentionBindings: { + 'alice': {_alice}, + }, + mentionNames: {_alice: 'Alice'}, + ), + [ + const PreviewMention( + pubkey: _alice, + label: 'Alice', + semanticsLabel: 'Alice', + ), + const PreviewText("'s idea"), + ], + ); + }); + test('an ambiguous mention stays text', () { expect( _pieces( diff --git a/mobile/test/features/channels/message_content_exact_mentions_test.dart b/mobile/test/features/channels/message_content_exact_mentions_test.dart index 19421fbd6e..a51a033b76 100644 --- a/mobile/test/features/channels/message_content_exact_mentions_test.dart +++ b/mobile/test/features/channels/message_content_exact_mentions_test.dart @@ -31,6 +31,62 @@ void main() { expect(tapped, 'c' * 64); }); + testWidgets('a tagged mention keeps its pill before a suffix', ( + tester, + ) async { + final bob = 'd' * 64; + // The composer keeps Bob's recipient when punctuation, an apostrophe, a + // hyphen or a slash follows the chip; the bubble draws the same pill with + // the suffix as plain text after it. A letter continues the name. + for (final (content, suffix) in [ + ("@Bob's idea", "'s idea"), + ('@Bob’s idea', '’s idea'), + ('@Bob-bot', '-bot'), + ('@Bob/Alice', '/Alice'), + ('@Bob.', '.'), + ]) { + String? tapped; + await tester.pumpWidget( + WidgetHelpers.testable( + child: MessageContent( + content: content, + mentionNames: {bob: 'Bob'}, + tags: [ + ['p', bob], + ], + onMentionTap: (key) => tapped = key, + ), + ), + ); + await tester.pumpAndSettle(); + expect(find.text('Bob'), findsOneWidget, reason: content); + expect( + find.textContaining(suffix, findRichText: true), + findsOneWidget, + reason: content, + ); + await tester.tap(find.text('Bob')); + expect(tapped, bob, reason: content); + } + String? tapped; + await tester.pumpWidget( + WidgetHelpers.testable( + child: MessageContent( + content: '@Bobby', + mentionNames: {bob: 'Bob'}, + tags: [ + ['p', bob], + ], + onMentionTap: (key) => tapped = key, + ), + ), + ); + await tester.pumpAndSettle(); + expect(find.text('Bob'), findsNothing); + await tester.tap(find.text('Bobby')); + expect(tapped, isNull); + }); + for (final tagged in [false, true]) { testWidgets( 'qualified chips preserve authority and narrow layout: $tagged', diff --git a/mobile/test/shared/mentions/mention_bindings_test.dart b/mobile/test/shared/mentions/mention_bindings_test.dart index f9dd6500ad..ddb4386523 100644 --- a/mobile/test/shared/mentions/mention_bindings_test.dart +++ b/mobile/test/shared/mentions/mention_bindings_test.dart @@ -30,6 +30,31 @@ void main() { isEmpty, ); }); + test('a label ends at the composer suffix boundary', () { + // Punctuation, either apostrophe, a hyphen or a slash follow the name as + // text; a letter, mark, digit or underscore continues it (`@Bobby`). + for (final suffix in ["'s idea", '’s idea', '-bot', '/Alice', '.', '_']) { + final found = mentionOccurrences('@Scout$suffix', ['Scout']); + if (suffix == '_') { + expect(found, isEmpty, reason: 'after $suffix'); + } else { + expect(found.single.end, 6, reason: 'after $suffix'); + } + } + for (final text in ['@Scouty', '@Scout1', '@Scoutē']) { + expect(mentionOccurrences(text, ['Scout']), isEmpty, reason: text); + } + // A reservation suffix ends at the same boundary, so the shorter label + // still never claims `@Scout () 2's`. + expect( + mentionOccurrences("@Scout ($second) 2's", ['Scout ($second)']), + isEmpty, + ); + expect( + mentionOccurrences('@Scout ($second) 2x', ['Scout ($second)']).single.end, + '@Scout ($second)'.length, + ); + }); // Historical plain denial and both tag orders run through channel/thread UI. test('ordinary ambiguity and qualification require signed authority', () { expect(