1
0
mirror of https://github.com/twitter/twemoji.git synced 2024-06-15 03:35:16 +00:00
Emoji for everyone. This is a mirror of the open-source Twemoji repository. https://twemoji.twitter.com/
Go to file
Bryan Haggerty 55443d4b39 Unicode 10.0/Emoji 5.0 additions + modifications to a subset of existing emojis
Adds new emojis based on the finalized [Emoji 5.0
spec](http://unicode.org/reports/tr51/index.html). Modifications to
some existing emojis included as well, see change list below for
specifics.

This change also moves the Twemoji version number to 2.3.

### New

* Star-Struck
* Face With Raised Eyebrow
* Exploding Head
* Crazy Face
* Face With Symbols Over Mouth
* Face Vomiting
* Shushing Face
* Face With Hand Over Mouth
* Face With Monocle
* Child
* Adult
* Older Adult
* Woman With Headscarf
* Bearded Person
* Breast-Feeding
* Mage
* Woman Mage
* Man Mage
* Fairy
* Woman Fairy
* Man Fairy
* Vampire
* Woman Vampire
* Man Vampire
* Merperson
* Mermaid
* Merman
* Elf
* Woman Elf
* Man Elf
* Genie
* Woman Genie
* Man Genie
* Zombie
* Woman Zombie
* Man Zombie
* Person in Steamy Room
* Woman in Steamy Room
* Man in Steamy Room
* Person Climbing
* Woman Climbing
* Man Climbing
* Person in Lotus Position
* Woman in Lotus Position
* Man in Lotus Position
* Love-You Gesture
* Palms Up Together
* Brain
* Orange Heart
* Scarf
* Gloves
* Coat
* Socks
* Billed Cap
* Zebra
* Giraffe
* Hedgehog
* Sauropod
* T-Rex
* Cricket
* Coconut
* Broccoli
* Pretzel
* Cut of Meat
* Sandwich
* Bowl With Spoon
* Canned Food
* Dumpling
* Fortune Cookie
* Takeout Box
* Pie
* Cup With Straw
* Chopsticks
* Flying Saucer
* Sled
* Curling Stone
* Flag for England
* Flag for Scotland
* Flag for Wales

### Modified

* Index Pointing Up
* Person Facepalming
* Man in Business Suit Levitating
* Person Getting Massage
* Hamburger
* Ogre
* Full Moon With Face
* New Moon Face
* First Quarter Moon With Face
* Last Quarter Moon With Face
* Japanese "vacancy" button
2017-05-23 14:10:35 -07:00
1 used original base folder 2016-03-02 19:43:00 +00:00
2 Unicode 10.0/Emoji 5.0 additions + modifications to a subset of existing emojis 2017-05-23 14:10:35 -07:00
16x16 fixed wrong 1f567 2014-11-15 00:42:35 +00:00
36x36 fixed wrong 1f567 2014-11-15 00:42:35 +00:00
72x72 fixed wrong 1f567 2014-11-15 00:42:35 +00:00
assets Merge pull request #89 from prdolmos/use-consistent-keypad-ten-color 2015-06-30 15:16:30 +01:00
svg updating travis 2015-12-11 12:35:51 +00:00
.gitignore Add component.json 2014-11-08 01:31:51 +09:00
.npmignore Fix npmignore for including emoji source files 2017-02-10 17:58:07 +01:00
.travis.yml trying to migrate to containers 2015-12-11 12:42:27 +00:00
bower.json Don't ignore V2 files in bower.json (fixes #171) 2016-09-05 16:49:59 +02:00
component.json added SELECT to the list of ignored nodes 2015-03-13 10:26:31 +00:00
CONTRIBUTING.md Initial open source release 2014-11-06 16:32:19 -06:00
FOLDERS.md Create FOLDERS.md 2017-03-30 10:56:39 +02:00
index.html Unicode 10.0/Emoji 5.0 additions + modifications to a subset of existing emojis 2017-05-23 14:10:35 -07:00
LICENSE Initial open source release 2014-11-06 16:32:19 -06:00
LICENSE-GRAPHICS Initial open source release 2014-11-06 16:32:19 -06:00
package.json Unicode 10.0/Emoji 5.0 additions + modifications to a subset of existing emojis 2017-05-23 14:10:35 -07:00
preview-generator.js fixed SVG smiles face alignment 2015-04-09 11:57:30 +01:00
preview-svg.html added benchmark in version 1 too 2016-03-02 11:00:58 +00:00
preview-template.html added benchmark in version 1 too 2016-03-02 11:00:58 +00:00
preview.html added benchmark in version 1 too 2016-03-02 11:00:58 +00:00
README.md Unicode 10.0/Emoji 5.0 additions + modifications to a subset of existing emojis 2017-05-23 14:10:35 -07:00
svg.sh removed duplicated viewBox problem with more updated Inkscape 2015-04-09 10:18:05 +01:00
test.html Initial open source release 2014-11-06 16:32:19 -06:00
test.js added onerror option and test 2015-11-17 11:12:53 +00:00
testrunner.js hopefully fixed travis 2016-02-28 18:22:34 +00:00
twemoji-dist.js avoid errors when required via node and no location is present 2014-11-07 10:02:24 +00:00
twemoji-generator.js Update twemoji-generator.js for Emoji 5.0 2017-04-05 22:53:25 +02:00
twemoji.amd.js added onerror option and test 2015-11-17 11:12:53 +00:00
twemoji.js added onerror option and test 2015-11-17 11:12:53 +00:00
twemoji.min.js added onerror option and test 2015-11-17 11:12:53 +00:00
twemoji.npm.js added onerror option and test 2015-11-17 11:12:53 +00:00

Twitter Emoji (Twemoji) Build Status

A simple library that provides standard Unicode emoji support across all platforms.

Twemoji v2.3 adheres to the Unicode 10.0 draft spec and supports the Emoji 5.0 draft spec

The Twemoji library offers support for 2,661 emojis, including skin tone and gender modifiers.

CDN Support

The folks over at MaxCDN have graciously provided CDN support.

Use the following in the <head> tag of your HTML document(s):

<script src="//twemoji.maxcdn.com/2/twemoji.min.js?2.3.0"></script>

Breaking changes in V2

TL;DR: there's no variant anymore, all callbacks receive the transformed iconId and in some cases the rawText too.

There are a few potentially breaking changes in twemoji version 2:

  • the parse invoked function signature is now (iconId, options) instead of (icon, options, variant)
  • 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
  • the default PNG icon size is 72 pixels and there are no other PNG assets for 16 or 32.
  • in order to access latest assets you need to specify folder 2/72x72 or 2/svg.

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.

API

Following are all the methods exposed in the twemoji namespace.

twemoji.parse( ... ) V1

This is the main parsing utility and has 3 overloads per parsing type.

There are mainly two kinds of parsing: string parsing and DOM parsing.

Each of them accepts a callback to generate an image source or an options object with parsing info.

Here is a walkthrough of all parsing possibilities:

string parsing (V1)

Given a generic string, replaces all emoji with an <img> tag.

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 &lt;script&gt; since it's out of this method's scope to prevent these kind of attacks.

However, for already sanitized strings, this method can be considered safe enough. Please see DOM parsing if security is one of your major concerns.

twemoji.parse('I \u2764\uFE0F emoji!');

// will produce
/*
I <img
  class="emoji"
  draggable="false"
  alt="❤️"
  src="https://twemoji.maxcdn.com/36x36/2764.png"> emoji!
*/

string parsing + callback

If a callback is passed, the value of the src attribute will be the value returned by the callback.

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="❤️"
  src="/assets/36x36/2764.gif"> emoji!
*/

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.

string parsing + callback returning falsy

If the callback returns "falsy values" such as null, undefined, 0, false, or an empty string, nothing will change for that specific emoji.

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="❤️"
  src="/assets/2764.png">ur
*/

string parsing + object

In case an object is passed as second parameter, the passed options object will reflect its properties.

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="❤️"
  src="/assets/128x128/2764.gif"> emoji!
*/
DOM parsing

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.

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.

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

img.src;        // https://twemoji.maxcdn.com/36x36/2764.png
img.alt;        // \u2764\uFE0F
img.className;  // emoji
img.draggable;  // false

All other overloads described for string are available in exactly the same way for DOM parsing.

Object as parameter

Here's the list of properties accepted by the optional object that can be passed to the parse function.

  {
    callback: Function,   // default the common replacer
    attributes: Function, // default returns {}
    base: string,         // default MaxCDN
    ext: string,          // default ".png"
    className: string,    // default "emoji"
    size: string|number,  // default "36x36"
    folder: string        // in case it's specified
                          // it replaces .size info, if any
  }
callback

The function to invoke in order to generate image src(s).

By default it is a function like the following one:

function imageSourceGenerator(icon, options) {
  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"
  );
}
attributes (V1)

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:

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.

