Skip to content

Commit

Permalink
content: Support the new class of channel wildcard mentions
Browse files Browse the repository at this point in the history
For channel wildcard mentions, the class in the corresponding HTML used
to be the same as the user mentions (class="user-mention"), but now
there's an additional class added. Now it looks like the following:
class="user-mention channel-wildcard-mention".

Fixes: zulip#1064
  • Loading branch information
sm-sayedi authored and gnprice committed Dec 5, 2024
1 parent 7b48ee3 commit fb6291f
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
13 changes: 12 additions & 1 deletion lib/model/content.dart
Original file line number Diff line number Diff line change
Expand Up @@ -880,16 +880,27 @@ class _ZulipContentParser {
|| classes.contains('user-group-mention'));
int i = 0;

if (i >= classes.length) return null;
bool hasChannelWildcardClass = false;
if (classes[i] == 'channel-wildcard-mention') {
// Newer channel wildcard mentions have this class; older ones don't.
i++;
hasChannelWildcardClass = true;
}

if (i >= classes.length) return null;
if (classes[i] == 'silent') {
// A silent @-mention. We ignore this flag; see [UserMentionNode].
i++;
}

if (i >= classes.length) return null;
if (classes[i] == 'user-mention' || classes[i] == 'user-group-mention') {
if (classes[i] == 'user-mention'
|| (classes[i] == 'user-group-mention' && !hasChannelWildcardClass)) {
// The class we already knew we'd find before we called this function.
// We ignore the distinction between these; see [UserMentionNode].
// Also, we don't expect "user-group-mention" and "channel-wildcard-mention"
// to be in the list at the same time.
i++;
}

Expand Down
50 changes: 49 additions & 1 deletion test/model/content_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,48 @@ class ContentExample {
'<p><span class="silent user-group-mention" data-user-group-id="186">test-empty</span></p>',
const UserMentionNode(nodes: [TextNode('test-empty')]));

static final channelWildcardMentionPlain = ContentExample.inline(
'plain channel wildcard @-mention',
"@**all**",
expectedText: '@all',
'<p><span class="user-mention channel-wildcard-mention" data-user-id="*">@all</span></p>',
const UserMentionNode(nodes: [TextNode('@all')]));

static final channelWildcardMentionSilent = ContentExample.inline(
'silent channel wildcard @-mention',
"@_**everyone**",
expectedText: 'everyone',
'<p><span class="user-mention channel-wildcard-mention silent" data-user-id="*">everyone</span></p>',
const UserMentionNode(nodes: [TextNode('everyone')]));

static final channelWildcardMentionSilentClassOrderReversed = ContentExample.inline(
'silent channel wildcard @-mention, class order reversed',
"@_**channel**", // (hypothetical server variation)
expectedText: 'channel',
'<p><span class="silent user-mention channel-wildcard-mention" data-user-id="*">channel</span></p>',
const UserMentionNode(nodes: [TextNode('channel')]));

static final legacyChannelWildcardMentionPlain = ContentExample.inline(
'legacy plain channel wildcard @-mention',
"@**channel**",
expectedText: '@channel',
'<p><span class="user-mention" data-user-id="*">@channel</span></p>',
const UserMentionNode(nodes: [TextNode('@channel')]));

static final legacyChannelWildcardMentionSilent = ContentExample.inline(
'legacy silent channel wildcard @-mention',
"@_**stream**",
expectedText: 'stream',
'<p><span class="user-mention silent" data-user-id="*">stream</span></p>',
const UserMentionNode(nodes: [TextNode('stream')]));

static final legacyChannelWildcardMentionSilentClassOrderReversed = ContentExample.inline(
'legacy silent channel wildcard @-mention, class order reversed',
"@_**all**", // (hypothetical server variation)
expectedText: 'all',
'<p><span class="silent user-mention" data-user-id="*">all</span></p>',
const UserMentionNode(nodes: [TextNode('all')]));

static final emojiUnicode = ContentExample.inline(
'Unicode emoji, encoded in span element',
":thumbs_up:",
Expand Down Expand Up @@ -1213,7 +1255,13 @@ void main() {
testParseExample(ContentExample.groupMentionSilent);
testParseExample(ContentExample.groupMentionSilentClassOrderReversed);

// TODO test wildcard mentions
testParseExample(ContentExample.channelWildcardMentionPlain);
testParseExample(ContentExample.channelWildcardMentionSilent);
testParseExample(ContentExample.channelWildcardMentionSilentClassOrderReversed);

testParseExample(ContentExample.legacyChannelWildcardMentionPlain);
testParseExample(ContentExample.legacyChannelWildcardMentionSilent);
testParseExample(ContentExample.legacyChannelWildcardMentionSilentClassOrderReversed);
});

testParseExample(ContentExample.emojiUnicode);
Expand Down
6 changes: 6 additions & 0 deletions test/widgets/content_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,12 @@ void main() {
testContentSmoke(ContentExample.userMentionSilent);
testContentSmoke(ContentExample.groupMentionPlain);
testContentSmoke(ContentExample.groupMentionSilent);
testContentSmoke(ContentExample.channelWildcardMentionPlain);
testContentSmoke(ContentExample.channelWildcardMentionSilent);
testContentSmoke(ContentExample.channelWildcardMentionSilentClassOrderReversed);
testContentSmoke(ContentExample.legacyChannelWildcardMentionPlain);
testContentSmoke(ContentExample.legacyChannelWildcardMentionSilent);
testContentSmoke(ContentExample.legacyChannelWildcardMentionSilentClassOrderReversed);

UserMention? findUserMentionInSpan(InlineSpan rootSpan) {
UserMention? result;
Expand Down

0 comments on commit fb6291f

Please sign in to comment.