bigbuffet-rw/app/soapbox/utils/rich_content.js

26 lines
936 B
JavaScript
Raw Normal View History

2021-07-09 12:22:01 -07:00
// Returns `true` if the node contains only emojis, up to a limit
2021-07-09 16:39:05 -07:00
export const onlyEmoji = (node, limit = 1, ignoreMentions = true) => {
2021-07-09 12:22:01 -07:00
if (!node) return false;
2021-07-09 16:39:05 -07:00
2021-07-10 10:41:57 -07:00
try {
// Remove mentions before checking content
if (ignoreMentions) {
node = node.cloneNode(true);
node.querySelectorAll('a.mention').forEach(m => m.parentNode.removeChild(m));
}
2021-07-09 16:39:05 -07:00
2021-07-10 10:41:57 -07:00
if (node.textContent.replaceAll(' ', '') !== '') return false;
const emojis = Array.from(node.querySelectorAll('img.emojione'));
if (emojis.length === 0) return false;
if (emojis.length > limit) return false;
const images = Array.from(node.querySelectorAll('img'));
if (images.length > emojis.length) return false;
return true;
} catch (e) {
// Apparently some browsers can't handle `node.textContent.replaceAll`??
// If anything in here crashes, skipping it is inconsequential.
console.error(e);
return false;
}
2021-07-09 12:22:01 -07:00
};