### Setup Development Environment Source: https://github.com/twitter/twemoji/blob/master/CONTRIBUTING.md Clone the repository, install dependencies, and build the project to set up your local development environment. ```bash git clone -b master https://github.com/$YOUR_USERNAME/twemoji.git/ cd twemoji yarn install yarn build yarn test ``` -------------------------------- ### Twemoji Parse with Folder and Extension Source: https://github.com/twitter/twemoji/blob/master/LEGACY.md Example of using the 'folder' and 'ext' options to generate URLs for SVG emoji. This replaces the default size-based URL generation. ```javascript twemoji.parse(genericNode, { folder: 'svg', ext: '.svg' }); ``` -------------------------------- ### DOM Parsing Example Source: https://github.com/twitter/twemoji/blob/master/README.md Demonstrates how to use `twemoji.parse()` with a DOM element. This method replaces emoji within text nodes, preserving surrounding HTML and event listeners. It's a safe option for replacing emoji directly in the document. ```js var div = document.createElement('div'); div.textContent = 'I 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/v/latest/72x72/2764.png img.alt; // img.className; // emoji img.draggable; // false ``` -------------------------------- ### twemoji Test Runner Initialization Source: https://github.com/twitter/twemoji/blob/master/src/test/index.html Initializes the twemoji test runner framework. This code sets up global variables and functions for managing tests, assertions, and reporting. It also includes a timer for visual feedback during asynchronous operations. ```javascript /*\n *! (C) Andrea Giammarchi, @WebReflection - Mit Style License \n */ (function(wru){ window.wru = wru; }(function(Y){function j(){A=K.call(m);if(A){if(typeof A=="function"){A={name:A\[S\]||"anonymous",test:A}}(P=l(l(Z.node,"div"),"span"))\[E\]=((ag(A,S)&&A\[S\])||(ag(A,e)&&A\[e\])||Q)+i+i;a=\[\];u=\[\];T=\[\];ab={};b("setup");T\[ah\]||b("test");N||r()}else{t()}}function p(aj){try{return O.call(h,aj)}catch(ai){return h.createElement(aj)}}function l(ai,aj){return ai.appendChild(p(aj))}function g(ai){P\[E\]=x.call(P\[E\],0,-2)+i+ai}function t(){var ak=Z.node.insertBefore(p("div"),Z.node.firstChild),al,aj,ai;if(ad){ai=aj="error";al="There Are Errors: "+ad}else{if(C){ai=aj="fail";al=C+" Tests Failed"}else{ai=aj="pass";al="Passed "+s+" Tests"}}Z.status=ai;ak\[E\]=""+al+"";ak.className=aj}function G(){var ai=this.lastChild.style;ai.display=ai.display=="none"?"block":"none"}function c(ai){P\[E\]+="
emoji!
*/
```
--------------------------------
### twemoji.convert.toCodePoint()
Source: https://github.com/twitter/twemoji/blob/master/LEGACY.md
Converts UTF-16 surrogate pairs into their equivalent HEX codepoint.
```APIDOC
## twemoji.convert.toCodePoint()
### Description
For given UTF-16 surrogate pairs, returns the equivalent HEX codepoint.
### Parameters
#### Path Parameters
- **surrogatePairs** (string) - Required - The UTF-16 surrogate pairs to convert.
- **separator** (string) - Optional - A separator to use between codepoints if multiple are provided.
### Request Example
```javascript
twemoji.convert.toCodePoint('\ud83c\udde8\ud83c\uddf3');
twemoji.convert.toCodePoint('\ud83c\udde8\ud83c\uddf3', '~');
```
### Response
#### Success Response (string)
- **codepoint** (string) - The equivalent HEX codepoint(s).
### Response Example
```json
"1f1e8-1f1f3"
"1f1e8~1f1f3"
```
```
--------------------------------
### twemoji.parse() - String Parsing with Options Object
Source: https://github.com/twitter/twemoji/blob/master/LEGACY.md
Parses a string and allows customization through an options object, which can include a callback function and size parameters.
```APIDOC
## twemoji.parse(string, options)
### Description
Parses a string and applies options for customization. The options object can contain a `callback` function and a `size` property to control the generated `
emoji!
```
```
--------------------------------
### twemoji.convert.fromCodePoint()
Source: https://github.com/twitter/twemoji/blob/master/LEGACY.md
Converts a given HEX codepoint into its corresponding UTF-16 surrogate pairs.
```APIDOC
## twemoji.convert.fromCodePoint()
### Description
For a given HEX codepoint, returns UTF-16 surrogate pairs.
### Parameters
#### Path Parameters
- **codepoint** (string) - Required - The HEX codepoint to convert.
### Request Example
```javascript
twemoji.convert.fromCodePoint('1f1e8');
```
### Response
#### Success Response (string)
- **surrogatePairs** (string) - The UTF-16 surrogate pairs.
### Response Example
```json
"\ud83c\udde8"
```
```
--------------------------------
### twemoji.parse() - String Parsing with Callback
Source: https://github.com/twitter/twemoji/blob/master/LEGACY.md
Parses a string and uses a callback function to determine the `src` attribute for the generated `
emoji!
```
```
--------------------------------
### Include Twemoji via CDN (Explicit Version)
Source: https://github.com/twitter/twemoji/blob/master/README.md
Include a specific version of the Twemoji library by using this script tag, which points to version 14.0.3.
```html
```
--------------------------------
### String Parsing with Custom Callback
Source: https://github.com/twitter/twemoji/blob/master/LEGACY.md
Allows custom image source generation for emoji by providing a callback function. The callback receives icon, options, and variant.
```javascript
twemoji.parse(
'I
emoji!',
function(icon, options, variant) {
return '/assets/' + options.size + '/' + icon + '.gif';
}
);
// will produce
/*
I
emoji!
*/
```
--------------------------------
### Custom Image Source Generator Function
Source: https://github.com/twitter/twemoji/blob/master/LEGACY.md
The callback function to invoke to generate image src attributes. It receives the icon code point and options, and should return a formatted URL string.
```javascript
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"
);
}
```
--------------------------------
### Default Image Source Generator Callback
Source: https://github.com/twitter/twemoji/blob/master/README.md
Shows the default function used by `twemoji.parse()` to generate the `src` attribute for emoji images. It constructs the URL using the provided options like base CDN, size, icon code point, and extension.
```js
function imageSourceGenerator(icon, options) {
return ''.concat(
options.base, // by default Twitter Inc. CDN
options.size, // by default "72x72" string
'/',
icon, // the found emoji as code point
options.ext // by default ".png"
);
}
```
--------------------------------
### Parse Body Content with Twemoji
Source: https://github.com/twitter/twemoji/blob/master/src/test/base.html
This snippet demonstrates how to parse the entire document body for emojis and render them using Twemoji. It's typically used after the page has loaded.
```javascript
this.onload = function () { twemoji.parse(document.body, { folder: 'svg', ext: '.svg' }); };
```
--------------------------------
### Twemoji Parse Options Object
Source: https://github.com/twitter/twemoji/blob/master/LEGACY.md
This object defines the properties accepted by the optional parameter of the twemoji.parse function. These properties allow customization of how emoji are rendered, including image sources, attributes, base URL, file extensions, class names, size, and folder.
```javascript
{
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
}
```
--------------------------------
### Convert Code Point to Surrogate Pairs
Source: https://github.com/twitter/twemoji/blob/master/LEGACY.md
Use this function to convert a HEX codepoint into its corresponding UTF-16 surrogate pairs. The output is a string representing the surrogate pairs.
```javascript
twemoji.convert.fromCodePoint('1f1e8');
// "\ud83c\udde8"
```
--------------------------------
### Copy Emoji to Clipboard on Click
Source: https://github.com/twitter/twemoji/blob/master/src/templates/preview.html
This snippet adds click event listeners to all emoji images within the document. When an emoji image is clicked, it prompts the user to copy the emoji's alt text (the emoji itself) to the clipboard using a keyboard shortcut (Ctrl+C or Command+C).
```javascript
(function (img, metaKey, i) { function copyToClipboard(e) { prompt('Copy to clipboard via ' + metaKey + '+C and Enter', this.alt); } for (i = 0; i < img.length; img[i++].onclick = copyToClipboard) {} }( document.getElementsByTagName('img'), /\b(?:Mac |i)OS\b/i.test(navigator.userAgent) ? 'Command' : 'Ctrl' ));
```
--------------------------------
### CSS for Inline Emoji Styling
Source: https://github.com/twitter/twemoji/blob/master/LEGACY.md
Apply this CSS to make emoji size dynamically with surrounding text. It ensures proper scaling, spacing, and vertical alignment.
```css
img.emoji {
height: 1em;
width: 1em;
margin: 0 .05em 0 .1em;
vertical-align: -0.1em;
}
```
--------------------------------
### Basic CSS for Emoji Display
Source: https://github.com/twitter/twemoji/blob/master/src/test/base.html
This CSS provides basic styling for displaying emojis inline. It ensures emojis appear correctly sized and without default browser decorations.
```css
img.emoji { display: inline !important; border: none !important; box-shadow: none !important; height: 1em !important; width: 1em !important; margin: 0 .07em !important; vertical-align: -0.1em !important; background: none !important; padding: 0 !important; }
```
--------------------------------
### Add Click Handler to Copy Alt Text
Source: https://github.com/twitter/twemoji/blob/master/src/test/preview-svg.html
Adds a click event listener to all generated emoji images. When clicked, it prompts the user to copy the emoji's alt text using a keyboard shortcut (Command/Ctrl + C).
```javascript
(function (img, metaKey, i) {
function copyToClipboard(e) {
prompt('Copy to clipboard via ' + metaKey + '+C and Enter', this.alt);
}
for (i = 0; i < img.length; img[i++].onclick = copyToClipboard) {}
}( document.getElementsByTagName('img'), /\b(?:Mac |i)OS\b/i.test(navigator.userAgent) ? 'Command' : 'Ctrl' ));
```
--------------------------------
### Twitter Widgets Script
Source: https://github.com/twitter/twemoji/blob/master/index.html
This script is used to load Twitter's widgets, such as the tweet button. It should be placed before the closing `` tag.
```javascript
!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');
```
--------------------------------
### twemoji.parse() - DOM Parsing
Source: https://github.com/twitter/twemoji/blob/master/README.md
The main parsing utility for replacing emoji within DOM elements. It targets `#text` nodes specifically, ensuring surrounding nodes and listeners are unaffected. This method is recommended for its safety and efficiency, though it incurs a slight performance cost due to DOM operations.
```APIDOC
## twemoji.parse(node, options)
### Description
Parses emoji within a given DOM node, replacing them with image tags. It specifically targets text nodes within the provided element.
### Method
`twemoji.parse()`
### Parameters
#### Path Parameters
None
#### Query Parameters
None
#### Request Body
None
### Parameters (Optional Object)
An optional object can be passed as the second argument to `twemoji.parse` to customize parsing behavior:
- **callback** (Function): The function to invoke to generate image sources. Defaults to a common replacer function.
- **attributes** (Function): A function that returns an object of attributes for the generated image tags. Defaults to returning an empty object.
- **base** (string): The base URL for emoji images. Defaults to the MaxCDN URL.
- **ext** (string): The file extension for emoji images. Defaults to ".png".
- **className** (string): The CSS class name for generated image tags. Defaults to "emoji".
- **size** (string|number): The size of the emoji images. Defaults to "72x72". Can be overridden by the `folder` property.
- **folder** (string): Specifies a folder for the emoji images, overriding the `size` property. Useful for formats like SVG.
### Request Example
```js
var div = document.createElement('div');
div.textContent = 'I \u2764\uFE0F emoji!';
document.body.appendChild(div);
twemoji.parse(document.body);
// Example with options:
twemoji.parse(document.body, {
folder: 'svg',
ext: '.svg'
});
```
### Response
#### Success Response
Emoji characters within the specified DOM node are replaced with `
```
### Error Handling
This method does not explicitly define error handling for invalid input or parsing failures.
```
--------------------------------
### Convert Surrogate Pairs to Code Point
Source: https://github.com/twitter/twemoji/blob/master/LEGACY.md
Converts UTF-16 surrogate pairs into their equivalent HEX codepoint. You can specify a custom separator character.
```javascript
twemoji.convert.toCodePoint('\ud83c\udde8\ud83c\uddf3');
// "1f1e8-1f1f3"
```
```javascript
twemoji.convert.toCodePoint('\ud83c\udde8\ud83c\uddf3', '~');
// "1f1e8~1f1f3"
```
--------------------------------
### Custom Attributes Callback Function
Source: https://github.com/twitter/twemoji/blob/master/LEGACY.md
The function to invoke to generate custom attributes for the image tag. It receives the icon code point and variant, and should return an object of attributes.
```javascript
function attributesCallback(icon, variant) {
return {
title: 'Emoji: ' + icon + variant
};
}
```
--------------------------------
### Set UTF-8 Character Set Meta Tag
Source: https://github.com/twitter/twemoji/blob/master/LEGACY.md
Ensure proper emoji support by setting the document's character set to UTF-8. Include this meta tag in the document's head.
```html
```
--------------------------------
### Parse Twemoji and Measure Performance
Source: https://github.com/twitter/twemoji/blob/master/src/templates/preview.html
This snippet parses emoji from an unordered list and measures the time taken for the parsing operation. It then displays the total count of emoji and the elapsed time.
```javascript
var ul = document.getElementsByTagName('ul')[0]; var total = ul.getElementsByTagName('li').length; var elapsed = +new Date; twemoji.parse(ul, {{emoji-options}}); elapsed = (+new Date) - elapsed; document.body.insertBefore( document.createTextNode(total + ' emoji parsed in ' + elapsed + 'ms'), document.body.firstChild );
```
--------------------------------
### Parse and Render Emoji with Twemoji
Source: https://github.com/twitter/twemoji/blob/master/index.html
Use this JavaScript snippet to parse and render emoji within the specified DOM element. Ensure the twemoji library is loaded before executing this code.
```javascript
twemoji.parse(document.body);
```
--------------------------------
### CSS for Emoji Container
Source: https://github.com/twitter/twemoji/blob/master/src/test/base.html
This CSS class is used to style a container for emojis, setting the font size for the emojis within it.
```css
.emojis { margin-bottom: 20px; font-size: 32px; }
```
--------------------------------
### twemoji.parse() - String Parsing
Source: https://github.com/twitter/twemoji/blob/master/LEGACY.md
Parses a given string, replacing all emoji with an
emoji!
```
```
--------------------------------
### twemoji.parse() with callback
Source: https://github.com/twitter/twemoji/blob/master/LEGACY.md
Parses and replaces emoji in the given scope, with an option to exclude specific characters via a callback function.
```APIDOC
## twemoji.parse() with callback
### Description
To exclude certain characters from being replaced by twemoji.js, call twemoji.parse() with a callback, returning false for the specific unicode icon.
### Parameters
#### Path Parameters
- **scope** (Node) - Required - The DOM node to parse.
- **options** (object) - Optional - Configuration options for parsing.
- **callback** (function) - Optional - A callback function to determine whether to replace an icon.
### Request Example
```javascript
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);
}
});
```
```
--------------------------------
### String Parsing with Falsy Callback Return
Source: https://github.com/twitter/twemoji/blob/master/LEGACY.md
If the callback returns a falsy value (e.g., `null`, `undefined`, `false`), the specific emoji will not be replaced.
```javascript
var i = 0;
twemoji.parse(
'emoji, m
ur',
function(icon, options, variant) {
if (i++ === 0) {
return; // no changes made first call
}
return '/assets/' + icon + options.ext;
}
);
// will produce
/*
emoji, m❤️n am
ur
*/
```
--------------------------------
### twemoji.parse() - DOM Parsing
Source: https://github.com/twitter/twemoji/blob/master/LEGACY.md
Parses emoji within an HTMLElement, replacing them with `
emoji!
emoji!
*/
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.