wp-emoji-loader.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /**
  2. * @output wp-includes/js/wp-emoji-loader.js
  3. */
  4. ( function( window, document, settings ) {
  5. var src, ready, ii, tests;
  6. // Create a canvas element for testing native browser support of emoji.
  7. var canvas = document.createElement( 'canvas' );
  8. var context = canvas.getContext && canvas.getContext( '2d' );
  9. /**
  10. * Checks if two sets of Emoji characters render the same visually.
  11. *
  12. * @since 4.9.0
  13. *
  14. * @private
  15. *
  16. * @param {number[]} set1 Set of Emoji character codes.
  17. * @param {number[]} set2 Set of Emoji character codes.
  18. *
  19. * @return {boolean} True if the two sets render the same.
  20. */
  21. function emojiSetsRenderIdentically( set1, set2 ) {
  22. var stringFromCharCode = String.fromCharCode;
  23. // Cleanup from previous test.
  24. context.clearRect( 0, 0, canvas.width, canvas.height );
  25. context.fillText( stringFromCharCode.apply( this, set1 ), 0, 0 );
  26. var rendered1 = canvas.toDataURL();
  27. // Cleanup from previous test.
  28. context.clearRect( 0, 0, canvas.width, canvas.height );
  29. context.fillText( stringFromCharCode.apply( this, set2 ), 0, 0 );
  30. var rendered2 = canvas.toDataURL();
  31. return rendered1 === rendered2;
  32. }
  33. /**
  34. * Detects if the browser supports rendering emoji or flag emoji.
  35. *
  36. * Flag emoji are a single glyph made of two characters, so some browsers
  37. * (notably, Firefox OS X) don't support them.
  38. *
  39. * @since 4.2.0
  40. *
  41. * @private
  42. *
  43. * @param {string} type Whether to test for support of "flag" or "emoji".
  44. *
  45. * @return {boolean} True if the browser can render emoji, false if it cannot.
  46. */
  47. function browserSupportsEmoji( type ) {
  48. var isIdentical;
  49. if ( ! context || ! context.fillText ) {
  50. return false;
  51. }
  52. /*
  53. * Chrome on OS X added native emoji rendering in M41. Unfortunately,
  54. * it doesn't work when the font is bolder than 500 weight. So, we
  55. * check for bold rendering support to avoid invisible emoji in Chrome.
  56. */
  57. context.textBaseline = 'top';
  58. context.font = '600 32px Arial';
  59. switch ( type ) {
  60. case 'flag':
  61. /*
  62. * Test for Transgender flag compatibility. This flag is shortlisted for the Emoji 13 spec,
  63. * but has landed in Twemoji early, so we can add support for it, too.
  64. *
  65. * To test for support, we try to render it, and compare the rendering to how it would look if
  66. * the browser doesn't render it correctly (white flag emoji + transgender symbol).
  67. */
  68. isIdentical = emojiSetsRenderIdentically(
  69. [ 0x1F3F3, 0xFE0F, 0x200D, 0x26A7, 0xFE0F ],
  70. [ 0x1F3F3, 0xFE0F, 0x200B, 0x26A7, 0xFE0F ]
  71. );
  72. if ( isIdentical ) {
  73. return false;
  74. }
  75. /*
  76. * Test for UN flag compatibility. This is the least supported of the letter locale flags,
  77. * so gives us an easy test for full support.
  78. *
  79. * To test for support, we try to render it, and compare the rendering to how it would look if
  80. * the browser doesn't render it correctly ([U] + [N]).
  81. */
  82. isIdentical = emojiSetsRenderIdentically(
  83. [ 0xD83C, 0xDDFA, 0xD83C, 0xDDF3 ],
  84. [ 0xD83C, 0xDDFA, 0x200B, 0xD83C, 0xDDF3 ]
  85. );
  86. if ( isIdentical ) {
  87. return false;
  88. }
  89. /*
  90. * Test for English flag compatibility. England is a country in the United Kingdom, it
  91. * does not have a two letter locale code but rather an five letter sub-division code.
  92. *
  93. * To test for support, we try to render it, and compare the rendering to how it would look if
  94. * the browser doesn't render it correctly (black flag emoji + [G] + [B] + [E] + [N] + [G]).
  95. */
  96. isIdentical = emojiSetsRenderIdentically(
  97. [ 0xD83C, 0xDFF4, 0xDB40, 0xDC67, 0xDB40, 0xDC62, 0xDB40, 0xDC65, 0xDB40, 0xDC6E, 0xDB40, 0xDC67, 0xDB40, 0xDC7F ],
  98. [ 0xD83C, 0xDFF4, 0x200B, 0xDB40, 0xDC67, 0x200B, 0xDB40, 0xDC62, 0x200B, 0xDB40, 0xDC65, 0x200B, 0xDB40, 0xDC6E, 0x200B, 0xDB40, 0xDC67, 0x200B, 0xDB40, 0xDC7F ]
  99. );
  100. return ! isIdentical;
  101. case 'emoji':
  102. /*
  103. * Why can't we be friends? Everyone can now shake hands in emoji, regardless of skin tone!
  104. *
  105. * To test for Emoji 14.0 support, try to render a new emoji: Handshake: Light Skin Tone, Dark Skin Tone.
  106. *
  107. * The Handshake: Light Skin Tone, Dark Skin Tone emoji is a ZWJ sequence combining 🫱 Rightwards Hand,
  108. * 🏻 Light Skin Tone, a Zero Width Joiner, 🫲 Leftwards Hand, and 🏿 Dark Skin Tone.
  109. *
  110. * 0x1FAF1 == Rightwards Hand
  111. * 0x1F3FB == Light Skin Tone
  112. * 0x200D == Zero-Width Joiner (ZWJ) that links the code points for the new emoji or
  113. * 0x200B == Zero-Width Space (ZWS) that is rendered for clients not supporting the new emoji.
  114. * 0x1FAF2 == Leftwards Hand
  115. * 0x1F3FF == Dark Skin Tone.
  116. *
  117. * When updating this test for future Emoji releases, ensure that individual emoji that make up the
  118. * sequence come from older emoji standards.
  119. */
  120. isIdentical = emojiSetsRenderIdentically(
  121. [0x1FAF1, 0x1F3FB, 0x200D, 0x1FAF2, 0x1F3FF],
  122. [0x1FAF1, 0x1F3FB, 0x200B, 0x1FAF2, 0x1F3FF]
  123. );
  124. return ! isIdentical;
  125. }
  126. return false;
  127. }
  128. /**
  129. * Adds a script to the head of the document.
  130. *
  131. * @ignore
  132. *
  133. * @since 4.2.0
  134. *
  135. * @param {Object} src The url where the script is located.
  136. * @return {void}
  137. */
  138. function addScript( src ) {
  139. var script = document.createElement( 'script' );
  140. script.src = src;
  141. script.defer = script.type = 'text/javascript';
  142. document.getElementsByTagName( 'head' )[0].appendChild( script );
  143. }
  144. tests = Array( 'flag', 'emoji' );
  145. settings.supports = {
  146. everything: true,
  147. everythingExceptFlag: true
  148. };
  149. /*
  150. * Tests the browser support for flag emojis and other emojis, and adjusts the
  151. * support settings accordingly.
  152. */
  153. for( ii = 0; ii < tests.length; ii++ ) {
  154. settings.supports[ tests[ ii ] ] = browserSupportsEmoji( tests[ ii ] );
  155. settings.supports.everything = settings.supports.everything && settings.supports[ tests[ ii ] ];
  156. if ( 'flag' !== tests[ ii ] ) {
  157. settings.supports.everythingExceptFlag = settings.supports.everythingExceptFlag && settings.supports[ tests[ ii ] ];
  158. }
  159. }
  160. settings.supports.everythingExceptFlag = settings.supports.everythingExceptFlag && ! settings.supports.flag;
  161. // Sets DOMReady to false and assigns a ready function to settings.
  162. settings.DOMReady = false;
  163. settings.readyCallback = function() {
  164. settings.DOMReady = true;
  165. };
  166. // When the browser can not render everything we need to load a polyfill.
  167. if ( ! settings.supports.everything ) {
  168. ready = function() {
  169. settings.readyCallback();
  170. };
  171. /*
  172. * Cross-browser version of adding a dom ready event.
  173. */
  174. if ( document.addEventListener ) {
  175. document.addEventListener( 'DOMContentLoaded', ready, false );
  176. window.addEventListener( 'load', ready, false );
  177. } else {
  178. window.attachEvent( 'onload', ready );
  179. document.attachEvent( 'onreadystatechange', function() {
  180. if ( 'complete' === document.readyState ) {
  181. settings.readyCallback();
  182. }
  183. } );
  184. }
  185. src = settings.source || {};
  186. if ( src.concatemoji ) {
  187. addScript( src.concatemoji );
  188. } else if ( src.wpemoji && src.twemoji ) {
  189. addScript( src.twemoji );
  190. addScript( src.wpemoji );
  191. }
  192. }
  193. } )( window, document, window._wpemojiSettings );