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.
 
 
 
 
 

156 line
4.9 KiB

  1. ( function( window, document, settings ) {
  2. var src, ready, ii, tests;
  3. /*
  4. * Create a canvas element for testing native browser support
  5. * of emoji.
  6. */
  7. var canvas = document.createElement( 'canvas' );
  8. var context = canvas.getContext && canvas.getContext( '2d' );
  9. /**
  10. * Detect if the browser supports rendering emoji or flag emoji. Flag emoji are a single glyph
  11. * made of two characters, so some browsers (notably, Firefox OS X) don't support them.
  12. *
  13. * @since 4.2.0
  14. *
  15. * @param type {String} Whether to test for support of "flag" or "emoji4" emoji.
  16. * @return {Boolean} True if the browser can render emoji, false if it cannot.
  17. */
  18. function browserSupportsEmoji( type ) {
  19. var stringFromCharCode = String.fromCharCode,
  20. flag, flag2, technologist, technologist2;
  21. if ( ! context || ! context.fillText ) {
  22. return false;
  23. }
  24. // Cleanup from previous test.
  25. context.clearRect( 0, 0, canvas.width, canvas.height );
  26. /*
  27. * Chrome on OS X added native emoji rendering in M41. Unfortunately,
  28. * it doesn't work when the font is bolder than 500 weight. So, we
  29. * check for bold rendering support to avoid invisible emoji in Chrome.
  30. */
  31. context.textBaseline = 'top';
  32. context.font = '600 32px Arial';
  33. switch ( type ) {
  34. case 'flag':
  35. /*
  36. * This works because the image will be one of three things:
  37. * - Two empty squares, if the browser doesn't render emoji
  38. * - Two squares with 'U' and 'N' in them, if the browser doesn't render flag emoji
  39. * - The United Nations flag
  40. *
  41. * The first two will encode to small images (1-2KB data URLs), the third will encode
  42. * to a larger image (4-5KB data URL).
  43. */
  44. context.fillText( stringFromCharCode( 55356, 56826, 55356, 56819 ), 0, 0 );
  45. if ( canvas.toDataURL().length < 3000 ) {
  46. return false;
  47. }
  48. context.clearRect( 0, 0, canvas.width, canvas.height );
  49. /*
  50. * Test for rainbow flag compatibility. As the rainbow flag was added out of sequence with
  51. * the usual Unicode release cycle, some browsers support it, and some don't, even if their
  52. * Unicode support is up to date.
  53. *
  54. * To test for support, we try to render it, and compare the rendering to how it would look if
  55. * the browser doesn't render it correctly (white flag emoji + rainbow emoji).
  56. */
  57. context.fillText( stringFromCharCode( 55356, 57331, 65039, 8205, 55356, 57096 ), 0, 0 );
  58. flag = canvas.toDataURL();
  59. context.clearRect( 0, 0, canvas.width, canvas.height );
  60. context.fillText( stringFromCharCode( 55356, 57331, 55356, 57096 ), 0, 0 );
  61. flag2 = canvas.toDataURL();
  62. return flag !== flag2;
  63. case 'emoji4':
  64. /*
  65. * Emoji 4 has the best technologists. So does WordPress!
  66. *
  67. * To test for support, try to render a new emoji (woman technologist: medium skin tone),
  68. * then compare it to how it would look if the browser doesn't render it correctly
  69. * (woman technologist: medium skin tone + personal computer).
  70. */
  71. context.fillText( stringFromCharCode( 55357, 56425, 55356, 57341, 8205, 55357, 56507), 0, 0 );
  72. technologist = canvas.toDataURL();
  73. context.clearRect( 0, 0, canvas.width, canvas.height );
  74. context.fillText( stringFromCharCode( 55357, 56425, 55356, 57341, 55357, 56507), 0, 0 );
  75. technologist2 = canvas.toDataURL();
  76. return technologist !== technologist2;
  77. }
  78. return false;
  79. }
  80. function addScript( src ) {
  81. var script = document.createElement( 'script' );
  82. script.src = src;
  83. script.defer = script.type = 'text/javascript';
  84. document.getElementsByTagName( 'head' )[0].appendChild( script );
  85. }
  86. tests = Array( 'flag', 'emoji4' );
  87. settings.supports = {
  88. everything: true,
  89. everythingExceptFlag: true
  90. };
  91. for( ii = 0; ii < tests.length; ii++ ) {
  92. settings.supports[ tests[ ii ] ] = browserSupportsEmoji( tests[ ii ] );
  93. settings.supports.everything = settings.supports.everything && settings.supports[ tests[ ii ] ];
  94. if ( 'flag' !== tests[ ii ] ) {
  95. settings.supports.everythingExceptFlag = settings.supports.everythingExceptFlag && settings.supports[ tests[ ii ] ];
  96. }
  97. }
  98. settings.supports.everythingExceptFlag = settings.supports.everythingExceptFlag && ! settings.supports.flag;
  99. settings.DOMReady = false;
  100. settings.readyCallback = function() {
  101. settings.DOMReady = true;
  102. };
  103. if ( ! settings.supports.everything ) {
  104. ready = function() {
  105. settings.readyCallback();
  106. };
  107. if ( document.addEventListener ) {
  108. document.addEventListener( 'DOMContentLoaded', ready, false );
  109. window.addEventListener( 'load', ready, false );
  110. } else {
  111. window.attachEvent( 'onload', ready );
  112. document.attachEvent( 'onreadystatechange', function() {
  113. if ( 'complete' === document.readyState ) {
  114. settings.readyCallback();
  115. }
  116. } );
  117. }
  118. src = settings.source || {};
  119. if ( src.concatemoji ) {
  120. addScript( src.concatemoji );
  121. } else if ( src.wpemoji && src.twemoji ) {
  122. addScript( src.twemoji );
  123. addScript( src.wpemoji );
  124. }
  125. }
  126. } )( window, document, window._wpemojiSettings );