diff --git a/routes/_utils/replaceEmoji.js b/routes/_utils/replaceEmoji.js index 1abdf42..8b6b811 100644 --- a/routes/_utils/replaceEmoji.js +++ b/routes/_utils/replaceEmoji.js @@ -1,5 +1,8 @@ import { getEmojiRegex } from './emojiRegex' +// \ufe0f is a variation selector, which seems to appear for some reason in e.g. ™ +let NON_EMOJI_REGEX = new RegExp('^[0-9#*™®\ufe0f]+$') + // replace emoji in HTML with something else, safely skipping HTML tags export function replaceEmoji (string, replacer) { let output = '' @@ -9,7 +12,7 @@ export function replaceEmoji (string, replacer) { function safeReplacer (substring) { // emoji regex matches digits and pound sign https://git.io/fpl6J - if (substring.match(/^[0-9#*™®]+$/)) { + if (substring.match(NON_EMOJI_REGEX)) { return substring } return replacer(substring) diff --git a/tests/unit/test-emoji.js b/tests/unit/test-emoji.js index 8da04b2..c1d3c5d 100644 --- a/tests/unit/test-emoji.js +++ b/tests/unit/test-emoji.js @@ -70,6 +70,17 @@ describe('test-emoji.js', function () { replaceEmoji(`woot !@#$%^&*()~` + '`' + `{[}]:;"'<,>.?/£™℠®`, replacer), `woot !@#$%^&*()~` + '`' + `{[}]:;"'<,>.?/£™℠®` ) + + assert.strictEqual( + replaceEmoji(`woot !@#$%^&*()~` + '`' + `{[}]:;"'<,>.?/£™℠®`, replacer), + `woot !@#$%^&*()~` + '`' + `{[}]:;"'<,>.?/£™℠®` + ) + + // hidden VARIATION SELECTOR character is in here + assert.strictEqual( + replaceEmoji("
It's shapes™️ ... continued
", replacer), + "It's shapes™️ ... continued
" + ) }) it('does not replace emoji inside HTML tags', function () {