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' );
17 if ( ! context || ! context.fillText ) {
22 * Chrome on OS X added native emoji rendering in M41. Unfortunately,
23 * it doesn't work when the font is bolder than 500 weight. So, we
24 * check for bold rendering support to avoid invisible emoji in Chrome.
26 context.textBaseline = 'top';
27 context.font = '600 32px Arial';
29 if ( type === 'flag' ) {
31 * This works because the image will be one of three things:
32 * - Two empty squares, if the browser doesn't render emoji
33 * - Two squares with 'G' and 'B' in them, if the browser doesn't render flag emoji
36 * The first two will encode to small images (1-2KB data URLs), the third will encode
37 * to a larger image (4-5KB data URL).
39 context.fillText( String.fromCharCode( 55356, 56812, 55356, 56807 ), 0, 0 );
40 return canvas.toDataURL().length > 3000;
43 * This creates a smiling emoji, and checks to see if there is any image data in the
44 * center pixel. In browsers that don't support emoji, the character will be rendered
45 * as an empty square, so the center pixel will be blank.
47 context.fillText( String.fromCharCode( 55357, 56835 ), 0, 0 );
48 return context.getImageData( 16, 16, 1, 1 ).data[0] !== 0;
52 function addScript( src ) {
53 var script = document.createElement( 'script' );
56 script.type = 'text/javascript';
57 document.getElementsByTagName( 'head' )[0].appendChild( script );
61 simple: browserSupportsEmoji( 'simple' ),
62 flag: browserSupportsEmoji( 'flag' )
65 if ( ! settings.supports.simple || ! settings.supports.flag ) {
66 src = settings.source || {};
68 if ( src.concatemoji ) {
69 addScript( src.concatemoji );
70 } else if ( src.wpemoji && src.twemoji ) {
71 addScript( src.twemoji );
72 addScript( src.wpemoji );
76 } )( window, document, window._wpemojiSettings );