Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

twemoji

Package Overview
Dependencies
Maintainers
1
Versions
63
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

twemoji

A Unicode standard based way to implement emoji across all platforms.

  • 0.0.3
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
97K
increased by1.51%
Maintainers
1
Weekly downloads
 
Created
Source

Twitter Emoji (Twemoji)

The aim of this library is to provide a Unicode standard based way to implement emoji across all platforms.

API

Following all methods exposed through the twemoji namespace.

twemoji.parse( ... )

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

There are mainly two kind of parsing, string parsing, and DOM parsing.

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

Here a walk through all parsing possibilities.

string parsing

Given a generic string, it will replace all emoji with an <img> tag.

While this can be used to inject via innerHTML emoji image tags, please note that this method does not sanitize the string or prevent malicious code to be executed. As example, if the text contains a <script> tag, this will not be converted into &lt;script&gt; since it's out of this method 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://abs.twimg.com/emoji/v1/36x36/2764.png"> emoji!
*/
string parsing + callback

If a callback is passed, the src attribute will be the one returned by the same 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 variable.

string parsing + callback returning falsy

If the callback returns falsy values such 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

Differently from string parsing, if the first argument is a HTMLElement generated image tags will be replacing emoji that are inside #text node only without compromising surrounding nodes, listeners, and avoiding completely the usage of innerHTML.

If security is a major concern, this parsing can be considered the safest option but with a slightly penalized performance gap due DOM operations that are inevitably costy compared to basic strings.

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://abs.twimg.com/emoji/v1/36x36/2764.png
img.alt;        // \u2764\uFE0F
img.class;      // emoji
img.draggable;  // false

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

Object as parameter

Here the list of properties accepted by the optional object that could be passed to parse.

  {
    callback: Function,
    base: string,
    ext: string,
    size: string|number
  }
callback

The function to invoke in order to generate images src.

By default it is a function like the following one:

function imageSourceGenrator(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"
  );
}
base

The default url to be used, by default it's 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 to be used, by default it's the same as twemoji.ext which is ".png".

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

size

The default assets size to be used, by default it's the same as twemoji.size which is "36x36".

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

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

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':      // trademark
                return false;
        }
        return ''.concat(options.base, options.size, '/', icon, options.ext);
    }
});

Committers and Contributors

  • Andrea Giammarchi (Twitter)
  • Marcus Kazmierczak (WordPress)
  • Joen Asmussen (WordPress)

Thank you to all of our contributors.

License

Copyright 2014 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/

Keywords

FAQs

Package last updated on 05 Nov 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc