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' ),
18 if ( ! context || ! context.fillText ) {
23 * Chrome on OS X added native emoji rendering in M41. Unfortunately,
24 * it doesn't work when the font is bolder than 500 weight. So, we
25 * check for bold rendering support to avoid invisible emoji in Chrome.
27 context.textBaseline = 'top';
28 context.font = '600 32px Arial';
30 if ( 'flag' === type ) {
32 * This works because the image will be one of three things:
33 * - Two empty squares, if the browser doesn't render emoji
34 * - Two squares with 'A' and 'U' in them, if the browser doesn't render flag emoji
35 * - The Australian flag
37 * The first two will encode to small images (1-2KB data URLs), the third will encode
38 * to a larger image (4-5KB data URL).
40 context.fillText( String.fromCharCode( 55356, 56806, 55356, 56826 ), 0, 0 );
41 return canvas.toDataURL().length > 3000;
42 } else if ( 'diversity' === type ) {
44 * This tests if the browser supports the Emoji Diversity specification, by rendering an
45 * emoji with no skin tone specified (in this case, Santa). It then adds a skin tone, and
46 * compares if the emoji rendering has changed.
48 context.fillText( String.fromCharCode( 55356, 57221 ), 0, 0 );
49 tone = context.getImageData( 16, 16, 1, 1 ).data.toString();
50 context.fillText( String.fromCharCode( 55356, 57221, 55356, 57343 ), 0, 0 );
51 // Chrome has issues comparing arrays, so we compare it as a string, instead.
52 return tone !== context.getImageData( 16, 16, 1, 1 ).data.toString();
54 if ( 'simple' === type ) {
56 * This creates a smiling emoji, and checks to see if there is any image data in the
57 * center pixel. In browsers that don't support emoji, the character will be rendered
58 * as an empty square, so the center pixel will be blank.
60 context.fillText( String.fromCharCode( 55357, 56835 ), 0, 0 );
63 * To check for Unicode 8 support, let's try rendering the most important advancement
64 * that the Unicode Consortium have made in years: the burrito.
66 context.fillText( String.fromCharCode( 55356, 57135 ), 0, 0 );
68 return context.getImageData( 16, 16, 1, 1 ).data[0] !== 0;
72 function addScript( src ) {
73 var script = document.createElement( 'script' );
76 script.type = 'text/javascript';
77 document.getElementsByTagName( 'head' )[0].appendChild( script );
81 simple: browserSupportsEmoji( 'simple' ),
82 flag: browserSupportsEmoji( 'flag' ),
83 unicode8: browserSupportsEmoji( 'unicode8' ),
84 diversity: browserSupportsEmoji( 'diversity' )
87 settings.DOMReady = false;
88 settings.readyCallback = function() {
89 settings.DOMReady = true;
92 if ( ! settings.supports.simple || ! settings.supports.flag || ! settings.supports.unicode8 || ! settings.supports.diversity ) {
94 settings.readyCallback();
97 if ( document.addEventListener ) {
98 document.addEventListener( 'DOMContentLoaded', ready, false );
99 window.addEventListener( 'load', ready, false );
101 window.attachEvent( 'onload', ready );
102 document.attachEvent( 'onreadystatechange', function() {
103 if ( 'complete' === document.readyState ) {
104 settings.readyCallback();
109 src = settings.source || {};
111 if ( src.concatemoji ) {
112 addScript( src.concatemoji );
113 } else if ( src.wpemoji && src.twemoji ) {
114 addScript( src.twemoji );
115 addScript( src.wpemoji );
119 } )( window, document, window._wpemojiSettings );