Skip to content

Commit

Permalink
fix emoji size when using "marked" markdown
Browse files Browse the repository at this point in the history
The old logic of determing whether a message consists solely of emoji
was pretty primitive: it just checked whether there are top level text
nodes that contain text.

This fails when using the "marked" markdown parser because it can return
a <p> node, or a <table> node or whatever as the first element; the
old logic would conclude that the message only uses emoji and render
the emojis in "big".

Rewrite the logic to scan every child node, ignoring "emoji" nodes, and
when there is a text node with some text, we know that the message
doesn't consist solely of emojis.

This approach also works with the "original" markdown parser.
  • Loading branch information
Dennis Brakhane committed Jun 24, 2019
1 parent c17a04d commit 78dcc24
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion app/emoji/client/emojiParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,30 @@ Tracker.autorun(() => {

const emojis = Array.from(checkEmojiOnly.querySelectorAll('.emoji:not(:empty), .emojione:not(:empty)'));

const emojiOnly = emojis.length && !Array.from(checkEmojiOnly.childNodes).filter((node) => node.nodeType === Node.TEXT_NODE).map((el) => el.nodeValue).join('').trim();
const walker = document.createTreeWalker(
checkEmojiOnly,
NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_TEXT,
(node) => {
if (node.nodeType === Node.ELEMENT_NODE && (
node.classList.contains('emojione')
|| node.classList.contains('emoji')
)) {
return NodeFilter.FILTER_REJECT;
} // else
return NodeFilter.FILTER_ACCEPT;
}
);

let hasText = false;

while (walker.nextNode()) {
if (walker.currentNode.nodeType === Node.TEXT_NODE && walker.currentNode.nodeValue.trim() !== '') {
hasText = true;
break;
}
}

const emojiOnly = emojis.length && !hasText;

if (emojiOnly) {
for (let i = 0, len = emojis.length; i < len; i++) {
Expand Down

0 comments on commit 78dcc24

Please sign in to comment.