1 ( function( window, document, settings ) {
5 * Detect if the browser supports rendering emoji or flag emoji. Flag emoji are a single glyph
6 * made of two characters, so some browsers (notably, Firefox OS X) don't support them.
10 * @param type {String} Whether to test for support of "simple" or "flag" emoji.
11 * @return {Boolean} True if the browser can render emoji, false if it cannot.
13 function browserSupportsEmoji( type ) {
14 var canvas = document.createElement( 'canvas' ),
15 context = canvas.getContext && canvas.getContext( '2d' ),
16 stringFromCharCode = String.fromCharCode,
19 if ( ! context || ! context.fillText ) {
24 * Chrome on OS X added native emoji rendering in M41. Unfortunately,
25 * it doesn't work when the font is bolder than 500 weight. So, we
26 * check for bold rendering support to avoid invisible emoji in Chrome.
28 context.textBaseline = 'top';
29 context.font = '600 32px Arial';
31 if ( 'flag' === type ) {
33 * This works because the image will be one of three things:
34 * - Two empty squares, if the browser doesn't render emoji
35 * - Two squares with 'A' and 'U' in them, if the browser doesn't render flag emoji
36 * - The Australian flag
38 * The first two will encode to small images (1-2KB data URLs), the third will encode
39 * to a larger image (4-5KB data URL).
41 context.fillText( stringFromCharCode( 55356, 56806, 55356, 56826 ), 0, 0 );
42 return canvas.toDataURL().length > 3000;
43 } else if ( 'diversity' === type ) {
45 * This tests if the browser supports the Emoji Diversity specification, by rendering an
46 * emoji with no skin tone specified (in this case, Santa). It then adds a skin tone, and
47 * compares if the emoji rendering has changed.
49 context.fillText( stringFromCharCode( 55356, 57221 ), 0, 0 );
50 tone = context.getImageData( 16, 16, 1, 1 ).data.toString();
51 context.fillText( stringFromCharCode( 55356, 57221, 55356, 57343 ), 0, 0 );
52 // Chrome has issues comparing arrays, so we compare it as a string, instead.
53 return tone !== context.getImageData( 16, 16, 1, 1 ).data.toString();
55 if ( 'simple' === type ) {
57 * This creates a smiling emoji, and checks to see if there is any image data in the
58 * center pixel. In browsers that don't support emoji, the character will be rendered
59 * as an empty square, so the center pixel will be blank.
61 context.fillText( stringFromCharCode( 55357, 56835 ), 0, 0 );
64 * To check for Unicode 8 support, let's try rendering the most important advancement
65 * that the Unicode Consortium have made in years: the burrito.
67 context.fillText( stringFromCharCode( 55356, 57135 ), 0, 0 );
69 return context.getImageData( 16, 16, 1, 1 ).data[0] !== 0;
73 function addScript( src ) {
74 var script = document.createElement( 'script' );
77 script.type = 'text/javascript';
78 document.getElementsByTagName( 'head' )[0].appendChild( script );
82 simple: browserSupportsEmoji( 'simple' ),
83 flag: browserSupportsEmoji( 'flag' ),
84 unicode8: browserSupportsEmoji( 'unicode8' ),
85 diversity: browserSupportsEmoji( 'diversity' )
88 settings.DOMReady = false;
89 settings.readyCallback = function() {
90 settings.DOMReady = true;
93 if ( ! settings.supports.simple || ! settings.supports.flag || ! settings.supports.unicode8 || ! settings.supports.diversity ) {
95 settings.readyCallback();
98 if ( document.addEventListener ) {
99 document.addEventListener( 'DOMContentLoaded', ready, false );
100 window.addEventListener( 'load', ready, false );
102 window.attachEvent( 'onload', ready );
103 document.attachEvent( 'onreadystatechange', function() {
104 if ( 'complete' === document.readyState ) {
105 settings.readyCallback();
110 src = settings.source || {};
112 if ( src.concatemoji ) {
113 addScript( src.concatemoji );
114 } else if ( src.wpemoji && src.twemoji ) {
115 addScript( src.twemoji );
116 addScript( src.wpemoji );
120 } )( window, document, window._wpemojiSettings );