]> cat aescling's git repositories - mastodon.git/blob - app/javascript/mastodon/emoji.js
Fix shortname problem in emojify() (regression from #5016) (#5032)
[mastodon.git] / app / javascript / mastodon / emoji.js
1 import { unicodeMapping } from './emojione_light';
2 import Trie from 'substring-trie';
3
4 const trie = new Trie(Object.keys(unicodeMapping));
5
6 const emojify = (str, customEmojis = {}) => {
7 let rtn = '';
8 for (;;) {
9 let match, i = 0, tag;
10 while (i < str.length && (tag = '<&'.indexOf(str[i])) === -1 && str[i] !== ':' && !(match = trie.search(str.slice(i)))) {
11 i += str.codePointAt(i) < 65536 ? 1 : 2;
12 }
13 if (i === str.length)
14 break;
15 else if (tag >= 0) {
16 const tagend = str.indexOf('>;'[tag], i + 1) + 1;
17 if (!tagend)
18 break;
19 rtn += str.slice(0, tagend);
20 str = str.slice(tagend);
21 } else if (str[i] === ':') {
22 try {
23 // if replacing :shortname: succeed, exit this block with "continue"
24 const closeColon = str.indexOf(':', i + 1) + 1;
25 if (!closeColon) throw null; // no pair of ':'
26 const lt = str.indexOf('<', i + 1);
27 if (!(lt === -1 || lt >= closeColon)) throw null; // tag appeared before closing ':'
28 const shortname = str.slice(i, closeColon);
29 if (shortname in customEmojis) {
30 rtn += str.slice(0, i) + `<img draggable="false" class="emojione" alt="${shortname}" title="${shortname}" src="${customEmojis[shortname]}" />`;
31 str = str.slice(closeColon);
32 continue;
33 }
34 } catch (e) {}
35 // replacing :shortname: failed
36 rtn += str.slice(0, i + 1);
37 str = str.slice(i + 1);
38 } else {
39 const [filename, shortCode] = unicodeMapping[match];
40 rtn += str.slice(0, i) + `<img draggable="false" class="emojione" alt="${match}" title=":${shortCode}:" src="/emoji/${filename}.svg" />`;
41 str = str.slice(i + match.length);
42 }
43 }
44 return rtn + str;
45 };
46
47 export default emojify;
This page took 0.198953 seconds and 4 git commands to generate.