[Glitch] Fix characters being emojified even when using Variation Selector 15 (text)

Port 37886c28da to glitch-soc

Signed-off-by: Claire <claire.github-309c@sitedethib.com>
This commit is contained in:
Claire 2023-04-20 16:43:55 +02:00
parent 66bda4713e
commit 0a813d96db

View file

@ -20,13 +20,18 @@ const emojiFilename = (filename) => {
}; };
const emojifyTextNode = (node, customEmojis) => { const emojifyTextNode = (node, customEmojis) => {
const VS15 = 0xFE0E;
const VS16 = 0xFE0F;
let str = node.textContent; let str = node.textContent;
const fragment = new DocumentFragment(); const fragment = new DocumentFragment();
let i = 0;
for (;;) { for (;;) {
let match, i = 0; let match;
// Skip to the next potential emoji to replace
if (customEmojis === null) { if (customEmojis === null) {
while (i < str.length && (useSystemEmojiFont || !(match = trie.search(str.slice(i))))) { while (i < str.length && (useSystemEmojiFont || !(match = trie.search(str.slice(i))))) {
i += str.codePointAt(i) < 65536 ? 1 : 2; i += str.codePointAt(i) < 65536 ? 1 : 2;
@ -37,13 +42,17 @@ const emojifyTextNode = (node, customEmojis) => {
} }
} }
let rend, replacement = null; // We reached the end of the string, nothing to replace
if (i === str.length) { if (i === str.length) {
break; break;
} else if (str[i] === ':') { }
if (!(() => {
rend = str.indexOf(':', i + 1) + 1; let rend, replacement = null;
if (!rend) return false; // no pair of ':' if (str[i] === ':') { // Potentially the start of a custom emoji shortcode
if (!(rend = str.indexOf(':', i + 1) + 1)) {
continue; // no pair of ':'
}
const shortname = str.slice(i, rend); const shortname = str.slice(i, rend);
// now got a replacee as ':shortname:' // now got a replacee as ':shortname:'
// if you want additional emoji handler, add statements below which set replacement and return true. // if you want additional emoji handler, add statements below which set replacement and return true.
@ -57,31 +66,34 @@ const emojifyTextNode = (node, customEmojis) => {
replacement.setAttribute('src', filename); replacement.setAttribute('src', filename);
replacement.setAttribute('data-original', customEmojis[shortname].url); replacement.setAttribute('data-original', customEmojis[shortname].url);
replacement.setAttribute('data-static', customEmojis[shortname].static_url); replacement.setAttribute('data-static', customEmojis[shortname].static_url);
return true; } else {
continue;
} }
return false;
})()) rend = ++i;
} else if (!useSystemEmojiFont) { // matched to unicode emoji } else if (!useSystemEmojiFont) { // matched to unicode emoji
rend = i + match.length;
// If the matched character was followed by VS15 (for selecting text presentation), skip it.
if (str.codePointAt(rend - 1) !== VS16 && str.codePointAt(rend) === VS15) {
i = rend + 1;
continue;
}
const { filename, shortCode } = unicodeMapping[match]; const { filename, shortCode } = unicodeMapping[match];
const title = shortCode ? `:${shortCode}:` : ''; const title = shortCode ? `:${shortCode}:` : '';
replacement = document.createElement('img'); replacement = document.createElement('img');
replacement.setAttribute('draggable', 'false'); replacement.setAttribute('draggable', 'false');
replacement.setAttribute('class', 'emojione'); replacement.setAttribute('class', 'emojione');
replacement.setAttribute('alt', match); replacement.setAttribute('alt', match);
replacement.setAttribute('title', title); replacement.setAttribute('title', title);
replacement.setAttribute('src', `${assetHost}/emoji/${emojiFilename(filename)}.svg`); replacement.setAttribute('src', `${assetHost}/emoji/${emojiFilename(filename)}.svg`);
rend = i + match.length;
// If the matched character was followed by VS15 (for selecting text presentation), skip it.
if (str.codePointAt(rend) === 65038) {
rend += 1;
}
} }
// Add the processed-up-to-now string and the emoji replacement
fragment.append(document.createTextNode(str.slice(0, i))); fragment.append(document.createTextNode(str.slice(0, i)));
if (replacement) {
fragment.append(replacement); fragment.append(replacement);
}
str = str.slice(rend); str = str.slice(rend);
i = 0;
} }
fragment.append(document.createTextNode(str)); fragment.append(document.createTextNode(str));