From a1e4d69fab26bbbb4264829a0aa78a581b24904b Mon Sep 17 00:00:00 2001 From: Matt Toohey Date: Thu, 19 Nov 2026 13:32:27 +1100 Subject: [PATCH] fix(mobile): end a rendered mention at the composer's suffix boundary MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A sent `@Bob's idea` or `@Bob-bot` carries Bob's p tag since picked chips keep their recipient through a following apostrophe, hyphen, slash or punctuation, but the bubble still drew it as plain text: the renderer's trailing boundary was a fixed punctuation set that predates that rule. The message scan and the bubble pattern now end a known name exactly where the composer's `mentionEndsAtBoundary` does, as buzz-app's `mentionBoundary` does (recommendation 3): only a letter, mark, digit or underscore directly after the name continues it, so `@Bobby` holds no Bob, while `@Bob's idea`, `@Bob’s idea`, `@Bob-bot`, `@Bob/Alice` and `@Bob.` render Bob's pill with the suffix as text. `mentionOccurrences` carries the rule as one Unicode class and applies it to the reservation suffix too, so the shorter label still never claims `@Scout () 2's`. Every reader of the scan follows: the bubble's non-breaking-space pass, inline previews, the edit sheet's seeded chips, draft restoration and the typed exact-name fallback. The bubble's inline pattern uses the same class for known names and keeps the old whitespace-or-punctuation end for unknown tokens, so `@Bob's` with no bound Bob stays text as before. One consequence of the shared rule: a closing `_` directly after a name is a name character, so `_@Bob_` no longer draws a pill, as in buzz-app. Namesake `@Sam ()` rendering and the in-code exclusion are unchanged. Tests: the exact-mentions widget test renders each suffix case with Bob tagged, taps the pill and sees the suffix as text, and sees no Bob in `@Bobby`; the scan test covers the suffixes, letter, digit, mark and underscore continuations and the reservation suffix; the preview test keeps Alice's chip before `'s idea`. All three fail with the old boundary sets. `dart format`, `flutter analyze`, `just file-size-check` and the full mobile `flutter test` suite pass; one run hit an unrelated activity-provider timing test that passes solo. Co-Authored-By: Claude Fable 5.1 Signed-off-by: Matt Toohey --- .../channels/message_content/token_pill.dart | 9 ++- .../lib/shared/mentions/mention_bindings.dart | 14 ++++- .../message_content/preview_text_test.dart | 20 +++++++ .../message_content_exact_mentions_test.dart | 56 +++++++++++++++++++ .../mentions/mention_bindings_test.dart | 25 +++++++++ 5 files changed, 120 insertions(+), 4 deletions(-) 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(