Skip to content

Commit

Permalink
content: Use className regexp for Unicode emoji, instead of class set
Browse files Browse the repository at this point in the history
And add another test for more complete coverage.

This removes our last use of the `classes` class set,
so it completes zulip#497.

Fixes: zulip#497
  • Loading branch information
gnprice authored and chrisbobbe committed Feb 6, 2024
1 parent efb25c7 commit 6956d5d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
18 changes: 8 additions & 10 deletions lib/model/content.dart
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,11 @@ class _ZulipContentParser {
return RegExp("^(?:$mentionClass(?: silent)?|silent $mentionClass)\$");
}();

static final _emojiClassRegexp = RegExp(r"^emoji(?:-[0-9a-f]+)*$");
static final _emojiClassNameRegexp = () {
const specificEmoji = r"emoji(?:-[0-9a-f]+)+";
return RegExp("^(?:emoji $specificEmoji|$specificEmoji emoji)\$");
}();
static final _emojiCodeFromClassNameRegexp = RegExp(r"emoji-([^ ]+)");

InlineContentNode parseInlineContent(dom.Node node) {
assert(_debugParserContext == _ParserContext.inline);
Expand All @@ -655,7 +659,6 @@ class _ZulipContentParser {

final element = node;
final localName = element.localName;
final classes = element.classes;
final className = element.className;
List<InlineContentNode> nodes() => parseInlineContentList(element.nodes);

Expand Down Expand Up @@ -691,14 +694,9 @@ class _ZulipContentParser {
}

if (localName == 'span'
&& classes.length == 2
&& classes.contains('emoji')
&& classes.every(_emojiClassRegexp.hasMatch)) {
final emojiCode = classes
.firstWhere((className) => className.startsWith('emoji-'))
.replaceFirst('emoji-', '');
assert(emojiCode.isNotEmpty);

&& _emojiClassNameRegexp.hasMatch(className)) {
final emojiCode = _emojiCodeFromClassNameRegexp.firstMatch(className)!
.group(1)!;
final unicode = tryParseEmojiCodeToUnicode(emojiCode);
if (unicode == null) return unimplemented();
return UnicodeEmojiNode(emojiUnicode: unicode, debugHtmlNode: debugHtmlNode);
Expand Down
5 changes: 5 additions & 0 deletions test/model/content_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,11 @@ void main() {
'<p><span aria-label="thumbs up" class="emoji emoji-1f44d" role="img" title="thumbs up">:thumbs_up:</span></p>',
const UnicodeEmojiNode(emojiUnicode: '\u{1f44d}')); // "👍"

testParseInline('parse Unicode emoji, encoded in span element, class order reversed',
// ":thumbs_up:" (hypothetical server variation)
'<p><span aria-label="thumbs up" class="emoji-1f44d emoji" role="img" title="thumbs up">:thumbs_up:</span></p>',
const UnicodeEmojiNode(emojiUnicode: '\u{1f44d}')); // "👍"

testParseInline('parse Unicode emoji, encoded in span element, multiple codepoints',
// ":transgender_flag:"
'<p><span aria-label="transgender flag" class="emoji emoji-1f3f3-fe0f-200d-26a7-fe0f" role="img" title="transgender flag">:transgender_flag:</span></p>',
Expand Down

0 comments on commit 6956d5d

Please sign in to comment.