Skip to content
This repository was archived by the owner on Mar 1, 2019. It is now read-only.

Commit

Permalink
Merge pull request #272 from buildkite/revert-270-update-unicode-emoj…
Browse files Browse the repository at this point in the history
…i-to-emoji-4

Revert "Update support for Emoji 4.0 (Unicode 9.0)"
  • Loading branch information
ticky authored May 24, 2017
2 parents 7352160 + c6ac54d commit 9bc4ca6
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 125 deletions.
50 changes: 15 additions & 35 deletions app/lib/Emoji.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,35 @@
import escape from 'escape-html';
import emojiRegex from 'emoji-regex';
import BUILDKITE_EMOJI from '../emojis/buildkite';
import UNICODE_EMOJI from '../emojis/apple';
import BUILDKITE_EMOJIS from '../emojis/buildkite';
import APPLE_EMOJIS from '../emojis/apple';

const UNICODE_REGEXP = emojiRegex();
const UNICODE_REGEXP = new RegExp('\\ud83c[\\udf00-\\udfff]|\\ud83d[\\udc00-\\ude4f]|\\ud83d[\\ude80-\\udeff]', 'g');
const COLON_REGEXP = new RegExp('\:[^\\s:]+\:', 'g');

class Emoji {
parse(string, options = {}) {
if (!string || string.length === 0) {
return '';
return "";
}

// Turn off escaping if the option is explicitly set
if (options.escape !== false) {
string = escape(string);
}

// Start with replacing BK emoji (which are more likely than Unicode emoji)
string = this._replaceColons(BUILDKITE_EMOJI, string);
// Start with replacing BK emojis (which are more likely than Apple emojis)
string = this._replace(BUILDKITE_EMOJIS, COLON_REGEXP, string);

// Then do a Unicode emoji parse
// Then do an Apple emoji parse
if (options.replaceUnicode !== false) {
string = this._replaceUnicode(UNICODE_EMOJI, string);
string = this._replace(APPLE_EMOJIS, UNICODE_REGEXP, string);
}

string = this._replaceColons(UNICODE_EMOJI, string);
string = this._replace(APPLE_EMOJIS, COLON_REGEXP, string);

return string;
}

// replaces unicode emoji with images
_replaceUnicode(catalogue, string) {
return string.replace(UNICODE_REGEXP, (match) => {
// NOTE: We accept either a normal match or a match with the VARIATION SELECTOR-16 removed
// as our Unicode catalogue lists most emoji *without* VARIATION SELECTOR-16 attached
const emojiIndex = catalogue.index[match] || catalogue.index[match.replace(/\uFE0F$/, '')];

if ((typeof emojiIndex) === 'number') {
return this._image(catalogue, catalogue.emoji[emojiIndex], match);
} else {
return match;
}
});
}

// replaces emoji shortcodes with images
_replaceColons(catalogue, string) {
// NOTE: replacements are done indirectly here, because the
// colon regexp will not catch modifiers in a single match
const matches = string.match(COLON_REGEXP);
_replace(catalogue, regexp, string) {
const matches = string.match(regexp);
const replacements = [];

// Bail if there aren't any emojis to replace
Expand Down Expand Up @@ -84,13 +64,13 @@ class Emoji {
}
}

return string.replace(COLON_REGEXP, () => replacements.shift());
return string.replace(regexp, () => replacements.shift());
}

_image({ host }, emoji, match) {
_image(catalogue, emoji) {
// Emoji catalogue hosts have a normalized host that always end with a "/"
const emojiUrl = `${host}${emoji.image}`;
const emojiCanonicalRepresentation = match || emoji.unicode || `:${emoji.name}:`;
const emojiUrl = `${catalogue.host}${emoji.image}`;
const emojiCanonicalRepresentation = emoji.unicode || `:${emoji.name}:`;

return `<img class="emoji" title="${emoji.name}" alt="${emojiCanonicalRepresentation}" src="${emojiUrl}" draggable="false" />`;
}
Expand Down
31 changes: 5 additions & 26 deletions app/lib/Emoji.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,37 +14,16 @@ function mockLoadWebpackedEmojis(catalogue) {
jest.mock('../emojis/buildkite', () => mockLoadWebpackedEmojis('buildkite'));
jest.mock('../emojis/apple', () => mockLoadWebpackedEmojis('apple'));

const EMOJI_TESTS = [
':buildkite:',
':wave::skin-tone-3:',
'👋🏿',
'👍 :buildkite:',
'™',
'👩‍👩‍👧',
':woman-woman-girl:',
'👩🏻‍🏫',
'🇦🇺💜🇨🇦',
'String with :rocket:',
'String with \\:rocket\\:',
'arn\\:aws\\:s3:::bucket',
'⛄',
'💩 in the 💨',
'©️©',
'🅱️epis'
];

describe('Emoji', () => {
describe('parse', () => {
it('turns emojis into HTML', () => {
EMOJI_TESTS.forEach((testcase) => {
expect(Emoji.parse(testcase)).toMatchSnapshot();
});
expect(Emoji.parse(":buildkite:")).toMatchSnapshot();
expect(Emoji.parse(":wave::skin-tone-3:")).toMatchSnapshot();
expect(Emoji.parse("👋🏿")).toMatchSnapshot();
});

it('allows you to turn off unicode parsing of emojis', () => {
EMOJI_TESTS.forEach((testcase) => {
expect(Emoji.parse(testcase, { replaceUnicode: false })).toMatchSnapshot();
});
it('allows you to turn of unicode parsing of emojis', () => {
expect(Emoji.parse("👍 :buildkite:", { replaceUnicode: false })).toMatchSnapshot();
});

it('escapes other HTML by default', () => {
Expand Down
60 changes: 2 additions & 58 deletions app/lib/__snapshots__/Emoji.spec.js.snap
Original file line number Diff line number Diff line change
@@ -1,38 +1,8 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Emoji parse allows you to turn off HTML escaping 1`] = `"<img class=\\"emoji\\" title=\\"tada\\" alt=\\"🎉\\" src=\\"emoji-host.com/img-apple-64/1f389.png\\" draggable=\\"false\\" /> <strong>This be strong...</strong>"`;

exports[`Emoji parse allows you to turn off unicode parsing of emojis 1`] = `"<img class=\\"emoji\\" title=\\"buildkite\\" alt=\\":buildkite:\\" src=\\"emoji-host.com/img-buildkite-64/buildkite.png\\" draggable=\\"false\\" />"`;

exports[`Emoji parse allows you to turn off unicode parsing of emojis 2`] = `"<img class=\\"emoji\\" title=\\"wave\\" alt=\\"👋🏼\\" src=\\"emoji-host.com/img-apple-64/1f44b-1f3fc.png\\" draggable=\\"false\\" />"`;

exports[`Emoji parse allows you to turn off unicode parsing of emojis 3`] = `"👋🏿"`;

exports[`Emoji parse allows you to turn off unicode parsing of emojis 4`] = `"👍 <img class=\\"emoji\\" title=\\"buildkite\\" alt=\\":buildkite:\\" src=\\"emoji-host.com/img-buildkite-64/buildkite.png\\" draggable=\\"false\\" />"`;

exports[`Emoji parse allows you to turn off unicode parsing of emojis 5`] = `"™"`;

exports[`Emoji parse allows you to turn off unicode parsing of emojis 6`] = `"👩‍👩‍👧"`;

exports[`Emoji parse allows you to turn off unicode parsing of emojis 7`] = `"<img class=\\"emoji\\" title=\\"woman-woman-girl\\" alt=\\"👩‍👩‍👧\\" src=\\"emoji-host.com/img-apple-64/1f469-200d-1f469-200d-1f467.png\\" draggable=\\"false\\" />"`;

exports[`Emoji parse allows you to turn off unicode parsing of emojis 8`] = `"👩🏻‍🏫"`;

exports[`Emoji parse allows you to turn off unicode parsing of emojis 9`] = `"🇦🇺💜🇨🇦"`;

exports[`Emoji parse allows you to turn off unicode parsing of emojis 10`] = `"String with <img class=\\"emoji\\" title=\\"rocket\\" alt=\\"🚀\\" src=\\"emoji-host.com/img-apple-64/1f680.png\\" draggable=\\"false\\" />"`;

exports[`Emoji parse allows you to turn off unicode parsing of emojis 11`] = `"String with \\\\:rocket\\\\:"`;

exports[`Emoji parse allows you to turn off unicode parsing of emojis 12`] = `"arn\\\\:aws\\\\:s3:::bucket"`;

exports[`Emoji parse allows you to turn off unicode parsing of emojis 13`] = `"⛄"`;

exports[`Emoji parse allows you to turn off unicode parsing of emojis 14`] = `"💩 in the 💨"`;
exports[`Emoji parse allows you to turn of unicode parsing of emojis 1`] = `"👍 <img class=\\"emoji\\" title=\\"buildkite\\" alt=\\":buildkite:\\" src=\\"emoji-host.com/img-buildkite-64/buildkite.png\\" draggable=\\"false\\" />"`;

exports[`Emoji parse allows you to turn off unicode parsing of emojis 15`] = `"©️©"`;

exports[`Emoji parse allows you to turn off unicode parsing of emojis 16`] = `"🅱️epis"`;
exports[`Emoji parse allows you to turn off HTML escaping 1`] = `"<img class=\\"emoji\\" title=\\"tada\\" alt=\\"🎉\\" src=\\"emoji-host.com/img-apple-64/1f389.png\\" draggable=\\"false\\" /> <strong>This be strong...</strong>"`;

exports[`Emoji parse escapes other HTML by default 1`] = `"<img class=\\"emoji\\" title=\\"tada\\" alt=\\"🎉\\" src=\\"emoji-host.com/img-apple-64/1f389.png\\" draggable=\\"false\\" /> &lt;strong&gt;This be strong...&lt;/strong&gt;"`;

Expand All @@ -41,29 +11,3 @@ exports[`Emoji parse turns emojis into HTML 1`] = `"<img class=\\"emoji\\" title
exports[`Emoji parse turns emojis into HTML 2`] = `"<img class=\\"emoji\\" title=\\"wave\\" alt=\\"👋🏼\\" src=\\"emoji-host.com/img-apple-64/1f44b-1f3fc.png\\" draggable=\\"false\\" />"`;

exports[`Emoji parse turns emojis into HTML 3`] = `"<img class=\\"emoji\\" title=\\"wave\\" alt=\\"👋🏿\\" src=\\"emoji-host.com/img-apple-64/1f44b-1f3ff.png\\" draggable=\\"false\\" />"`;

exports[`Emoji parse turns emojis into HTML 4`] = `"<img class=\\"emoji\\" title=\\"+1\\" alt=\\"👍\\" src=\\"emoji-host.com/img-apple-64/1f44d.png\\" draggable=\\"false\\" /> <img class=\\"emoji\\" title=\\"buildkite\\" alt=\\":buildkite:\\" src=\\"emoji-host.com/img-buildkite-64/buildkite.png\\" draggable=\\"false\\" />"`;

exports[`Emoji parse turns emojis into HTML 5`] = `"™"`;

exports[`Emoji parse turns emojis into HTML 6`] = `"<img class=\\"emoji\\" title=\\"woman-woman-girl\\" alt=\\"👩‍👩‍👧\\" src=\\"emoji-host.com/img-apple-64/1f469-200d-1f469-200d-1f467.png\\" draggable=\\"false\\" />"`;

exports[`Emoji parse turns emojis into HTML 7`] = `"<img class=\\"emoji\\" title=\\"woman-woman-girl\\" alt=\\"👩‍👩‍👧\\" src=\\"emoji-host.com/img-apple-64/1f469-200d-1f469-200d-1f467.png\\" draggable=\\"false\\" />"`;

exports[`Emoji parse turns emojis into HTML 8`] = `"<img class=\\"emoji\\" title=\\"woman\\" alt=\\"👩🏻\\" src=\\"emoji-host.com/img-apple-64/1f469-1f3fb.png\\" draggable=\\"false\\" />‍<img class=\\"emoji\\" title=\\"school\\" alt=\\"🏫\\" src=\\"emoji-host.com/img-apple-64/1f3eb.png\\" draggable=\\"false\\" />"`;

exports[`Emoji parse turns emojis into HTML 9`] = `"<img class=\\"emoji\\" title=\\"flag-au\\" alt=\\"🇦🇺\\" src=\\"emoji-host.com/img-apple-64/1f1e6-1f1fa.png\\" draggable=\\"false\\" /><img class=\\"emoji\\" title=\\"purple_heart\\" alt=\\"💜\\" src=\\"emoji-host.com/img-apple-64/1f49c.png\\" draggable=\\"false\\" /><img class=\\"emoji\\" title=\\"flag-ca\\" alt=\\"🇨🇦\\" src=\\"emoji-host.com/img-apple-64/1f1e8-1f1e6.png\\" draggable=\\"false\\" />"`;

exports[`Emoji parse turns emojis into HTML 10`] = `"String with <img class=\\"emoji\\" title=\\"rocket\\" alt=\\"🚀\\" src=\\"emoji-host.com/img-apple-64/1f680.png\\" draggable=\\"false\\" />"`;

exports[`Emoji parse turns emojis into HTML 11`] = `"String with \\\\:rocket\\\\:"`;

exports[`Emoji parse turns emojis into HTML 12`] = `"arn\\\\:aws\\\\:s3:::bucket"`;

exports[`Emoji parse turns emojis into HTML 13`] = `"<img class=\\"emoji\\" title=\\"snowman_without_snow\\" alt=\\"⛄\\" src=\\"emoji-host.com/img-apple-64/26c4.png\\" draggable=\\"false\\" />"`;

exports[`Emoji parse turns emojis into HTML 14`] = `"<img class=\\"emoji\\" title=\\"hankey\\" alt=\\"💩\\" src=\\"emoji-host.com/img-apple-64/1f4a9.png\\" draggable=\\"false\\" /> in the <img class=\\"emoji\\" title=\\"dash\\" alt=\\"💨\\" src=\\"emoji-host.com/img-apple-64/1f4a8.png\\" draggable=\\"false\\" />"`;

exports[`Emoji parse turns emojis into HTML 15`] = `"<img class=\\"emoji\\" title=\\"copyright\\" alt=\\"©️\\" src=\\"emoji-host.com/img-apple-64/00a9.png\\" draggable=\\"false\\" />©"`;

exports[`Emoji parse turns emojis into HTML 16`] = `"<img class=\\"emoji\\" title=\\"b\\" alt=\\"🅱️\\" src=\\"emoji-host.com/img-apple-64/1f171.png\\" draggable=\\"false\\" />epis"`;
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@
"codemirror": "^5.25.0",
"create-react-class": "^15.5.3",
"deepmerge": "^1.3.0",
"emoji-regex": "^6.4.2",
"es6-error": "^4.0.0",
"escape-html": "^1.0.3",
"eventemitter3": "^2.0.2",
Expand Down
2 changes: 1 addition & 1 deletion vendor/emojis
Submodule emojis updated 2129 files
4 changes: 0 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2268,10 +2268,6 @@ elliptic@^6.0.0:
minimalistic-assert "^1.0.0"
minimalistic-crypto-utils "^1.0.0"

emoji-regex@^6.4.2:
version "6.4.2"
resolved "https://registry.yarnpkg.com/emoji-regex/-/emoji-regex-6.4.2.tgz#a30b6fee353d406d96cfb9fa765bdc82897eff6e"

emojis-list@^2.0.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/emojis-list/-/emojis-list-2.1.0.tgz#4daa4d9db00f9819880c79fa457ae5b09a1fd389"
Expand Down

0 comments on commit 9bc4ca6

Please sign in to comment.