You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

589 lines
32 KiB

1 year ago
  1. /*jslint indent: 2, browser: true, bitwise: true, plusplus: true */
  2. var twemoji = (function (
  3. /*! Copyright Twitter Inc. and other contributors. Licensed under MIT *//*
  4. https://github.com/twitter/twemoji/blob/gh-pages/LICENSE
  5. */
  6. /*
  7. * Note: this file was modified in two places to add support for a doNotParse() callback.
  8. * The modifications are surrounded by `// WP start` and `// WP end` comments.
  9. */
  10. // WARNING: this file is generated automatically via
  11. // `node scripts/build.js`
  12. // please update its `createTwemoji` function
  13. // at the bottom of the same file instead.
  14. ) {
  15. 'use strict';
  16. /*jshint maxparams:4 */
  17. var
  18. // the exported module object
  19. twemoji = {
  20. /////////////////////////
  21. // properties //
  22. /////////////////////////
  23. // default assets url, by default will be Twitter Inc. CDN
  24. base: 'https://twemoji.maxcdn.com/v/14.0.2/',
  25. // default assets file extensions, by default '.png'
  26. ext: '.png',
  27. // default assets/folder size, by default "72x72"
  28. // available via Twitter CDN: 72
  29. size: '72x72',
  30. // default class name, by default 'emoji'
  31. className: 'emoji',
  32. // basic utilities / helpers to convert code points
  33. // to JavaScript surrogates and vice versa
  34. convert: {
  35. /**
  36. * Given an HEX codepoint, returns UTF16 surrogate pairs.
  37. *
  38. * @param string generic codepoint, i.e. '1F4A9'
  39. * @return string codepoint transformed into utf16 surrogates pair,
  40. * i.e. \uD83D\uDCA9
  41. *
  42. * @example
  43. * twemoji.convert.fromCodePoint('1f1e8');
  44. * // "\ud83c\udde8"
  45. *
  46. * '1f1e8-1f1f3'.split('-').map(twemoji.convert.fromCodePoint).join('')
  47. * // "\ud83c\udde8\ud83c\uddf3"
  48. */
  49. fromCodePoint: fromCodePoint,
  50. /**
  51. * Given UTF16 surrogate pairs, returns the equivalent HEX codepoint.
  52. *
  53. * @param string generic utf16 surrogates pair, i.e. \uD83D\uDCA9
  54. * @param string optional separator for double code points, default='-'
  55. * @return string utf16 transformed into codepoint, i.e. '1F4A9'
  56. *
  57. * @example
  58. * twemoji.convert.toCodePoint('\ud83c\udde8\ud83c\uddf3');
  59. * // "1f1e8-1f1f3"
  60. *
  61. * twemoji.convert.toCodePoint('\ud83c\udde8\ud83c\uddf3', '~');
  62. * // "1f1e8~1f1f3"
  63. */
  64. toCodePoint: toCodePoint
  65. },
  66. /////////////////////////
  67. // methods //
  68. /////////////////////////
  69. /**
  70. * User first: used to remove missing images
  71. * preserving the original text intent when
  72. * a fallback for network problems is desired.
  73. * Automatically added to Image nodes via DOM
  74. * It could be recycled for string operations via:
  75. * $('img.emoji').on('error', twemoji.onerror)
  76. */
  77. onerror: function onerror() {
  78. if (this.parentNode) {
  79. this.parentNode.replaceChild(createText(this.alt, false), this);
  80. }
  81. },
  82. /**
  83. * Main method/logic to generate either <img> tags or HTMLImage nodes.
  84. * "emojify" a generic text or DOM Element.
  85. *
  86. * @overloads
  87. *
  88. * String replacement for `innerHTML` or server side operations
  89. * twemoji.parse(string);
  90. * twemoji.parse(string, Function);
  91. * twemoji.parse(string, Object);
  92. *
  93. * HTMLElement tree parsing for safer operations over existing DOM
  94. * twemoji.parse(HTMLElement);
  95. * twemoji.parse(HTMLElement, Function);
  96. * twemoji.parse(HTMLElement, Object);
  97. *
  98. * @param string|HTMLElement the source to parse and enrich with emoji.
  99. *
  100. * string replace emoji matches with <img> tags.
  101. * Mainly used to inject emoji via `innerHTML`
  102. * It does **not** parse the string or validate it,
  103. * it simply replaces found emoji with a tag.
  104. * NOTE: be sure this won't affect security.
  105. *
  106. * HTMLElement walk through the DOM tree and find emoji
  107. * that are inside **text node only** (nodeType === 3)
  108. * Mainly used to put emoji in already generated DOM
  109. * without compromising surrounding nodes and
  110. * **avoiding** the usage of `innerHTML`.
  111. * NOTE: Using DOM elements instead of strings should
  112. * improve security without compromising too much
  113. * performance compared with a less safe `innerHTML`.
  114. *
  115. * @param Function|Object [optional]
  116. * either the callback that will be invoked or an object
  117. * with all properties to use per each found emoji.
  118. *
  119. * Function if specified, this will be invoked per each emoji
  120. * that has been found through the RegExp except
  121. * those follwed by the invariant \uFE0E ("as text").
  122. * Once invoked, parameters will be:
  123. *
  124. * iconId:string the lower case HEX code point
  125. * i.e. "1f4a9"
  126. *
  127. * options:Object all info for this parsing operation
  128. *
  129. * variant:char the optional \uFE0F ("as image")
  130. * variant, in case this info
  131. * is anyhow meaningful.
  132. * By default this is ignored.
  133. *
  134. * If such callback will return a falsy value instead
  135. * of a valid `src` to use for the image, nothing will
  136. * actually change for that specific emoji.
  137. *
  138. *
  139. * Object if specified, an object containing the following properties
  140. *
  141. * callback Function the callback to invoke per each found emoji.
  142. * base string the base url, by default twemoji.base
  143. * ext string the image extension, by default twemoji.ext
  144. * size string the assets size, by default twemoji.size
  145. *
  146. * @example
  147. *
  148. * twemoji.parse("I \u2764\uFE0F emoji!");
  149. * // I <img class="emoji" draggable="false" alt="❤️" src="/assets/2764.gif"/> emoji!
  150. *
  151. *
  152. * twemoji.parse("I \u2764\uFE0F emoji!", function(iconId, options) {
  153. * return '/assets/' + iconId + '.gif';
  154. * });
  155. * // I <img class="emoji" draggable="false" alt="❤️" src="/assets/2764.gif"/> emoji!
  156. *
  157. *
  158. * twemoji.parse("I \u2764\uFE0F emoji!", {
  159. * size: 72,
  160. * callback: function(iconId, options) {
  161. * return '/assets/' + options.size + '/' + iconId + options.ext;
  162. * }
  163. * });
  164. * // I <img class="emoji" draggable="false" alt="❤️" src="/assets/72x72/2764.png"/> emoji!
  165. *
  166. */
  167. parse: parse,
  168. /**
  169. * Given a string, invokes the callback argument
  170. * per each emoji found in such string.
  171. * This is the most raw version used by
  172. * the .parse(string) method itself.
  173. *
  174. * @param string generic string to parse
  175. * @param Function a generic callback that will be
  176. * invoked to replace the content.
  177. * This callback will receive standard
  178. * String.prototype.replace(str, callback)
  179. * arguments such:
  180. * callback(
  181. * rawText, // the emoji match
  182. * );
  183. *
  184. * and others commonly received via replace.
  185. */
  186. replace: replace,
  187. /**
  188. * Simplify string tests against emoji.
  189. *
  190. * @param string some text that might contain emoji
  191. * @return boolean true if any emoji was found, false otherwise.
  192. *
  193. * @example
  194. *
  195. * if (twemoji.test(someContent)) {
  196. * console.log("emoji All The Things!");
  197. * }
  198. */
  199. test: test
  200. },
  201. // used to escape HTML special chars in attributes
  202. escaper = {
  203. '&': '&amp;',
  204. '<': '&lt;',
  205. '>': '&gt;',
  206. "'": '&#39;',
  207. '"': '&quot;'
  208. },
  209. // RegExp based on emoji's official Unicode standards
  210. // http://www.unicode.org/Public/UNIDATA/EmojiSources.txt
  211. 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
  212. // avoid runtime RegExp creation for not so smart,
  213. // not JIT based, and old browsers / engines
  214. UFE0Fg = /\uFE0F/g,
  215. // avoid using a string literal like '\u200D' here because minifiers expand it inline
  216. U200D = String.fromCharCode(0x200D),
  217. // used to find HTML special chars in attributes
  218. rescaper = /[&<>'"]/g,
  219. // nodes with type 1 which should **not** be parsed
  220. shouldntBeParsed = /^(?:iframe|noframes|noscript|script|select|style|textarea)$/,
  221. // just a private shortcut
  222. fromCharCode = String.fromCharCode;
  223. return twemoji;
  224. /////////////////////////
  225. // private functions //
  226. // declaration //
  227. /////////////////////////
  228. /**
  229. * Shortcut to create text nodes
  230. * @param string text used to create DOM text node
  231. * @return Node a DOM node with that text
  232. */
  233. function createText(text, clean) {
  234. return document.createTextNode(clean ? text.replace(UFE0Fg, '') : text);
  235. }
  236. /**
  237. * Utility function to escape html attribute text
  238. * @param string text use in HTML attribute
  239. * @return string text encoded to use in HTML attribute
  240. */
  241. function escapeHTML(s) {
  242. return s.replace(rescaper, replacer);
  243. }
  244. /**
  245. * Default callback used to generate emoji src
  246. * based on Twitter CDN
  247. * @param string the emoji codepoint string
  248. * @param string the default size to use, i.e. "36x36"
  249. * @return string the image source to use
  250. */
  251. function defaultImageSrcGenerator(icon, options) {
  252. return ''.concat(options.base, options.size, '/', icon, options.ext);
  253. }
  254. /**
  255. * Given a generic DOM nodeType 1, walk through all children
  256. * and store every nodeType 3 (#text) found in the tree.
  257. * @param Element a DOM Element with probably some text in it
  258. * @param Array the list of previously discovered text nodes
  259. * @return Array same list with new discovered nodes, if any
  260. */
  261. function grabAllTextNodes(node, allText) {
  262. var
  263. childNodes = node.childNodes,
  264. length = childNodes.length,
  265. subnode,
  266. nodeType;
  267. while (length--) {
  268. subnode = childNodes[length];
  269. nodeType = subnode.nodeType;
  270. // parse emoji only in text nodes
  271. if (nodeType === 3) {
  272. // collect them to process emoji later
  273. allText.push(subnode);
  274. }
  275. // ignore all nodes that are not type 1, that are svg, or that
  276. // should not be parsed as script, style, and others
  277. else if (nodeType === 1 && !('ownerSVGElement' in subnode) &&
  278. !shouldntBeParsed.test(subnode.nodeName.toLowerCase())) {
  279. // WP start
  280. // Use doNotParse() callback if set.
  281. if ( twemoji.doNotParse && twemoji.doNotParse( subnode ) ) {
  282. continue;
  283. }
  284. // WP end
  285. grabAllTextNodes(subnode, allText);
  286. }
  287. }
  288. return allText;
  289. }
  290. /**
  291. * Used to both remove the possible variant
  292. * and to convert utf16 into code points.
  293. * If there is a zero-width-joiner (U+200D), leave the variants in.
  294. * @param string the raw text of the emoji match
  295. * @return string the code point
  296. */
  297. function grabTheRightIcon(rawText) {
  298. // if variant is present as \uFE0F
  299. return toCodePoint(rawText.indexOf(U200D) < 0 ?
  300. rawText.replace(UFE0Fg, '') :
  301. rawText
  302. );
  303. }
  304. /**
  305. * DOM version of the same logic / parser:
  306. * emojify all found sub-text nodes placing images node instead.
  307. * @param Element generic DOM node with some text in some child node
  308. * @param Object options containing info about how to parse
  309. *
  310. * .callback Function the callback to invoke per each found emoji.
  311. * .base string the base url, by default twemoji.base
  312. * .ext string the image extension, by default twemoji.ext
  313. * .size string the assets size, by default twemoji.size
  314. *
  315. * @return Element same generic node with emoji in place, if any.
  316. */
  317. function parseNode(node, options) {
  318. var
  319. allText = grabAllTextNodes(node, []),
  320. length = allText.length,
  321. attrib,
  322. attrname,
  323. modified,
  324. fragment,
  325. subnode,
  326. text,
  327. match,
  328. i,
  329. index,
  330. img,
  331. rawText,
  332. iconId,
  333. src;
  334. while (length--) {
  335. modified = false;
  336. fragment = document.createDocumentFragment();
  337. subnode = allText[length];
  338. text = subnode.nodeValue;
  339. i = 0;
  340. while ((match = re.exec(text))) {
  341. index = match.index;
  342. if (index !== i) {
  343. fragment.appendChild(
  344. createText(text.slice(i, index), true)
  345. );
  346. }
  347. rawText = match[0];
  348. iconId = grabTheRightIcon(rawText);
  349. i = index + rawText.length;
  350. src = options.callback(iconId, options);
  351. if (iconId && src) {
  352. img = new Image();
  353. img.onerror = options.onerror;
  354. img.setAttribute('draggable', 'false');
  355. attrib = options.attributes(rawText, iconId);
  356. for (attrname in attrib) {
  357. if (
  358. attrib.hasOwnProperty(attrname) &&
  359. // don't allow any handlers to be set + don't allow overrides
  360. attrname.indexOf('on') !== 0 &&
  361. !img.hasAttribute(attrname)
  362. ) {
  363. img.setAttribute(attrname, attrib[attrname]);
  364. }
  365. }
  366. img.className = options.className;
  367. img.alt = rawText;
  368. img.src = src;
  369. modified = true;
  370. fragment.appendChild(img);
  371. }
  372. if (!img) fragment.appendChild(createText(rawText, false));
  373. img = null;
  374. }
  375. // is there actually anything to replace in here ?
  376. if (modified) {
  377. // any text left to be added ?
  378. if (i < text.length) {
  379. fragment.appendChild(
  380. createText(text.slice(i), true)
  381. );
  382. }
  383. // replace the text node only, leave intact
  384. // anything else surrounding such text
  385. subnode.parentNode.replaceChild(fragment, subnode);
  386. }
  387. }
  388. return node;
  389. }
  390. /**
  391. * String/HTML version of the same logic / parser:
  392. * emojify a generic text placing images tags instead of surrogates pair.
  393. * @param string generic string with possibly some emoji in it
  394. * @param Object options containing info about how to parse
  395. *
  396. * .callback Function the callback to invoke per each found emoji.
  397. * .base string the base url, by default twemoji.base
  398. * .ext string the image extension, by default twemoji.ext
  399. * .size string the assets size, by default twemoji.size
  400. *
  401. * @return the string with <img tags> replacing all found and parsed emoji
  402. */
  403. function parseString(str, options) {
  404. return replace(str, function (rawText) {
  405. var
  406. ret = rawText,
  407. iconId = grabTheRightIcon(rawText),
  408. src = options.callback(iconId, options),
  409. attrib,
  410. attrname;
  411. if (iconId && src) {
  412. // recycle the match string replacing the emoji
  413. // with its image counter part
  414. ret = '<img '.concat(
  415. 'class="', options.className, '" ',
  416. 'draggable="false" ',
  417. // needs to preserve user original intent
  418. // when variants should be copied and pasted too
  419. 'alt="',
  420. rawText,
  421. '"',
  422. ' src="',
  423. src,
  424. '"'
  425. );
  426. attrib = options.attributes(rawText, iconId);
  427. for (attrname in attrib) {
  428. if (
  429. attrib.hasOwnProperty(attrname) &&
  430. // don't allow any handlers to be set + don't allow overrides
  431. attrname.indexOf('on') !== 0 &&
  432. ret.indexOf(' ' + attrname + '=') === -1
  433. ) {
  434. ret = ret.concat(' ', attrname, '="', escapeHTML(attrib[attrname]), '"');
  435. }
  436. }
  437. ret = ret.concat('/>');
  438. }
  439. return ret;
  440. });
  441. }
  442. /**
  443. * Function used to actually replace HTML special chars
  444. * @param string HTML special char
  445. * @return string encoded HTML special char
  446. */
  447. function replacer(m) {
  448. return escaper[m];
  449. }
  450. /**
  451. * Default options.attribute callback
  452. * @return null
  453. */
  454. function returnNull() {
  455. return null;
  456. }
  457. /**
  458. * Given a generic value, creates its squared counterpart if it's a number.
  459. * As example, number 36 will return '36x36'.
  460. * @param any a generic value.
  461. * @return any a string representing asset size, i.e. "36x36"
  462. * only in case the value was a number.
  463. * Returns initial value otherwise.
  464. */
  465. function toSizeSquaredAsset(value) {
  466. return typeof value === 'number' ?
  467. value + 'x' + value :
  468. value;
  469. }
  470. /////////////////////////
  471. // exported functions //
  472. // declaration //
  473. /////////////////////////
  474. function fromCodePoint(codepoint) {
  475. var code = typeof codepoint === 'string' ?
  476. parseInt(codepoint, 16) : codepoint;
  477. if (code < 0x10000) {
  478. return fromCharCode(code);
  479. }
  480. code -= 0x10000;
  481. return fromCharCode(
  482. 0xD800 + (code >> 10),
  483. 0xDC00 + (code & 0x3FF)
  484. );
  485. }
  486. function parse(what, how) {
  487. if (!how || typeof how === 'function') {
  488. how = {callback: how};
  489. }
  490. // WP start
  491. // Allow passing of the doNotParse() callback in the settings.
  492. // The callback is used in `grabAllTextNodes()` (DOM mode only) as a filter
  493. // that allows bypassing of some of the text nodes. It gets the current subnode as argument.
  494. twemoji.doNotParse = how.doNotParse;
  495. // WP end
  496. // if first argument is string, inject html <img> tags
  497. // otherwise use the DOM tree and parse text nodes only
  498. return (typeof what === 'string' ? parseString : parseNode)(what, {
  499. callback: how.callback || defaultImageSrcGenerator,
  500. attributes: typeof how.attributes === 'function' ? how.attributes : returnNull,
  501. base: typeof how.base === 'string' ? how.base : twemoji.base,
  502. ext: how.ext || twemoji.ext,
  503. size: how.folder || toSizeSquaredAsset(how.size || twemoji.size),
  504. className: how.className || twemoji.className,
  505. onerror: how.onerror || twemoji.onerror
  506. });
  507. }
  508. function replace(text, callback) {
  509. return String(text).replace(re, callback);
  510. }
  511. function test(text) {
  512. // IE6 needs a reset before too
  513. re.lastIndex = 0;
  514. var result = re.test(text);
  515. re.lastIndex = 0;
  516. return result;
  517. }
  518. function toCodePoint(unicodeSurrogates, sep) {
  519. var
  520. r = [],
  521. c = 0,
  522. p = 0,
  523. i = 0;
  524. while (i < unicodeSurrogates.length) {
  525. c = unicodeSurrogates.charCodeAt(i++);
  526. if (p) {
  527. r.push((0x10000 + ((p - 0xD800) << 10) + (c - 0xDC00)).toString(16));
  528. p = 0;
  529. } else if (0xD800 <= c && c <= 0xDBFF) {
  530. p = c;
  531. } else {
  532. r.push(c.toString(16));
  533. }
  534. }
  535. return r.join(sep || '-');
  536. }
  537. }());