No Description

wp-emoji-loader.js 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. * Burning Love: Just a hunk, a hunk of burnin' love.
  104. *
  105. * To test for Emoji 13.1 support, try to render a new emoji: Heart on Fire!
  106. *
  107. * The Heart on Fire emoji is a ZWJ sequence combining ❤️ Red Heart, a Zero Width Joiner and 🔥 Fire.
  108. *
  109. * 0x2764, 0xfe0f == Red Heart emoji.
  110. * 0x200D == Zero-Width Joiner (ZWJ) that links the two code points for the new emoji or
  111. * 0x200B == Zero-Width Space (ZWS) that is rendered for clients not supporting the new emoji.
  112. * 0xD83D, 0xDD25 == Fire.
  113. *
  114. * When updating this test for future Emoji releases, ensure that individual emoji that make up the
  115. * sequence come from older emoji standards.
  116. */
  117. isIdentical = emojiSetsRenderIdentically(
  118. [0x2764, 0xfe0f, 0x200D, 0xD83D, 0xDD25],
  119. [0x2764, 0xfe0f, 0x200B, 0xD83D, 0xDD25]
  120. );
  121. return ! isIdentical;
  122. }
  123. return false;
  124. }
  125. /**
  126. * Adds a script to the head of the document.
  127. *
  128. * @ignore
  129. *
  130. * @since 4.2.0
  131. *
  132. * @param {Object} src The url where the script is located.
  133. * @return {void}
  134. */
  135. function addScript( src ) {
  136. var script = document.createElement( 'script' );
  137. script.src = src;
  138. script.defer = script.type = 'text/javascript';
  139. document.getElementsByTagName( 'head' )[0].appendChild( script );
  140. }
  141. tests = Array( 'flag', 'emoji' );
  142. settings.supports = {
  143. everything: true,
  144. everythingExceptFlag: true
  145. };
  146. /*
  147. * Tests the browser support for flag emojis and other emojis, and adjusts the
  148. * support settings accordingly.
  149. */
  150. for( ii = 0; ii < tests.length; ii++ ) {
  151. settings.supports[ tests[ ii ] ] = browserSupportsEmoji( tests[ ii ] );
  152. settings.supports.everything = settings.supports.everything && settings.supports[ tests[ ii ] ];
  153. if ( 'flag' !== tests[ ii ] ) {
  154. settings.supports.everythingExceptFlag = settings.supports.everythingExceptFlag && settings.supports[ tests[ ii ] ];
  155. }
  156. }
  157. settings.supports.everythingExceptFlag = settings.supports.everythingExceptFlag && ! settings.supports.flag;
  158. // Sets DOMReady to false and assigns a ready function to settings.
  159. settings.DOMReady = false;
  160. settings.readyCallback = function() {
  161. settings.DOMReady = true;
  162. };
  163. // When the browser can not render everything we need to load a polyfill.
  164. if ( ! settings.supports.everything ) {
  165. ready = function() {
  166. settings.readyCallback();
  167. };
  168. /*
  169. * Cross-browser version of adding a dom ready event.
  170. */
  171. if ( document.addEventListener ) {
  172. document.addEventListener( 'DOMContentLoaded', ready, false );
  173. window.addEventListener( 'load', ready, false );
  174. } else {
  175. window.attachEvent( 'onload', ready );
  176. document.attachEvent( 'onreadystatechange', function() {
  177. if ( 'complete' === document.readyState ) {
  178. settings.readyCallback();
  179. }
  180. } );
  181. }
  182. src = settings.source || {};
  183. if ( src.concatemoji ) {
  184. addScript( src.concatemoji );
  185. } else if ( src.wpemoji && src.twemoji ) {
  186. addScript( src.twemoji );
  187. addScript( src.wpemoji );
  188. }
  189. }
  190. } )( window, document, window._wpemojiSettings );