1 ( function( window, document, settings ) {
2 var src, ready, ii, tests;
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", "flag", "diversity" or "unicode8" 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,
17 flag, flag2, tonedata, tone, tone2;
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';
34 * This works because the image will be one of three things:
35 * - Two empty squares, if the browser doesn't render emoji
36 * - Two squares with 'A' and 'U' in them, if the browser doesn't render flag emoji
37 * - The Australian flag
39 * The first two will encode to small images (1-2KB data URLs), the third will encode
40 * to a larger image (4-5KB data URL).
42 context.fillText( stringFromCharCode( 55356, 56806, 55356, 56826 ), 0, 0 );
43 if ( canvas.toDataURL().length < 3000 ) {
47 context.clearRect( 0, 0, canvas.width, canvas.height );
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.
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).
57 context.fillText( stringFromCharCode( 55356, 57331, 65039, 8205, 55356, 57096 ), 0, 0 );
58 flag = canvas.toDataURL();
60 context.clearRect( 0, 0, canvas.width, canvas.height );
62 context.fillText( stringFromCharCode( 55356, 57331, 55356, 57096 ), 0, 0 );
63 flag2 = canvas.toDataURL();
65 return flag !== flag2;
68 * This tests if the browser supports the Emoji Diversity specification, by rendering an
69 * emoji with no skin tone specified (in this case, Santa). It then adds a skin tone, and
70 * compares if the emoji rendering has changed.
72 context.fillText( stringFromCharCode( 55356, 57221 ), 0, 0 );
73 tonedata = context.getImageData( 16, 16, 1, 1 ).data;
74 tone = tonedata[0] + ',' + tonedata[1] + ',' + tonedata[2] + ',' + tonedata[3];
76 context.fillText( stringFromCharCode( 55356, 57221, 55356, 57343 ), 0, 0 );
77 // Chrome has issues comparing arrays, and Safari has issues converting arrays to strings.
78 // So, we create our own string and compare that, instead.
79 tonedata = context.getImageData( 16, 16, 1, 1 ).data;
80 tone2 = tonedata[0] + ',' + tonedata[1] + ',' + tonedata[2] + ',' + tonedata[3];
82 return tone !== tone2;
85 * This creates a smiling emoji, and checks to see if there is any image data in the
86 * center pixel. In browsers that don't support emoji, the character will be rendered
87 * as an empty square, so the center pixel will be blank.
89 context.fillText( stringFromCharCode( 55357, 56835 ), 0, 0 );
90 return context.getImageData( 16, 16, 1, 1 ).data[0] !== 0;
93 * To check for Unicode 8 support, let's try rendering the most important advancement
94 * that the Unicode Consortium have made in years: the burrito.
96 context.fillText( stringFromCharCode( 55356, 57135 ), 0, 0 );
97 return context.getImageData( 16, 16, 1, 1 ).data[0] !== 0;
100 * Do Unicode 9 emoji render?
103 context.fillText( stringFromCharCode( 55358, 56631 ), 0, 0 );
104 return context.getImageData( 16, 16, 1, 1 ).data[0] !== 0;
110 function addScript( src ) {
111 var script = document.createElement( 'script' );
114 script.type = 'text/javascript';
115 document.getElementsByTagName( 'head' )[0].appendChild( script );
118 tests = Array( 'simple', 'flag', 'unicode8', 'diversity', 'unicode9' );
120 settings.supports = {
122 everythingExceptFlag: true
125 for( ii = 0; ii < tests.length; ii++ ) {
126 settings.supports[ tests[ ii ] ] = browserSupportsEmoji( tests[ ii ] );
128 settings.supports.everything = settings.supports.everything && settings.supports[ tests[ ii ] ];
130 if ( 'flag' !== tests[ ii ] ) {
131 settings.supports.everythingExceptFlag = settings.supports.everythingExceptFlag && settings.supports[ tests[ ii ] ];
135 settings.supports.everythingExceptFlag = settings.supports.everythingExceptFlag && ! settings.supports.flag;
137 settings.DOMReady = false;
138 settings.readyCallback = function() {
139 settings.DOMReady = true;
142 if ( ! settings.supports.everything ) {
144 settings.readyCallback();
147 if ( document.addEventListener ) {
148 document.addEventListener( 'DOMContentLoaded', ready, false );
149 window.addEventListener( 'load', ready, false );
151 window.attachEvent( 'onload', ready );
152 document.attachEvent( 'onreadystatechange', function() {
153 if ( 'complete' === document.readyState ) {
154 settings.readyCallback();
159 src = settings.source || {};
161 if ( src.concatemoji ) {
162 addScript( src.concatemoji );
163 } else if ( src.wpemoji && src.twemoji ) {
164 addScript( src.twemoji );
165 addScript( src.wpemoji );
169 } )( window, document, window._wpemojiSettings );