2015-03-11 18:49:28 -04:00
|
|
|
/*jslint indent: 2, browser: true, bitwise: true, plusplus: true */
|
2015-03-12 18:00:28 -04:00
|
|
|
var twemoji = (function (
|
2015-03-11 18:49:28 -04:00
|
|
|
/*! Copyright Twitter Inc. and other contributors. Licensed under MIT *//*
|
Emoji: Replace `twitter/twemoji` with `jdecked/twemoji`.
After a chaotic change of ownership, the `twitter/twemoji` library is now considered abandoned.
After waiting for this moment to arise, a fork was created by several former employees who had maintained the library which lives at `jdecked/twemoji` on GitHub.
This switches out where the underlying source code comes from for the library, and applies the 15.0.3 update, which adheres to the Unicode 15 spec and adds support for all Emoji introduced in Emoji 15.0.
This does not update the underlying `precommit:emoji` Grunt script responsible for updating `formatting.php`. After GitHub recently sunset support for SVN, the current process needs to be replaced with a new one. This will be handled in #60520.
Let the masses rejoice for the 🐦⬛ singing in the dead of night, secure 🛜, aromatic 🫚, and some very silly 🪿🪿.
RIP Twemoji. Long live Twemoji!
Props peterwilsoncc, kraftbj, jeffpaul, azaozz, dd32, hareesh-pillai, jorbin.
Fixes #57600.
Built from https://develop.svn.wordpress.org/trunk@57626
git-svn-id: http://core.svn.wordpress.org/trunk@57127 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-02-13 09:36:07 -05:00
|
|
|
https://github.com/jdecked/twemoji/blob/gh-pages/LICENSE
|
2015-03-11 18:49:28 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
// WARNING: this file is generated automatically via
|
2020-06-15 14:04:26 -04:00
|
|
|
// `node scripts/build.js`
|
2015-03-11 18:49:28 -04:00
|
|
|
// please update its `createTwemoji` function
|
|
|
|
// at the bottom of the same file instead.
|
|
|
|
|
|
|
|
) {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/*jshint maxparams:4 */
|
|
|
|
|
|
|
|
var
|
|
|
|
// the exported module object
|
|
|
|
twemoji = {
|
|
|
|
|
|
|
|
|
|
|
|
/////////////////////////
|
|
|
|
// properties //
|
|
|
|
/////////////////////////
|
|
|
|
|
Emoji: Replace `twitter/twemoji` with `jdecked/twemoji`.
After a chaotic change of ownership, the `twitter/twemoji` library is now considered abandoned.
After waiting for this moment to arise, a fork was created by several former employees who had maintained the library which lives at `jdecked/twemoji` on GitHub.
This switches out where the underlying source code comes from for the library, and applies the 15.0.3 update, which adheres to the Unicode 15 spec and adds support for all Emoji introduced in Emoji 15.0.
This does not update the underlying `precommit:emoji` Grunt script responsible for updating `formatting.php`. After GitHub recently sunset support for SVN, the current process needs to be replaced with a new one. This will be handled in #60520.
Let the masses rejoice for the 🐦⬛ singing in the dead of night, secure 🛜, aromatic 🫚, and some very silly 🪿🪿.
RIP Twemoji. Long live Twemoji!
Props peterwilsoncc, kraftbj, jeffpaul, azaozz, dd32, hareesh-pillai, jorbin.
Fixes #57600.
Built from https://develop.svn.wordpress.org/trunk@57626
git-svn-id: http://core.svn.wordpress.org/trunk@57127 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-02-13 09:36:07 -05:00
|
|
|
// default assets url, by default will be jsDelivr CDN
|
|
|
|
base: 'https://cdn.jsdelivr.net/gh/jdecked/twemoji@15.0.3/assets/',
|
2015-03-11 18:49:28 -04:00
|
|
|
|
|
|
|
// default assets file extensions, by default '.png'
|
|
|
|
ext: '.png',
|
|
|
|
|
2016-03-10 00:37:27 -05:00
|
|
|
// default assets/folder size, by default "72x72"
|
|
|
|
// available via Twitter CDN: 72
|
|
|
|
size: '72x72',
|
2015-03-11 18:49:28 -04:00
|
|
|
|
|
|
|
// default class name, by default 'emoji'
|
|
|
|
className: 'emoji',
|
|
|
|
|
|
|
|
// basic utilities / helpers to convert code points
|
|
|
|
// to JavaScript surrogates and vice versa
|
|
|
|
convert: {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given an HEX codepoint, returns UTF16 surrogate pairs.
|
|
|
|
*
|
|
|
|
* @param string generic codepoint, i.e. '1F4A9'
|
|
|
|
* @return string codepoint transformed into utf16 surrogates pair,
|
|
|
|
* i.e. \uD83D\uDCA9
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* twemoji.convert.fromCodePoint('1f1e8');
|
|
|
|
* // "\ud83c\udde8"
|
|
|
|
*
|
|
|
|
* '1f1e8-1f1f3'.split('-').map(twemoji.convert.fromCodePoint).join('')
|
|
|
|
* // "\ud83c\udde8\ud83c\uddf3"
|
|
|
|
*/
|
|
|
|
fromCodePoint: fromCodePoint,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given UTF16 surrogate pairs, returns the equivalent HEX codepoint.
|
|
|
|
*
|
|
|
|
* @param string generic utf16 surrogates pair, i.e. \uD83D\uDCA9
|
|
|
|
* @param string optional separator for double code points, default='-'
|
|
|
|
* @return string utf16 transformed into codepoint, i.e. '1F4A9'
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* twemoji.convert.toCodePoint('\ud83c\udde8\ud83c\uddf3');
|
|
|
|
* // "1f1e8-1f1f3"
|
|
|
|
*
|
|
|
|
* twemoji.convert.toCodePoint('\ud83c\udde8\ud83c\uddf3', '~');
|
|
|
|
* // "1f1e8~1f1f3"
|
|
|
|
*/
|
|
|
|
toCodePoint: toCodePoint
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
/////////////////////////
|
|
|
|
// methods //
|
|
|
|
/////////////////////////
|
|
|
|
|
|
|
|
/**
|
|
|
|
* User first: used to remove missing images
|
|
|
|
* preserving the original text intent when
|
|
|
|
* a fallback for network problems is desired.
|
|
|
|
* Automatically added to Image nodes via DOM
|
|
|
|
* It could be recycled for string operations via:
|
|
|
|
* $('img.emoji').on('error', twemoji.onerror)
|
|
|
|
*/
|
|
|
|
onerror: function onerror() {
|
|
|
|
if (this.parentNode) {
|
2017-08-13 22:04:45 -04:00
|
|
|
this.parentNode.replaceChild(createText(this.alt, false), this);
|
2015-03-11 18:49:28 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Main method/logic to generate either <img> tags or HTMLImage nodes.
|
|
|
|
* "emojify" a generic text or DOM Element.
|
|
|
|
*
|
|
|
|
* @overloads
|
|
|
|
*
|
|
|
|
* String replacement for `innerHTML` or server side operations
|
|
|
|
* twemoji.parse(string);
|
|
|
|
* twemoji.parse(string, Function);
|
|
|
|
* twemoji.parse(string, Object);
|
|
|
|
*
|
|
|
|
* HTMLElement tree parsing for safer operations over existing DOM
|
|
|
|
* twemoji.parse(HTMLElement);
|
|
|
|
* twemoji.parse(HTMLElement, Function);
|
|
|
|
* twemoji.parse(HTMLElement, Object);
|
|
|
|
*
|
|
|
|
* @param string|HTMLElement the source to parse and enrich with emoji.
|
|
|
|
*
|
|
|
|
* string replace emoji matches with <img> tags.
|
|
|
|
* Mainly used to inject emoji via `innerHTML`
|
|
|
|
* It does **not** parse the string or validate it,
|
|
|
|
* it simply replaces found emoji with a tag.
|
|
|
|
* NOTE: be sure this won't affect security.
|
|
|
|
*
|
|
|
|
* HTMLElement walk through the DOM tree and find emoji
|
|
|
|
* that are inside **text node only** (nodeType === 3)
|
|
|
|
* Mainly used to put emoji in already generated DOM
|
|
|
|
* without compromising surrounding nodes and
|
|
|
|
* **avoiding** the usage of `innerHTML`.
|
|
|
|
* NOTE: Using DOM elements instead of strings should
|
|
|
|
* improve security without compromising too much
|
|
|
|
* performance compared with a less safe `innerHTML`.
|
|
|
|
*
|
|
|
|
* @param Function|Object [optional]
|
|
|
|
* either the callback that will be invoked or an object
|
|
|
|
* with all properties to use per each found emoji.
|
|
|
|
*
|
|
|
|
* Function if specified, this will be invoked per each emoji
|
|
|
|
* that has been found through the RegExp except
|
|
|
|
* those follwed by the invariant \uFE0E ("as text").
|
|
|
|
* Once invoked, parameters will be:
|
|
|
|
*
|
2016-03-10 00:37:27 -05:00
|
|
|
* iconId:string the lower case HEX code point
|
2015-03-11 18:49:28 -04:00
|
|
|
* i.e. "1f4a9"
|
|
|
|
*
|
|
|
|
* options:Object all info for this parsing operation
|
|
|
|
*
|
|
|
|
* variant:char the optional \uFE0F ("as image")
|
|
|
|
* variant, in case this info
|
|
|
|
* is anyhow meaningful.
|
|
|
|
* By default this is ignored.
|
|
|
|
*
|
|
|
|
* If such callback will return a falsy value instead
|
|
|
|
* of a valid `src` to use for the image, nothing will
|
|
|
|
* actually change for that specific emoji.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Object if specified, an object containing the following properties
|
|
|
|
*
|
|
|
|
* callback Function the callback to invoke per each found emoji.
|
|
|
|
* base string the base url, by default twemoji.base
|
|
|
|
* ext string the image extension, by default twemoji.ext
|
|
|
|
* size string the assets size, by default twemoji.size
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* twemoji.parse("I \u2764\uFE0F emoji!");
|
2017-08-13 22:04:45 -04:00
|
|
|
* // I <img class="emoji" draggable="false" alt="❤️" src="/assets/2764.gif"/> emoji!
|
2015-03-11 18:49:28 -04:00
|
|
|
*
|
|
|
|
*
|
2016-03-10 00:37:27 -05:00
|
|
|
* twemoji.parse("I \u2764\uFE0F emoji!", function(iconId, options) {
|
|
|
|
* return '/assets/' + iconId + '.gif';
|
2015-03-11 18:49:28 -04:00
|
|
|
* });
|
2017-08-13 22:04:45 -04:00
|
|
|
* // I <img class="emoji" draggable="false" alt="❤️" src="/assets/2764.gif"/> emoji!
|
2015-03-11 18:49:28 -04:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* twemoji.parse("I \u2764\uFE0F emoji!", {
|
|
|
|
* size: 72,
|
2016-03-10 00:37:27 -05:00
|
|
|
* callback: function(iconId, options) {
|
|
|
|
* return '/assets/' + options.size + '/' + iconId + options.ext;
|
2015-03-11 18:49:28 -04:00
|
|
|
* }
|
|
|
|
* });
|
2017-08-13 22:04:45 -04:00
|
|
|
* // I <img class="emoji" draggable="false" alt="❤️" src="/assets/72x72/2764.png"/> emoji!
|
2015-03-11 18:49:28 -04:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
parse: parse,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a string, invokes the callback argument
|
|
|
|
* per each emoji found in such string.
|
|
|
|
* This is the most raw version used by
|
|
|
|
* the .parse(string) method itself.
|
|
|
|
*
|
|
|
|
* @param string generic string to parse
|
|
|
|
* @param Function a generic callback that will be
|
|
|
|
* invoked to replace the content.
|
2021-03-18 15:20:19 -04:00
|
|
|
* This callback will receive standard
|
2015-03-11 18:49:28 -04:00
|
|
|
* String.prototype.replace(str, callback)
|
|
|
|
* arguments such:
|
|
|
|
* callback(
|
2016-03-10 00:37:27 -05:00
|
|
|
* rawText, // the emoji match
|
2015-03-11 18:49:28 -04:00
|
|
|
* );
|
|
|
|
*
|
|
|
|
* and others commonly received via replace.
|
|
|
|
*/
|
|
|
|
replace: replace,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Simplify string tests against emoji.
|
|
|
|
*
|
|
|
|
* @param string some text that might contain emoji
|
|
|
|
* @return boolean true if any emoji was found, false otherwise.
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* if (twemoji.test(someContent)) {
|
|
|
|
* console.log("emoji All The Things!");
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
test: test
|
|
|
|
},
|
|
|
|
|
2015-06-26 18:01:25 -04:00
|
|
|
// used to escape HTML special chars in attributes
|
|
|
|
escaper = {
|
|
|
|
'&': '&',
|
|
|
|
'<': '<',
|
|
|
|
'>': '>',
|
|
|
|
"'": ''',
|
|
|
|
'"': '"'
|
|
|
|
},
|
|
|
|
|
2015-03-11 18:49:28 -04:00
|
|
|
// RegExp based on emoji's official Unicode standards
|
|
|
|
// http://www.unicode.org/Public/UNIDATA/EmojiSources.txt
|
Emoji: Replace `twitter/twemoji` with `jdecked/twemoji`.
After a chaotic change of ownership, the `twitter/twemoji` library is now considered abandoned.
After waiting for this moment to arise, a fork was created by several former employees who had maintained the library which lives at `jdecked/twemoji` on GitHub.
This switches out where the underlying source code comes from for the library, and applies the 15.0.3 update, which adheres to the Unicode 15 spec and adds support for all Emoji introduced in Emoji 15.0.
This does not update the underlying `precommit:emoji` Grunt script responsible for updating `formatting.php`. After GitHub recently sunset support for SVN, the current process needs to be replaced with a new one. This will be handled in #60520.
Let the masses rejoice for the 🐦⬛ singing in the dead of night, secure 🛜, aromatic 🫚, and some very silly 🪿🪿.
RIP Twemoji. Long live Twemoji!
Props peterwilsoncc, kraftbj, jeffpaul, azaozz, dd32, hareesh-pillai, jorbin.
Fixes #57600.
Built from https://develop.svn.wordpress.org/trunk@57626
git-svn-id: http://core.svn.wordpress.org/trunk@57127 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-02-13 09:36:07 -05:00
|
|
|
re = /(?:\ud83d\udc68\ud83c\udffb\u200d\u2764\ufe0f\u200d\ud83d\udc8b\u200d\ud83d\udc68\ud83c[\udffb-\udfff]|\ud83d\udc68\ud83c\udffc\u200d\u2764\ufe0f\u200d\ud83d\udc8b\u200d\ud83d\udc68\ud83c[\udffb-\udfff]|\ud83d\udc68\ud83c\udffd\u200d\u2764\ufe0f\u200d\ud83d\udc8b\u200d\ud83d\udc68\ud83c[\udffb-\udfff]|\ud83d\udc68\ud83c\udffe\u200d\u2764\ufe0f\u200d\ud83d\udc8b\u200d\ud83d\udc68\ud83c[\udffb-\udfff]|\ud83d\udc68\ud83c\udfff\u200d\u2764\ufe0f\u200d\ud83d\udc8b\u200d\ud83d\udc68\ud83c[\udffb-\udfff]|\ud83d\udc69\ud83c\udffb\u200d\u2764\ufe0f\u200d\ud83d\udc8b\u200d\ud83d\udc68\ud83c[\udffb-\udfff]|\ud83d\udc69\ud83c\udffb\u200d\u2764\ufe0f\u200d\ud83d\udc8b\u200d\ud83d\udc69\ud83c[\udffb-\udfff]|\ud83d\udc69\ud83c\udffc\u200d\u2764\ufe0f\u200d\ud83d\udc8b\u200d\ud83d\udc68\ud83c[\udffb-\udfff]|\ud83d\udc69\ud83c\udffc\u200d\u2764\ufe0f\u200d\ud83d\udc8b\u200d\ud83d\udc69\ud83c[\udffb-\udfff]|\ud83d\udc69\ud83c\udffd\u200d\u2764\ufe0f\u200d\ud83d\udc8b\u200d\ud83d\udc68\ud83c[\udffb-\udfff]|\ud83d\udc69\ud83c\udffd\u200d\u2764\ufe0f\u200d\ud83d\udc8b\u200d\ud83d\udc69\ud83c[\udffb-\udfff]|\ud83d\udc69\ud83c\udffe\u200d\u2764\ufe0f\u200d\ud83d\udc8b\u200d\ud83d\udc68\ud83c[\udffb-\udfff]|\ud83d\udc69\ud83c\udffe\u200d\u2764\ufe0f\u200d\ud83d\udc8b\u200d\ud83d\udc69\ud83c[\udffb-\udfff]|\ud83d\udc69\ud83c\udfff\u200d\u2764\ufe0f\u200d\ud83d\udc8b\u200d\ud83d\udc68\ud83c[\udffb-\udfff]|\ud83d\udc69\ud83c\udfff\u200d\u2764\ufe0f\u200d\ud83d\udc8b\u200d\ud83d\udc69\ud83c[\udffb-\udfff]|\ud83e\uddd1\ud83c\udffb\u200d\u2764\ufe0f\u200d\ud83d\udc8b\u200d\ud83e\uddd1\ud83c[\udffc-\udfff]|\ud83e\uddd1\ud83c\udffc\u200d\u2764\ufe0f\u200d\ud83d\udc8b\u200d\ud83e\uddd1\ud83c[\udffb\udffd-\udfff]|\ud83e\uddd1\ud83c\udffd\u200d\u2764\ufe0f\u200d\ud83d\udc8b\u200d\ud83e\uddd1\ud83c[\udffb\udffc\udffe\udfff]|\ud83e\uddd1\ud83c\udffe\u200d\u2764\ufe0f\u200d\ud83d\udc8b\u200d\ud83e\uddd1\ud83c[\udffb-\udffd\udfff]|\ud83e\uddd1\ud83c\udfff\u200d\u2764\ufe0f\u200d\ud83d\udc8b\u200d\ud83e\uddd1\ud83c[\udffb-\udffe]|\ud83d\udc68\ud83c\udffb\u200d\u2764\ufe0f\u200d\ud83d\udc68\ud83c[\udffb-\udfff]|\ud83d\udc68\ud83c\udffb\u200d\ud83e\udd1d\u200d\ud83d\udc68\ud83c[\udffc-\udfff]|\ud83d\udc68\ud83c\udffc\u200d\u2764\ufe0f\u200d\ud83d\udc68\ud83c[\udffb-\udfff]|\ud83d\udc68\ud83c\udffc\u200d\ud83e\udd1d\u200d\ud83d\udc68\ud83c[\udffb\udffd-\udfff]|\ud83d\udc68\ud83c\udffd\u200d\u2764\ufe0f\u200d\ud83d\udc68\ud83c[\udffb-\udfff]|\ud83d\udc68\ud83c\udffd\u200d\ud83e\udd1d\u200d\ud83d\udc68\ud83c[\udffb\udffc\udffe\udfff]|\ud83d\udc68\ud83c\udffe\u200d\u2764\ufe0f\u200d\ud83d\udc68\ud83c[\udffb-\udfff]|\ud83d\udc68\ud83c\udffe\u200d\ud83e\udd1d\u200d\ud83d\udc68\ud83c[\udffb-\udffd\udfff]|\ud83d\udc68\ud83c\udfff\u200d\u2764\ufe0f\u200d\ud83d\udc68\ud83c[\udffb-\udfff]|\ud83d\udc68\ud83c\udfff\u200d\ud83e\udd1d\u200d\ud83d\udc68\ud83c[\udffb-\udffe]|\ud83d\udc69\ud83c\udffb\u200d\u2764\ufe0f\u200d\ud83d\udc68\ud83c[\udffb-\udfff]|\ud83d\udc69\ud83c\udffb\u200d\u2764\ufe0f\u200d\ud83d\udc69\ud83c[\udffb-\udfff]|\ud83d\udc69\ud83c\udffb\u200d\ud83e\udd1d\u200d\ud83d\udc68\ud83c[\udffc-\udfff]|\ud83d\udc69\ud83c\udffb\u200d\ud83e\udd1d\u200d\ud83d\udc69\ud83c[\udffc-\udfff]|\ud83d\udc69\ud83c\udffc\u200d\u2764\ufe0f\u200d\ud83d\udc68\ud83c[\udffb-\udfff]|\ud83d\udc69\ud83c\udffc\u200d\u2764\ufe0f\u200d\ud83d\udc69\ud83c[\udffb-\udfff]|\ud83d\udc69\ud83c\udffc\u200d\ud83e\udd1d\u200d\ud83d\udc68\ud83c[\udffb\udffd-\udfff]|\ud83d\udc69\ud83c\udffc\u200d\ud83e\udd1d\u200d\ud83d\udc69\ud83c[\udffb\udffd-\udfff]|\ud83d\udc69\ud83c\udffd\u200d\u2764\ufe0f\u200d\ud83d\udc68\ud83c[\udffb-\udfff]|\ud83d\udc69\ud83c\udffd\u200d\u2764\ufe0f\u200d\ud83d\udc69\ud83c[\udffb-\udfff]|\ud83d\udc69\ud83c\udffd\u200d\ud83e\udd1d\u200d\ud83d\udc68\ud83c[\udffb\udffc\udffe\udfff]|\ud83d\udc69\ud83c\udffd\u200d\ud83e\udd1d\u200d\ud83d\udc69\ud83c[\udffb\udffc\udffe\udfff]|\ud83d\udc69\ud83c\udffe\u200d\u2764\ufe0f\u200d\ud83d\udc68\ud83c[\udffb-\udfff]|\ud83d\udc69\ud83c\udffe\u200d\u2764\ufe0f\u200d\ud83d\udc69\ud83c[\udffb-\udfff]|\ud83d\udc69\ud83c\udffe\u200d\ud8
|
2016-03-10 00:37:27 -05:00
|
|
|
|
|
|
|
// avoid runtime RegExp creation for not so smart,
|
|
|
|
// not JIT based, and old browsers / engines
|
|
|
|
UFE0Fg = /\uFE0F/g,
|
|
|
|
|
|
|
|
// avoid using a string literal like '\u200D' here because minifiers expand it inline
|
|
|
|
U200D = String.fromCharCode(0x200D),
|
2015-03-11 18:49:28 -04:00
|
|
|
|
2015-06-26 18:01:25 -04:00
|
|
|
// used to find HTML special chars in attributes
|
|
|
|
rescaper = /[&<>'"]/g,
|
|
|
|
|
2017-08-13 22:04:45 -04:00
|
|
|
// nodes with type 1 which should **not** be parsed
|
|
|
|
shouldntBeParsed = /^(?:iframe|noframes|noscript|script|select|style|textarea)$/,
|
2015-03-11 18:49:28 -04:00
|
|
|
|
|
|
|
// just a private shortcut
|
|
|
|
fromCharCode = String.fromCharCode;
|
|
|
|
|
|
|
|
return twemoji;
|
|
|
|
|
|
|
|
|
|
|
|
/////////////////////////
|
|
|
|
// private functions //
|
|
|
|
// declaration //
|
|
|
|
/////////////////////////
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shortcut to create text nodes
|
|
|
|
* @param string text used to create DOM text node
|
|
|
|
* @return Node a DOM node with that text
|
|
|
|
*/
|
2017-08-13 22:04:45 -04:00
|
|
|
function createText(text, clean) {
|
|
|
|
return document.createTextNode(clean ? text.replace(UFE0Fg, '') : text);
|
2015-03-11 18:49:28 -04:00
|
|
|
}
|
|
|
|
|
2015-06-26 18:01:25 -04:00
|
|
|
/**
|
|
|
|
* Utility function to escape html attribute text
|
|
|
|
* @param string text use in HTML attribute
|
|
|
|
* @return string text encoded to use in HTML attribute
|
|
|
|
*/
|
|
|
|
function escapeHTML(s) {
|
|
|
|
return s.replace(rescaper, replacer);
|
|
|
|
}
|
|
|
|
|
2015-03-11 18:49:28 -04:00
|
|
|
/**
|
|
|
|
* Default callback used to generate emoji src
|
|
|
|
* based on Twitter CDN
|
|
|
|
* @param string the emoji codepoint string
|
|
|
|
* @param string the default size to use, i.e. "36x36"
|
|
|
|
* @return string the image source to use
|
|
|
|
*/
|
|
|
|
function defaultImageSrcGenerator(icon, options) {
|
|
|
|
return ''.concat(options.base, options.size, '/', icon, options.ext);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a generic DOM nodeType 1, walk through all children
|
|
|
|
* and store every nodeType 3 (#text) found in the tree.
|
|
|
|
* @param Element a DOM Element with probably some text in it
|
|
|
|
* @param Array the list of previously discovered text nodes
|
|
|
|
* @return Array same list with new discovered nodes, if any
|
|
|
|
*/
|
|
|
|
function grabAllTextNodes(node, allText) {
|
|
|
|
var
|
|
|
|
childNodes = node.childNodes,
|
|
|
|
length = childNodes.length,
|
|
|
|
subnode,
|
|
|
|
nodeType;
|
|
|
|
while (length--) {
|
|
|
|
subnode = childNodes[length];
|
|
|
|
nodeType = subnode.nodeType;
|
|
|
|
// parse emoji only in text nodes
|
|
|
|
if (nodeType === 3) {
|
|
|
|
// collect them to process emoji later
|
|
|
|
allText.push(subnode);
|
|
|
|
}
|
2017-08-13 22:04:45 -04:00
|
|
|
// ignore all nodes that are not type 1, that are svg, or that
|
2015-03-11 18:49:28 -04:00
|
|
|
// should not be parsed as script, style, and others
|
2017-08-13 22:04:45 -04:00
|
|
|
else if (nodeType === 1 && !('ownerSVGElement' in subnode) &&
|
|
|
|
!shouldntBeParsed.test(subnode.nodeName.toLowerCase())) {
|
2023-02-01 19:53:25 -05:00
|
|
|
|
|
|
|
// WP start
|
|
|
|
// Use doNotParse() callback if set.
|
|
|
|
if ( twemoji.doNotParse && twemoji.doNotParse( subnode ) ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// WP end
|
|
|
|
|
2015-03-11 18:49:28 -04:00
|
|
|
grabAllTextNodes(subnode, allText);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return allText;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to both remove the possible variant
|
2016-03-10 00:37:27 -05:00
|
|
|
* and to convert utf16 into code points.
|
|
|
|
* If there is a zero-width-joiner (U+200D), leave the variants in.
|
|
|
|
* @param string the raw text of the emoji match
|
2017-05-25 03:18:45 -04:00
|
|
|
* @return string the code point
|
2015-03-11 18:49:28 -04:00
|
|
|
*/
|
2016-03-10 00:37:27 -05:00
|
|
|
function grabTheRightIcon(rawText) {
|
2015-03-11 18:49:28 -04:00
|
|
|
// if variant is present as \uFE0F
|
2016-03-10 00:37:27 -05:00
|
|
|
return toCodePoint(rawText.indexOf(U200D) < 0 ?
|
|
|
|
rawText.replace(UFE0Fg, '') :
|
|
|
|
rawText
|
2015-03-11 18:49:28 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DOM version of the same logic / parser:
|
|
|
|
* emojify all found sub-text nodes placing images node instead.
|
|
|
|
* @param Element generic DOM node with some text in some child node
|
|
|
|
* @param Object options containing info about how to parse
|
|
|
|
*
|
|
|
|
* .callback Function the callback to invoke per each found emoji.
|
|
|
|
* .base string the base url, by default twemoji.base
|
|
|
|
* .ext string the image extension, by default twemoji.ext
|
|
|
|
* .size string the assets size, by default twemoji.size
|
|
|
|
*
|
|
|
|
* @return Element same generic node with emoji in place, if any.
|
|
|
|
*/
|
|
|
|
function parseNode(node, options) {
|
|
|
|
var
|
|
|
|
allText = grabAllTextNodes(node, []),
|
|
|
|
length = allText.length,
|
2015-06-26 18:01:25 -04:00
|
|
|
attrib,
|
|
|
|
attrname,
|
2015-03-13 13:20:26 -04:00
|
|
|
modified,
|
2015-03-11 18:49:28 -04:00
|
|
|
fragment,
|
|
|
|
subnode,
|
|
|
|
text,
|
|
|
|
match,
|
|
|
|
i,
|
|
|
|
index,
|
|
|
|
img,
|
2016-03-10 00:37:27 -05:00
|
|
|
rawText,
|
|
|
|
iconId,
|
2015-06-26 18:01:25 -04:00
|
|
|
src;
|
2015-03-11 18:49:28 -04:00
|
|
|
while (length--) {
|
2015-03-13 13:20:26 -04:00
|
|
|
modified = false;
|
2015-03-11 18:49:28 -04:00
|
|
|
fragment = document.createDocumentFragment();
|
|
|
|
subnode = allText[length];
|
|
|
|
text = subnode.nodeValue;
|
|
|
|
i = 0;
|
|
|
|
while ((match = re.exec(text))) {
|
|
|
|
index = match.index;
|
|
|
|
if (index !== i) {
|
|
|
|
fragment.appendChild(
|
2017-08-13 22:04:45 -04:00
|
|
|
createText(text.slice(i, index), true)
|
2015-03-11 18:49:28 -04:00
|
|
|
);
|
|
|
|
}
|
2016-03-10 00:37:27 -05:00
|
|
|
rawText = match[0];
|
|
|
|
iconId = grabTheRightIcon(rawText);
|
|
|
|
i = index + rawText.length;
|
|
|
|
src = options.callback(iconId, options);
|
2019-01-17 20:42:50 -05:00
|
|
|
if (iconId && src) {
|
2016-03-10 00:37:27 -05:00
|
|
|
img = new Image();
|
|
|
|
img.onerror = options.onerror;
|
|
|
|
img.setAttribute('draggable', 'false');
|
|
|
|
attrib = options.attributes(rawText, iconId);
|
|
|
|
for (attrname in attrib) {
|
|
|
|
if (
|
|
|
|
attrib.hasOwnProperty(attrname) &&
|
|
|
|
// don't allow any handlers to be set + don't allow overrides
|
|
|
|
attrname.indexOf('on') !== 0 &&
|
|
|
|
!img.hasAttribute(attrname)
|
|
|
|
) {
|
|
|
|
img.setAttribute(attrname, attrib[attrname]);
|
2015-03-31 22:39:28 -04:00
|
|
|
}
|
2015-03-11 18:49:28 -04:00
|
|
|
}
|
2016-03-10 00:37:27 -05:00
|
|
|
img.className = options.className;
|
|
|
|
img.alt = rawText;
|
|
|
|
img.src = src;
|
|
|
|
modified = true;
|
|
|
|
fragment.appendChild(img);
|
2015-03-11 18:49:28 -04:00
|
|
|
}
|
2017-08-13 22:04:45 -04:00
|
|
|
if (!img) fragment.appendChild(createText(rawText, false));
|
2015-03-12 18:00:28 -04:00
|
|
|
img = null;
|
2015-03-11 18:49:28 -04:00
|
|
|
}
|
|
|
|
// is there actually anything to replace in here ?
|
2015-03-12 18:00:28 -04:00
|
|
|
if (modified) {
|
2015-03-11 18:49:28 -04:00
|
|
|
// any text left to be added ?
|
|
|
|
if (i < text.length) {
|
|
|
|
fragment.appendChild(
|
2017-08-13 22:04:45 -04:00
|
|
|
createText(text.slice(i), true)
|
2015-03-11 18:49:28 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
// replace the text node only, leave intact
|
|
|
|
// anything else surrounding such text
|
|
|
|
subnode.parentNode.replaceChild(fragment, subnode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* String/HTML version of the same logic / parser:
|
|
|
|
* emojify a generic text placing images tags instead of surrogates pair.
|
|
|
|
* @param string generic string with possibly some emoji in it
|
|
|
|
* @param Object options containing info about how to parse
|
|
|
|
*
|
|
|
|
* .callback Function the callback to invoke per each found emoji.
|
|
|
|
* .base string the base url, by default twemoji.base
|
|
|
|
* .ext string the image extension, by default twemoji.ext
|
|
|
|
* .size string the assets size, by default twemoji.size
|
|
|
|
*
|
|
|
|
* @return the string with <img tags> replacing all found and parsed emoji
|
|
|
|
*/
|
|
|
|
function parseString(str, options) {
|
2016-03-10 00:37:27 -05:00
|
|
|
return replace(str, function (rawText) {
|
2015-06-26 18:01:25 -04:00
|
|
|
var
|
2016-03-10 00:37:27 -05:00
|
|
|
ret = rawText,
|
|
|
|
iconId = grabTheRightIcon(rawText),
|
|
|
|
src = options.callback(iconId, options),
|
2015-06-26 18:01:25 -04:00
|
|
|
attrib,
|
2016-03-10 00:37:27 -05:00
|
|
|
attrname;
|
2019-01-17 20:42:50 -05:00
|
|
|
if (iconId && src) {
|
2016-03-10 00:37:27 -05:00
|
|
|
// recycle the match string replacing the emoji
|
|
|
|
// with its image counter part
|
|
|
|
ret = '<img '.concat(
|
|
|
|
'class="', options.className, '" ',
|
|
|
|
'draggable="false" ',
|
|
|
|
// needs to preserve user original intent
|
|
|
|
// when variants should be copied and pasted too
|
|
|
|
'alt="',
|
|
|
|
rawText,
|
|
|
|
'"',
|
|
|
|
' src="',
|
|
|
|
src,
|
|
|
|
'"'
|
2015-03-11 18:49:28 -04:00
|
|
|
);
|
2016-03-10 00:37:27 -05:00
|
|
|
attrib = options.attributes(rawText, iconId);
|
|
|
|
for (attrname in attrib) {
|
|
|
|
if (
|
|
|
|
attrib.hasOwnProperty(attrname) &&
|
|
|
|
// don't allow any handlers to be set + don't allow overrides
|
|
|
|
attrname.indexOf('on') !== 0 &&
|
|
|
|
ret.indexOf(' ' + attrname + '=') === -1
|
|
|
|
) {
|
|
|
|
ret = ret.concat(' ', attrname, '="', escapeHTML(attrib[attrname]), '"');
|
2015-06-26 18:01:25 -04:00
|
|
|
}
|
2015-03-11 18:49:28 -04:00
|
|
|
}
|
2017-08-13 22:04:45 -04:00
|
|
|
ret = ret.concat('/>');
|
2015-03-11 18:49:28 -04:00
|
|
|
}
|
2015-06-26 18:01:25 -04:00
|
|
|
return ret;
|
2015-03-11 18:49:28 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-06-26 18:01:25 -04:00
|
|
|
/**
|
|
|
|
* Function used to actually replace HTML special chars
|
|
|
|
* @param string HTML special char
|
|
|
|
* @return string encoded HTML special char
|
|
|
|
*/
|
|
|
|
function replacer(m) {
|
|
|
|
return escaper[m];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default options.attribute callback
|
|
|
|
* @return null
|
|
|
|
*/
|
|
|
|
function returnNull() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2015-03-11 18:49:28 -04:00
|
|
|
/**
|
|
|
|
* Given a generic value, creates its squared counterpart if it's a number.
|
|
|
|
* As example, number 36 will return '36x36'.
|
|
|
|
* @param any a generic value.
|
|
|
|
* @return any a string representing asset size, i.e. "36x36"
|
|
|
|
* only in case the value was a number.
|
|
|
|
* Returns initial value otherwise.
|
|
|
|
*/
|
|
|
|
function toSizeSquaredAsset(value) {
|
|
|
|
return typeof value === 'number' ?
|
|
|
|
value + 'x' + value :
|
|
|
|
value;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/////////////////////////
|
|
|
|
// exported functions //
|
|
|
|
// declaration //
|
|
|
|
/////////////////////////
|
|
|
|
|
|
|
|
function fromCodePoint(codepoint) {
|
|
|
|
var code = typeof codepoint === 'string' ?
|
|
|
|
parseInt(codepoint, 16) : codepoint;
|
|
|
|
if (code < 0x10000) {
|
|
|
|
return fromCharCode(code);
|
|
|
|
}
|
|
|
|
code -= 0x10000;
|
|
|
|
return fromCharCode(
|
|
|
|
0xD800 + (code >> 10),
|
|
|
|
0xDC00 + (code & 0x3FF)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function parse(what, how) {
|
|
|
|
if (!how || typeof how === 'function') {
|
|
|
|
how = {callback: how};
|
|
|
|
}
|
2023-02-01 19:53:25 -05:00
|
|
|
|
|
|
|
// WP start
|
|
|
|
// Allow passing of the doNotParse() callback in the settings.
|
|
|
|
// The callback is used in `grabAllTextNodes()` (DOM mode only) as a filter
|
|
|
|
// that allows bypassing of some of the text nodes. It gets the current subnode as argument.
|
|
|
|
twemoji.doNotParse = how.doNotParse;
|
|
|
|
// WP end
|
|
|
|
|
2015-03-11 18:49:28 -04:00
|
|
|
// if first argument is string, inject html <img> tags
|
|
|
|
// otherwise use the DOM tree and parse text nodes only
|
|
|
|
return (typeof what === 'string' ? parseString : parseNode)(what, {
|
2015-06-26 18:01:25 -04:00
|
|
|
callback: how.callback || defaultImageSrcGenerator,
|
|
|
|
attributes: typeof how.attributes === 'function' ? how.attributes : returnNull,
|
|
|
|
base: typeof how.base === 'string' ? how.base : twemoji.base,
|
|
|
|
ext: how.ext || twemoji.ext,
|
|
|
|
size: how.folder || toSizeSquaredAsset(how.size || twemoji.size),
|
2015-11-14 04:52:29 -05:00
|
|
|
className: how.className || twemoji.className,
|
|
|
|
onerror: how.onerror || twemoji.onerror
|
2015-03-11 18:49:28 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function replace(text, callback) {
|
|
|
|
return String(text).replace(re, callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
function test(text) {
|
|
|
|
// IE6 needs a reset before too
|
|
|
|
re.lastIndex = 0;
|
|
|
|
var result = re.test(text);
|
|
|
|
re.lastIndex = 0;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
function toCodePoint(unicodeSurrogates, sep) {
|
|
|
|
var
|
|
|
|
r = [],
|
|
|
|
c = 0,
|
|
|
|
p = 0,
|
|
|
|
i = 0;
|
|
|
|
while (i < unicodeSurrogates.length) {
|
|
|
|
c = unicodeSurrogates.charCodeAt(i++);
|
|
|
|
if (p) {
|
|
|
|
r.push((0x10000 + ((p - 0xD800) << 10) + (c - 0xDC00)).toString(16));
|
|
|
|
p = 0;
|
|
|
|
} else if (0xD800 <= c && c <= 0xDBFF) {
|
|
|
|
p = c;
|
|
|
|
} else {
|
|
|
|
r.push(c.toString(16));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return r.join(sep || '-');
|
|
|
|
}
|
|
|
|
|
2023-02-01 19:53:25 -05:00
|
|
|
}());
|