2014-11-06 23:04:08 +00:00
# Twitter Emoji (Twemoji) [![Build Status](https://travis-ci.org/twitter/twemoji.svg?branch=gh-pages)](https://travis-ci.org/twitter/twemoji)
2014-11-06 22:32:19 +00:00
A simple library that provides standard Unicode [emoji ](http://en.wikipedia.org/wiki/Emoji ) support across all platforms.
2018-06-05 21:00:34 +00:00
**Twemoji v11.0** adheres to the [Unicode 11.0 spec ](https://unicode.org/versions/Unicode11.0.0/ ) and supports the [Emoji 11.0 draft spec ](https://www.unicode.org/reports/tr51/tr51-13.html )
2016-03-01 18:25:41 +00:00
2018-06-05 21:00:34 +00:00
The Twemoji library offers support for **2,841** emojis, including skin tone and gender modifiers.
2016-03-01 18:25:41 +00:00
2017-07-20 08:31:15 +00:00
#### Please note
The files in the root folder in this project are deprecated and listed for CDN and historical reasons.
Please use the files for the current version (2) instead, located under the folder `./2/` .
2016-03-01 18:25:41 +00:00
Gender equal emojis, new emojis for professions, and tweaks to existing emojis
Includes additions to address gender equality, expanded professions,
increased skin tone support, family representations, and minor
modifications to some existing emojis.
Many of the additions included are based on the [Emoji 4.0 draft
spec](http://unicode.org/reports/tr51/proposed.html) and are subject to
change prior to ratification.
This change also moves the Twemoji version number to 2.2.
### New
* Woman with turban
* Blonde woman
* Man getting face massage
* Man getting haircut
* Man frowning
* Man pouting
* Man gesturing not ok
* Man gesturing ok
* Man tipping hand
* Man raising hand
* Woman bowing deeply
* Man facepalming
* Man shrugging
* Man doing cartwheel
* Woman juggling
* Man juggling
* Woman walking
* Woman running
* Women wrestling
* Woman playing water polo
* Man playing handball
* Men with bunny ears
* Woman rowing boat
* Woman golfing
* Woman surfing
* Woman swimming
* Man with ball
* Woman weight lifting
* Woman biking
* Woman mountain biking
* Male health worker
* Female health worker
* Male judge
* Female judge
* Male pilot
* Female pilot
* Male farmer
* Female farmer
* Male cook
* Female cook
* Male student
* Female student
* Male singer
* Female singer
* Male artist
* Female artist
* Male teacher
* Female teacher
* Male factory worker
* Female factory worker
* Male technologist
* Female technologist
* Male office worker
* Female office worker
* Male mechanic
* Female mechanic
* Male scientist
* Female scientist
* Male astronaut
* Female astronaut
* Male firefighter
* Female firefighter
* Female police officer
* Female construction worker
* Female guard
* Female sleuth
* Family (man, boy)
* Family (man, boy, boy)
* Family (man, girl)
* Family (man, girl, boy)
* Family (man, girl, girl)
* Family (woman, boy)
* Family (woman, boy, boy)
* Family (woman, girl)
* Family (woman, girl, boy)
* Family (woman, girl, girl)
* UN Flag
* Female sign
* Male sign
* Staff of aesculapius
### Modified
* Man with turban
* Person with ball
* Family
* Man and woman holding hands
* Two men holding hands
* Two women holding hands
* Women with bunny ears
* Man in business suit levitating
* Sleeping accommodation
* Skier
* Snowboarder
* Flag of Réunion
* Grinning face with smiling eyes
* Roasted sweet potato
* Bird
* Man with gua pi mao
* Railway car
* Face with rolling eyes
* Astonished face
2016-09-20 17:29:59 +00:00
## CDN Support
The folks over at [MaxCDN ](https://www.maxcdn.com ) have graciously provided CDN support.
Use the following in the `<head>` tag of your HTML document(s):
2016-03-01 18:25:41 +00:00
```html
2018-06-05 21:00:34 +00:00
< script src = "//twemoji.maxcdn.com/2/twemoji.min.js?11.0" > < / script >
2016-03-01 18:25:41 +00:00
```
## Breaking changes in V2
2016-10-08 18:39:49 +00:00
_TL;DR_: there's no `variant` anymore, all callbacks receive the transformed `iconId` and in some cases the rawText too.
2016-03-01 18:25:41 +00:00
2016-10-08 18:25:07 +00:00
There are a few potentially breaking changes in `twemoji` version 2:
2016-03-01 18:25:41 +00:00
* the `parse` invoked function signature is now `(iconId, options)` instead of `(icon, options, variant)`
2016-10-08 18:25:07 +00:00
* the `attributes` function now receives `(rawText, iconId)` instead of `(icon, variant)`
* the **default** remote protocol is now **https** regardless of whether the current site is _http_ or even _file_
2016-03-01 18:25:41 +00:00
* the **default** PNG icon size is **72** pixels and **there are no other PNG assets** for 16 or 32.
Gender equal emojis, new emojis for professions, and tweaks to existing emojis
Includes additions to address gender equality, expanded professions,
increased skin tone support, family representations, and minor
modifications to some existing emojis.
Many of the additions included are based on the [Emoji 4.0 draft
spec](http://unicode.org/reports/tr51/proposed.html) and are subject to
change prior to ratification.
This change also moves the Twemoji version number to 2.2.
### New
* Woman with turban
* Blonde woman
* Man getting face massage
* Man getting haircut
* Man frowning
* Man pouting
* Man gesturing not ok
* Man gesturing ok
* Man tipping hand
* Man raising hand
* Woman bowing deeply
* Man facepalming
* Man shrugging
* Man doing cartwheel
* Woman juggling
* Man juggling
* Woman walking
* Woman running
* Women wrestling
* Woman playing water polo
* Man playing handball
* Men with bunny ears
* Woman rowing boat
* Woman golfing
* Woman surfing
* Woman swimming
* Man with ball
* Woman weight lifting
* Woman biking
* Woman mountain biking
* Male health worker
* Female health worker
* Male judge
* Female judge
* Male pilot
* Female pilot
* Male farmer
* Female farmer
* Male cook
* Female cook
* Male student
* Female student
* Male singer
* Female singer
* Male artist
* Female artist
* Male teacher
* Female teacher
* Male factory worker
* Female factory worker
* Male technologist
* Female technologist
* Male office worker
* Female office worker
* Male mechanic
* Female mechanic
* Male scientist
* Female scientist
* Male astronaut
* Female astronaut
* Male firefighter
* Female firefighter
* Female police officer
* Female construction worker
* Female guard
* Female sleuth
* Family (man, boy)
* Family (man, boy, boy)
* Family (man, girl)
* Family (man, girl, boy)
* Family (man, girl, girl)
* Family (woman, boy)
* Family (woman, boy, boy)
* Family (woman, girl)
* Family (woman, girl, boy)
* Family (woman, girl, girl)
* UN Flag
* Female sign
* Male sign
* Staff of aesculapius
### Modified
* Man with turban
* Person with ball
* Family
* Man and woman holding hands
* Two men holding hands
* Two women holding hands
* Women with bunny ears
* Man in business suit levitating
* Sleeping accommodation
* Skier
* Snowboarder
* Flag of Réunion
* Grinning face with smiling eyes
* Roasted sweet potato
* Bird
* Man with gua pi mao
* Railway car
* Face with rolling eyes
* Astonished face
2016-09-20 17:29:59 +00:00
* in order to access latest assets you need to specify *folder* `2/72x72` or `2/svg` .
2016-03-01 18:25:41 +00:00
2016-10-08 18:25:07 +00:00
Everything else is pretty much the same, so if you were using the defaults, all you need to do is to add the version `2/` before the `twemoji.js` file you were using.
2016-03-01 18:25:41 +00:00
2014-11-06 22:32:19 +00:00
## API
2016-10-08 18:25:07 +00:00
Following are all the methods exposed in the `twemoji` namespace.
2014-11-06 22:32:19 +00:00
2016-03-01 18:25:41 +00:00
### twemoji.parse( ... ) V1
2014-11-06 22:32:19 +00:00
2016-10-08 18:25:07 +00:00
This is the main parsing utility and has 3 overloads per parsing type.
2014-11-06 22:32:19 +00:00
2016-10-08 18:25:07 +00:00
There are mainly two kinds of parsing: [string parsing ](https://github.com/twitter/twemoji#string-parsing ) and [DOM parsing ](https://github.com/twitter/twemoji#dom-parsing ).
2014-11-06 22:32:19 +00:00
2016-10-08 18:25:07 +00:00
Each of them accepts a callback to generate an image source or an options object with parsing info.
2014-11-06 22:32:19 +00:00
2016-10-08 18:25:07 +00:00
Here is a walkthrough of all parsing possibilities:
2014-11-06 22:32:19 +00:00
2016-03-01 18:25:41 +00:00
##### string parsing (V1)
2016-10-08 18:25:07 +00:00
Given a generic string, replaces all emoji with an `<img>` tag.
2014-11-06 22:32:19 +00:00
2016-10-08 18:25:07 +00:00
While this can be used to inject emoji via image tags in `innerHTML` , please note that this method does not sanitize the string or prevent malicious code from being executed. As an example, if the text contains a `<script>` tag, it **will not** be converted into `<script>` since it's out of this method's scope to prevent these kind of attacks.
2014-11-06 22:32:19 +00:00
2014-11-11 16:07:21 +00:00
However, for already sanitized strings, this method can be considered safe enough. Please see DOM parsing if security is one of your major concerns.
2014-11-06 22:32:19 +00:00
```js
twemoji.parse('I \u2764\uFE0F emoji!');
// will produce
/*
I < img
class="emoji"
draggable="false"
alt="❤️"
2017-07-20 08:34:28 +00:00
src="https://twemoji.maxcdn.com/36x36/2764.png"/> emoji!
2014-11-06 22:32:19 +00:00
*/
```
2014-11-11 16:07:21 +00:00
_string parsing + callback_
2016-10-08 18:25:07 +00:00
If a callback is passed, the value of the `src` attribute will be the value returned by the callback.
2014-11-06 22:32:19 +00:00
```js
twemoji.parse(
'I \u2764\uFE0F emoji!',
function(icon, options, variant) {
return '/assets/' + options.size + '/' + icon + '.gif';
}
);
// will produce
/*
I < img
class="emoji"
draggable="false"
alt="❤️"
2017-07-20 08:34:28 +00:00
src="/assets/36x36/2764.gif"/> emoji!
2014-11-06 22:32:19 +00:00
*/
```
2014-11-11 16:07:21 +00:00
By default, the `options.size` parameter will be the string `"36x36"` and the `variant` will be an optional `\uFE0F` char that is usually ignored by default. If your assets include or distinguish between `\u2764\uFE0F` and `\u2764` , you might want to use such a variable.
2014-11-06 22:32:19 +00:00
2014-11-11 16:07:21 +00:00
_string parsing + callback returning_ `falsy`
2016-10-08 18:25:07 +00:00
If the callback returns "falsy values" such as `null` , `undefined` , `0` , `false` , or an empty string, nothing will change for that specific emoji.
2014-11-06 22:32:19 +00:00
```js
var i = 0;
twemoji.parse(
'emoji, m\u2764\uFE0Fn am\u2764\uFE0Fur',
function(icon, options, variant) {
if (i++ === 0) {
return; // no changes made first call
}
return '/assets/' + icon + options.ext;
}
);
// will produce
/*
emoji, m❤️ n am< img
class="emoji"
draggable="false"
alt="❤️"
2017-07-20 08:34:28 +00:00
src="/assets/2764.png"/>ur
2014-11-06 22:32:19 +00:00
*/
```
2014-11-11 16:07:21 +00:00
_string parsing + object_
2016-10-08 18:25:07 +00:00
2014-11-06 22:32:19 +00:00
In case an object is passed as second parameter, the passed `options` object will reflect its properties.
```js
twemoji.parse(
'I \u2764\uFE0F emoji!',
{
callback: function(icon, options) {
return '/assets/' + options.size + '/' + icon + '.gif';
},
size: 128
}
);
// will produce
/*
I < img
class="emoji"
draggable="false"
alt="❤️"
2017-07-20 08:34:28 +00:00
src="/assets/128x128/2764.gif"/> emoji!
2014-11-06 22:32:19 +00:00
*/
```
##### DOM parsing
2016-10-08 18:25:07 +00:00
In contrast to `string` parsing, if the first argument is an `HTMLElement` , generated image tags will replace emoji that are **inside `#text` nodes only** without compromising surrounding nodes or listeners, and completely avoiding the usage of `innerHTML` .
2014-11-06 22:32:19 +00:00
2016-10-08 18:25:07 +00:00
If security is a major concern, this parsing can be considered the safest option but with a slight performance penalty due to DOM operations that are inevitably *costly* .
2014-11-06 22:32:19 +00:00
```js
var div = document.createElement('div');
div.textContent = 'I \u2764\uFE0F emoji!';
document.body.appendChild(div);
twemoji.parse(document.body);
var img = div.querySelector('img');
// note the div is preserved
img.parentNode === div; // true
2014-11-07 23:36:32 +00:00
img.src; // https://twemoji.maxcdn.com/36x36/2764.png
2014-11-06 22:32:19 +00:00
img.alt; // \u2764\uFE0F
2014-11-07 23:36:32 +00:00
img.className; // emoji
2014-11-06 22:32:19 +00:00
img.draggable; // false
```
2016-10-08 18:25:07 +00:00
All other overloads described for `string` are available in exactly the same way for DOM parsing.
2014-11-06 22:32:19 +00:00
### Object as parameter
2016-10-08 18:25:07 +00:00
Here's the list of properties accepted by the optional object that can be passed to the `parse` function.
2014-11-06 22:32:19 +00:00
```js
{
2014-11-07 23:36:32 +00:00
callback: Function, // default the common replacer
2015-04-11 13:49:52 +00:00
attributes: Function, // default returns {}
2014-11-07 23:36:32 +00:00
base: string, // default MaxCDN
ext: string, // default ".png"
2014-11-08 00:21:51 +00:00
className: string, // default "emoji"
2014-11-07 23:36:32 +00:00
size: string|number, // default "36x36"
2014-11-08 00:21:51 +00:00
folder: string // in case it's specified
// it replaces .size info, if any
2014-11-06 22:32:19 +00:00
}
```
##### callback
2016-10-08 18:25:07 +00:00
The function to invoke in order to generate image `src` (s).
2014-11-06 22:32:19 +00:00
By default it is a function like the following one:
```js
2015-01-02 22:04:33 +00:00
function imageSourceGenerator(icon, options) {
2014-11-06 22:32:19 +00:00
return ''.concat(
options.base, // by default Twitter Inc. CDN
options.size, // by default "36x36" string
'/',
icon, // the found emoji as code point
options.ext // by default ".png"
);
}
```
2016-03-01 18:25:41 +00:00
##### attributes (V1)
2015-04-11 13:49:52 +00:00
The function to invoke in order to generate additional, custom attributes for the image tag.
By default it is a function like the following one:
```js
function attributesCallback(icon, variant) {
return {
title: 'Emoji: ' + icon + variant
};
}
```
Event handlers cannot be specified via this method, and twemoji-provided attributes (src, alt, className, draggable) cannot be re-defined.
2014-11-06 22:32:19 +00:00
##### base
2014-11-11 16:07:21 +00:00
The default url is the same as `twemoji.base` , so if you modify the former, it will reflect as default for all parsed strings or nodes.
2014-11-06 22:32:19 +00:00
##### ext
2014-11-11 16:07:21 +00:00
The default image extension is the same as `twemoji.ext` which is `".png"` .
2014-11-06 22:32:19 +00:00
If you modify the former, it will reflect as default for all parsed strings or nodes.
2014-11-08 00:21:51 +00:00
##### className
2016-10-08 18:25:07 +00:00
The default `class` for each generated image is `emoji` . It is possible to specify a different one through this property.
2014-11-08 00:21:51 +00:00
2014-11-06 22:32:19 +00:00
##### size
2016-10-08 18:25:07 +00:00
The default asset size is the same as `twemoji.size` which is `"36x36"` .
2014-11-06 22:32:19 +00:00
If you modify the former, it will reflect as default for all parsed strings or nodes.
2014-11-08 00:21:51 +00:00
##### folder
2016-10-08 18:25:07 +00:00
In case you don't want to specify a size for the image. It is possible to choose a folder, as in the case of SVG emoji.
2014-11-08 00:21:51 +00:00
```js
twemoji.parse(genericNode, {
folder: 'svg',
ext: '.svg'
});
```
2016-10-08 18:25:07 +00:00
This will generate urls such `https://twemoji.maxcdn.com/svg/2764.svg` instead of using a specific size based image.
2014-11-08 00:21:51 +00:00
2016-04-27 12:52:38 +00:00
## Utilities
Basic utilities / helpers to convert code points to JavaScript surrogates and vice versa.
#### twemoji.convert.fromCodePoint()
2016-10-08 18:25:07 +00:00
For a given HEX codepoint, returns UTF-16 surrogate pairs.
2016-04-27 12:52:38 +00:00
```js
twemoji.convert.fromCodePoint('1f1e8');
// "\ud83c\udde8"
```
#### twemoji.convert.toCodePoint()
2016-10-08 18:25:07 +00:00
For given UTF-16 surrogate pairs, returns the equivalent HEX codepoint.
2016-04-27 12:52:38 +00:00
```js
twemoji.convert.toCodePoint('\ud83c\udde8\ud83c\uddf3');
// "1f1e8-1f1f3"
2016-06-07 16:28:35 +00:00
2016-04-27 12:52:38 +00:00
twemoji.convert.toCodePoint('\ud83c\udde8\ud83c\uddf3', '~');
// "1f1e8~1f1f3"
```
2014-11-08 00:21:51 +00:00
2014-11-06 22:32:19 +00:00
## Tips
#### Inline Styles
If you'd like to size the emoji according to the surrounding text, you can add the following CSS to your stylesheet:
```
img.emoji {
height: 1em;
width: 1em;
margin: 0 .05em 0 .1em;
vertical-align: -0.1em;
}
```
This will make sure emoji derive their width and height from the `font-size` of the text they're shown with. It also adds just a little bit of space before and after each emoji, and pulls them upwards a little bit for better optical alignment.
#### UTF-8 Character Set
2016-10-08 18:25:07 +00:00
To properly support emoji, the document character set must be set to UTF-8. This can done by including the following meta tag in the document `<head>`
2014-11-06 22:32:19 +00:00
```html
< meta charset = "utf-8" >
```
2016-03-01 18:25:41 +00:00
#### Exclude Characters (V1)
2014-11-06 22:32:19 +00:00
To exclude certain characters from being replaced by twemoji.js, call twemoji.parse() with a callback, returning false for the specific unicode icon. For example:
```js
twemoji.parse(document.body, {
callback: function(icon, options, variant) {
switch ( icon ) {
2015-02-12 00:01:20 +00:00
case 'a9': // © copyright
case 'ae': // ® registered trademark
case '2122': // ™ trademark
2014-11-06 22:32:19 +00:00
return false;
}
return ''.concat(options.base, options.size, '/', icon, options.ext);
}
});
```
### Build
2016-03-01 18:30:59 +00:00
If you'd like to test and/or contribute please follow these instructions.
2014-11-06 22:32:19 +00:00
2016-03-01 18:30:59 +00:00
```bash
# clone this repo
git clone https://github.com/twitter/twemoji.git
cd twemoji
# install dependencies
npm install
# generate 2/twemoji*.js files
./2/utils/generate
```
2016-10-08 18:25:07 +00:00
If you'd like to test and/or propose some changes to the V2 library please change the `./2/utils/generate` file at its end so that everything will be generated properly once launched.
2014-11-06 22:32:19 +00:00
2014-11-11 02:38:57 +00:00
## Attribution Requirements
2015-11-28 08:15:54 +00:00
As an open source project, attribution is critical from a legal, practical and motivational perspective in our opinion. The graphics are licensed under the CC-BY 4.0 which has a pretty good guide on [best practices for attribution ](https://wiki.creativecommons.org/Best_practices_for_attribution ).
2014-11-11 02:38:57 +00:00
2014-11-11 17:38:14 +00:00
However, we consider the guide a bit onerous and as a project, will accept a mention in a project README or an 'About' section or footer on a website. In mobile applications, a common place would be in the Settings/About section (for example, see the mobile Twitter application Settings->About->Legal section). We would consider a mention in the HTML/JS source sufficient also.
2014-11-11 02:38:57 +00:00
2014-11-11 03:50:52 +00:00
## Community Projects
2016-03-10 21:00:16 +00:00
* [Twemoji Awesome ](http://ellekasai.github.io/twemoji-awesome/ ) by [@ellekasai ](https://twitter.com/ellekasai/status/531979044036698112 ): Use Twemoji using CSS classes (like [Font Awesome ](http://fortawesome.github.io/Font-Awesome/ )).
2015-11-28 08:15:54 +00:00
* [Twemoji Ruby ](https://github.com/jollygoodcode/twemoji ) by [@JollyGoodCode ](https://twitter.com/jollygoodcode ): Use Twemoji in Ruby.
2015-12-11 11:53:29 +00:00
* [Twemoji for Pencil ](https://github.com/nathanielw/Twemoji-for-Pencil ) by [@Nathanielnw ](https://twitter.com/nathanielnw ): Use Twemoji in Pencil.
2016-12-24 14:46:57 +00:00
* [FrwTwemoji - Twemoji in dotnet ](http://github.frenchw.net/FrwTwemoji/ ) by [@FrenchW ](https://twitter.com/frenchw ): Use Twemoji in any dotnet project (C#, asp.net ...).
2016-03-08 10:35:55 +00:00
* [Emojiawesome - Twemoji for Yellow ](https://github.com/datenstrom/yellow-plugins/tree/master/emojiawesome ) by [@datenstrom ](https://github.com/datenstrom/ ): Use Twemoji in Yellow CMS.
2016-07-03 14:21:35 +00:00
* [EmojiPanel for Twitter ](https://github.com/danbovey/EmojiPanel ) by [@danielbovey ](https://twitter.com/danielbovey/status/749580050274582528 ): Insert Twemoji into your tweets on twitter.com.
2016-07-15 22:53:00 +00:00
* [Twitter Color Emoji font ](https://github.com/eosrei/twemoji-color-font ) by [@bderickson ](https://twitter.com/bderickson ): Use Twemoji as your system default font on Linux & OS X.
2016-12-09 18:15:13 +00:00
* [Emojica ](https://github.com/xoudini/emojica ) by [@xoudini ](https://twitter.com/xoudini ): An iOS framework allowing you to replace all standard emoji in strings with Twemoji.
2014-11-11 03:50:52 +00:00
2014-11-06 22:32:19 +00:00
## Committers and Contributors
2016-07-13 17:56:05 +00:00
* Bryan Haggerty (Twitter)
2018-02-13 22:10:41 +00:00
* Nathan Downs (Twitter)
2017-05-23 21:10:35 +00:00
* Tom Wuttke (ex-Twitter)
2016-07-13 17:56:05 +00:00
* Andrea Giammarchi (ex-Twitter)
2014-11-06 22:32:19 +00:00
* Joen Asmussen (WordPress)
* Marcus Kazmierczak (WordPress)
The goal of this project is to simply provide emoji for everyone. We definitely welcome improvements and fixes, but we may not merge every pull request suggested by the community due to the simple nature of the project.
2016-10-08 18:25:07 +00:00
The rules for contributing are available in the `CONTRIBUTING.md` file.
2014-11-06 22:32:19 +00:00
Thank you to all of our [contributors ](https://github.com/twitter/twemoji/graphs/contributors ).
## License
2018-02-13 22:10:41 +00:00
Copyright 2018 Twitter, Inc and other contributors
2016-10-08 18:25:07 +00:00
2014-11-06 22:32:19 +00:00
Code licensed under the MIT License: http://opensource.org/licenses/MIT
2016-10-08 18:25:07 +00:00
Graphics licensed under CC-BY 4.0: https://creativecommons.org/licenses/by/4.0/