fix(mobile): end a rendered mention at the composer's suffix boundary

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 (<key>) 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 (<key>)` 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 <noreply@anthropic.com>
Signed-off-by: Matt Toohey <contact@matttoohey.com>
This commit is contained in:
Matt Toohey
2026-11-19 13:32:27 +11:00
parent 1996069693
commit a1e4d69fab
5 changed files with 120 additions and 4 deletions

View File

@@ -118,6 +118,11 @@ RegExp _buildPrefixPattern({
final escapedPrefix = RegExp.escape(prefix); final escapedPrefix = RegExp.escape(prefix);
const leadingBoundary = r'(?<![\w./:-])'; const leadingBoundary = r'(?<![\w./:-])';
// A known name ends where `mentionOccurrences` ends it: a letter, mark,
// digit or underscore continues it, anything else follows it as text, so
// `@Bob's`, `@Bob-bot` and `@Bob/Alice` draw Bob's pill. An unknown token
// still ends only at whitespace or sentence punctuation.
const knownTrailingBoundary = r'(?![\p{L}\p{M}\p{N}_])';
const trailingBoundary = r'(?=$|[\s,;.!?:)\]}])'; const trailingBoundary = r'(?=$|[\s,;.!?:)\]}])';
if (names.isEmpty) { if (names.isEmpty) {
@@ -125,13 +130,15 @@ RegExp _buildPrefixPattern({
'$leadingBoundary$escapedPrefix(?:$genericTokenPattern)$trailingBoundary', '$leadingBoundary$escapedPrefix(?:$genericTokenPattern)$trailingBoundary',
caseSensitive: false, caseSensitive: false,
multiLine: true, multiLine: true,
unicode: true,
); );
} }
final knownAlternatives = names.map(RegExp.escape).join('|'); final knownAlternatives = names.map(RegExp.escape).join('|');
return RegExp( return RegExp(
'$leadingBoundary$escapedPrefix(?:(?:$knownAlternatives)$trailingBoundary|(?:$genericTokenPattern)$trailingBoundary)', '$leadingBoundary$escapedPrefix(?:(?:$knownAlternatives)$knownTrailingBoundary|(?:$genericTokenPattern)$trailingBoundary)',
caseSensitive: false, caseSensitive: false,
multiLine: true, multiLine: true,
unicode: true,
); );
} }

View File

@@ -1,8 +1,15 @@
/// A letter, mark, digit or underscore directly after a label continues the
/// name, so `@Bobby` holds no `Bob`. Anything else ends it: punctuation, a
/// straight or curly apostrophe, a hyphen or a slash (`@Bob's`, `@Bob-bot`,
/// `@Bob/Alice`). Same rule as the composer's `mentionEndsAtBoundary`.
const _nameCharacter = r'[\p{L}\p{M}\p{N}_]';
/// Longest literal ranges win, including labels containing another @ sign. /// Longest literal ranges win, including labels containing another @ sign.
/// Recognition precedes eligibility: ambiguous labels still block shorter ones. /// Recognition precedes eligibility: ambiguous labels still block shorter ones.
/// A label opens where a query does (portable mention rules, section 2): at /// A label opens where a query does (portable mention rules, section 2): at
/// the start, after whitespace, `(`, `[` or `{`, or after a Markdown /// the start, after whitespace, `(`, `[` or `{`, or after a Markdown
/// delimiter, so a mention picked after a bracket keeps its recipient. /// delimiter, so a mention picked after a bracket keeps its recipient. It
/// ends at the composer's suffix boundary, [_nameCharacter].
List<({int start, int end, String label})> mentionOccurrences( List<({int start, int end, String label})> mentionOccurrences(
String text, String text,
Iterable<String> labels, Iterable<String> labels,
@@ -14,11 +21,12 @@ List<({int start, int end, String label})> mentionOccurrences(
// candidate binds it. Never fall back to a shorter, different recipient. // candidate binds it. Never fall back to a shorter, different recipient.
final suffix = final suffix =
RegExp(r' \([0-9a-f]{64}\)$', caseSensitive: false).hasMatch(label) 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( 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, caseSensitive: false,
unicode: true,
); );
for (final match in pattern.allMatches(text)) { for (final match in pattern.allMatches(text)) {
final start = match.end - match.group(1)!.length; final start = match.end - match.group(1)!.length;

View File

@@ -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', () { test('an ambiguous mention stays text', () {
expect( expect(
_pieces( _pieces(

View File

@@ -31,6 +31,62 @@ void main() {
expect(tapped, 'c' * 64); 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]) { for (final tagged in [false, true]) {
testWidgets( testWidgets(
'qualified chips preserve authority and narrow layout: $tagged', 'qualified chips preserve authority and narrow layout: $tagged',

View File

@@ -30,6 +30,31 @@ void main() {
isEmpty, 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 (<key>) 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. // Historical plain denial and both tag orders run through channel/thread UI.
test('ordinary ambiguity and qualification require signed authority', () { test('ordinary ambiguity and qualification require signed authority', () {
expect( expect(