Skip to content

Commit 1455740

Browse files
committed
fix: Apply emoji fix for messages with mentions
1 parent 3b22272 commit 1455740

File tree

2 files changed

+58
-16
lines changed

2 files changed

+58
-16
lines changed

projects/stream-chat-angular/src/lib/message/message.component.spec.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,41 @@ describe('MessageComponent', () => {
864864
expect(content).toContain('👨‍👩‍👧');
865865
});
866866

867+
it('should add class to emojis in Chrome', () => {
868+
component.message = {
869+
html: 'This message contains an emoji 🥑',
870+
} as any as StreamMessage;
871+
component.ngOnChanges({ message: {} as SimpleChange });
872+
873+
expect(component.messageTextParts[0].content).toContain(
874+
'class="str-chat__emoji-display-fix"'
875+
);
876+
877+
component.message = {
878+
html: '@sara what do you think about 🥑s? ',
879+
mentioned_users: [{ id: 'sara' }],
880+
} as StreamMessage;
881+
component.ngOnChanges({ message: {} as SimpleChange });
882+
883+
expect(component.messageTextParts[2].content).toContain(
884+
'class="str-chat__emoji-display-fix"'
885+
);
886+
887+
// Simulate a browser that isn't Google Chrome
888+
const chrome = (window as typeof window & { chrome: Object }).chrome;
889+
(window as typeof window & { chrome: Object | undefined }).chrome =
890+
undefined;
891+
892+
component.ngOnChanges({ message: {} as SimpleChange });
893+
894+
expect(component.messageTextParts[0].content).not.toContain(
895+
'class="str-chat__emoji-display-fix"'
896+
);
897+
898+
// Revert changes to the window object
899+
(window as typeof window & { chrome: Object }).chrome = chrome;
900+
});
901+
867902
it('should display reply count for parent messages', () => {
868903
expect(queryReplyCountButton()).toBeNull();
869904

projects/stream-chat-angular/src/lib/message/message.component.ts

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -306,28 +306,15 @@ export class MessageComponent implements OnInit, OnChanges, OnDestroy {
306306
!this.message!.mentioned_users ||
307307
this.message!.mentioned_users.length === 0
308308
) {
309-
// Wrap emojis in span to display emojis correctly in Chrome https://bugs.chromium.org/p/chromium/issues/detail?id=596223
310-
const regex = new RegExp(emojiRegex(), 'g');
311-
// Based on this: https://stackoverflow.com/questions/4565112/javascript-how-to-find-out-if-the-user-browser-is-chrome
312-
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
313-
const isChrome =
314-
!!(window as any).chrome &&
315-
typeof (window as any).opr === 'undefined';
316-
/* eslint-enable @typescript-eslint/no-unsafe-member-access */
317-
content = content.replace(
318-
regex,
319-
(match) =>
320-
`<span ${
321-
isChrome ? 'class="str-chat__emoji-display-fix"' : ''
322-
}>${match}</span>`
323-
);
309+
content = this.fixEmojiDisplay(content);
324310
this.messageTextParts = [{ content, type: 'text' }];
325311
} else {
326312
this.messageTextParts = [];
327313
let text = content;
328314
this.message!.mentioned_users.forEach((user) => {
329315
const mention = `@${user.name || user.id}`;
330-
const precedingText = text.substring(0, text.indexOf(mention));
316+
let precedingText = text.substring(0, text.indexOf(mention));
317+
precedingText = this.fixEmojiDisplay(precedingText);
331318
this.messageTextParts.push({
332319
content: precedingText,
333320
type: 'text',
@@ -340,9 +327,29 @@ export class MessageComponent implements OnInit, OnChanges, OnDestroy {
340327
text = text.replace(precedingText + mention, '');
341328
});
342329
if (text) {
330+
text = this.fixEmojiDisplay(text);
343331
this.messageTextParts.push({ content: text, type: 'text' });
344332
}
345333
}
346334
}
347335
}
336+
337+
private fixEmojiDisplay(content: string) {
338+
// Wrap emojis in span to display emojis correctly in Chrome https://bugs.chromium.org/p/chromium/issues/detail?id=596223
339+
const regex = new RegExp(emojiRegex(), 'g');
340+
// Based on this: https://stackoverflow.com/questions/4565112/javascript-how-to-find-out-if-the-user-browser-is-chrome
341+
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
342+
const isChrome =
343+
!!(window as any).chrome && typeof (window as any).opr === 'undefined';
344+
/* eslint-enable @typescript-eslint/no-unsafe-member-access */
345+
content = content.replace(
346+
regex,
347+
(match) =>
348+
`<span ${
349+
isChrome ? 'class="str-chat__emoji-display-fix"' : ''
350+
}>${match}</span>`
351+
);
352+
353+
return content;
354+
}
348355
}

0 commit comments

Comments
 (0)