base

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.

ext

The default image extension is the same as twemoji.ext which is ".png".

If you modify the former, it will reflect as default for all parsed strings or nodes.

className

The default class for each generated image is emoji. It is possible to specify a different one through this property.

size

The default asset size is the same as twemoji.size which is "36x36".

If you modify the former, it will reflect as default for all parsed strings or nodes.

folder

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.

twemoji.parse(genericNode, {
  folder: 'svg',
  ext: '.svg'
});

This will generate urls such https://twemoji.maxcdn.com/svg/2764.svg instead of using a specific size based image.

Utilities

Basic utilities / helpers to convert code points to JavaScript surrogates and vice versa.

twemoji.convert.fromCodePoint()

For a given HEX codepoint, returns UTF-16 surrogate pairs.

twemoji.convert.fromCodePoint('1f1e8');
 // "\ud83c\udde8"

twemoji.convert.toCodePoint()

For given UTF-16 surrogate pairs, returns the equivalent HEX codepoint.

 twemoji.convert.toCodePoint('\ud83c\udde8\ud83c\uddf3');
 // "1f1e8-1f1f3"

 twemoji.convert.toCodePoint('\ud83c\udde8\ud83c\uddf3', '~');
 // "1f1e8~1f1f3"

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

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>

<meta charset="utf-8">

Exclude Characters (V1)

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:

twemoji.parse(document.body, {
    callback: function(icon, options, variant) {
        switch ( icon ) {
            case 'a9':      // © copyright
            case 'ae':      // ® registered trademark
            case '2122':    // ™ trademark
                return false;
        }
        return ''.concat(options.base, options.size, '/', icon, options.ext);
    }
});

Build

If you'd like to test and/or contribute please follow these instructions.

# 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

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.

Attribution Requirements

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.

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.

Community Projects

Committers and Contributors

  • Bryan Haggerty (Twitter)
  • Tom Wuttke (ex-Twitter)
  • Andrea Giammarchi (ex-Twitter)
  • 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.

The rules for contributing are available in the CONTRIBUTING.md file.

Thank you to all of our contributors.

License

Copyright 2017 Twitter, Inc and other contributors

Code licensed under the MIT License: http://opensource.org/licenses/MIT

Graphics licensed under CC-BY 4.0: https://creativecommons.org/licenses/by/4.0/