a495f3552b847899b66b96ff2ff49fcd05729367
[autoinstalls/wordpress.git] / wp-includes / js / wp-emoji-loader.js
1 ( function( window, document, settings ) {
2         var src, ready;
3
4         /**
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.
7          *
8          * @since 4.2.0
9          *
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.
12          */
13         function browserSupportsEmoji( type ) {
14                 var canvas = document.createElement( 'canvas' ),
15                         context = canvas.getContext && canvas.getContext( '2d' );
16
17                 if ( ! context || ! context.fillText ) {
18                         return false;
19                 }
20
21                 /*
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.
25                  */
26                 context.textBaseline = 'top';
27                 context.font = '600 32px Arial';
28
29                 if ( 'flag' === type ) {
30                         /*
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 'A' and 'U' in them, if the browser doesn't render flag emoji
34                          * - The Australian flag
35                          *
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).
38                          */
39                         context.fillText( String.fromCharCode( 55356, 56806, 55356, 56826 ), 0, 0 );
40                         return canvas.toDataURL().length > 3000;
41                 } else {
42                         if ( 'simple' === type ) {
43                                 /*
44                                  * This creates a smiling emoji, and checks to see if there is any image data in the
45                                  * center pixel. In browsers that don't support emoji, the character will be rendered
46                                  * as an empty square, so the center pixel will be blank.
47                                  */
48                                 context.fillText( String.fromCharCode( 55357, 56835 ), 0, 0 );
49                         } else {
50                                 /*
51                                  * To check for Unicode 8 support, let's try rendering the most important advancement
52                                  * that the Unicode Consortium have made in years: the burrito.
53                                  */
54                                 context.fillText( String.fromCharCode( 55356, 57135 ), 0, 0 );
55                         }
56                         return context.getImageData( 16, 16, 1, 1 ).data[0] !== 0;
57                 }
58         }
59
60         function addScript( src ) {
61                 var script = document.createElement( 'script' );
62
63                 script.src = src;
64                 script.type = 'text/javascript';
65                 document.getElementsByTagName( 'head' )[0].appendChild( script );
66         }
67
68         settings.supports = {
69                 simple:   browserSupportsEmoji( 'simple' ),
70                 flag:     browserSupportsEmoji( 'flag' ),
71                 unicode8: browserSupportsEmoji( 'unicode8' )
72         };
73
74         settings.DOMReady = false;
75         settings.readyCallback = function() {
76                 settings.DOMReady = true;
77         };
78
79         if ( ! settings.supports.simple || ! settings.supports.flag || ! settings.supports.unicode8 ) {
80                 ready = function() {
81                         settings.readyCallback();
82                 };
83
84                 if ( document.addEventListener ) {
85                         document.addEventListener( 'DOMContentLoaded', ready, false );
86                         window.addEventListener( 'load', ready, false );
87                 } else {
88                         window.attachEvent( 'onload', ready );
89                         document.attachEvent( 'onreadystatechange', function() {
90                                 if ( 'complete' === document.readyState ) {
91                                         settings.readyCallback();
92                                 }
93                         } );
94                 }
95
96                 src = settings.source || {};
97
98                 if ( src.concatemoji ) {
99                         addScript( src.concatemoji );
100                 } else if ( src.wpemoji && src.twemoji ) {
101                         addScript( src.twemoji );
102                         addScript( src.wpemoji );
103                 }
104         }
105
106 } )( window, document, window._wpemojiSettings );