]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/js/wp-emoji-loader.js
WordPress 4.5.1
[autoinstalls/wordpress.git] / wp-includes / js / wp-emoji-loader.js
1 ( function( window, document, settings ) {
2         var src, ready, ii, tests;
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", "flag", "diversity" or "unicode8" 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                         stringFromCharCode = String.fromCharCode,
17                         tonedata, tone, tone2;
18
19                 if ( ! context || ! context.fillText ) {
20                         return false;
21                 }
22
23                 /*
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.
27                  */
28                 context.textBaseline = 'top';
29                 context.font = '600 32px Arial';
30
31                 switch ( type ) {
32                         case 'flag':
33                                 /*
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
38                                  *
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).
41                                  */
42                                 context.fillText( stringFromCharCode( 55356, 56806, 55356, 56826 ), 0, 0 );
43                                 return canvas.toDataURL().length > 3000;
44                         case 'diversity':
45                                 /*
46                                  * This tests if the browser supports the Emoji Diversity specification, by rendering an
47                                  * emoji with no skin tone specified (in this case, Santa). It then adds a skin tone, and
48                                  * compares if the emoji rendering has changed.
49                                  */
50                                 context.fillText( stringFromCharCode( 55356, 57221 ), 0, 0 );
51                                 tonedata = context.getImageData( 16, 16, 1, 1 ).data;
52                                 tone = tonedata[0] + ',' + tonedata[1] + ',' + tonedata[2] + ',' + tonedata[3];
53
54                                 context.fillText( stringFromCharCode( 55356, 57221, 55356, 57343 ), 0, 0 );
55                                 // Chrome has issues comparing arrays, and Safari has issues converting arrays to strings.
56                                 // So, we create our own string and compare that, instead.
57                                 tonedata = context.getImageData( 16, 16, 1, 1 ).data;
58                                 tone2 = tonedata[0] + ',' + tonedata[1] + ',' + tonedata[2] + ',' + tonedata[3];
59
60                                 return tone !== tone2;
61                         case 'simple':
62                                 /*
63                                  * This creates a smiling emoji, and checks to see if there is any image data in the
64                                  * center pixel. In browsers that don't support emoji, the character will be rendered
65                                  * as an empty square, so the center pixel will be blank.
66                                  */
67                                 context.fillText( stringFromCharCode( 55357, 56835 ), 0, 0 );
68                                 return context.getImageData( 16, 16, 1, 1 ).data[0] !== 0;
69                         case 'unicode8':
70                                 /*
71                                  * To check for Unicode 8 support, let's try rendering the most important advancement
72                                  * that the Unicode Consortium have made in years: the burrito.
73                                  */
74                                 context.fillText( stringFromCharCode( 55356, 57135 ), 0, 0 );
75                                 return context.getImageData( 16, 16, 1, 1 ).data[0] !== 0;
76                 }
77
78                 return false;
79         }
80
81         function addScript( src ) {
82                 var script = document.createElement( 'script' );
83
84                 script.src = src;
85                 script.type = 'text/javascript';
86                 document.getElementsByTagName( 'head' )[0].appendChild( script );
87         }
88
89         tests = Array( 'simple', 'flag', 'unicode8', 'diversity' );
90
91         settings.supports = {
92                 everything: true,
93                 everythingExceptFlag: true
94         };
95
96         for( ii = 0; ii < tests.length; ii++ ) {
97                 settings.supports[ tests[ ii ] ] = browserSupportsEmoji( tests[ ii ] );
98
99                 settings.supports.everything = settings.supports.everything && settings.supports[ tests[ ii ] ];
100
101                 if ( 'flag' !== tests[ ii ] ) {
102                         settings.supports.everythingExceptFlag = settings.supports.everythingExceptFlag && settings.supports[ tests[ ii ] ];
103                 }
104         }
105
106         settings.supports.everythingExceptFlag = settings.supports.everythingExceptFlag && ! settings.supports.flag;
107
108         settings.DOMReady = false;
109         settings.readyCallback = function() {
110                 settings.DOMReady = true;
111         };
112
113         if ( ! settings.supports.everything ) {
114                 ready = function() {
115                         settings.readyCallback();
116                 };
117
118                 if ( document.addEventListener ) {
119                         document.addEventListener( 'DOMContentLoaded', ready, false );
120                         window.addEventListener( 'load', ready, false );
121                 } else {
122                         window.attachEvent( 'onload', ready );
123                         document.attachEvent( 'onreadystatechange', function() {
124                                 if ( 'complete' === document.readyState ) {
125                                         settings.readyCallback();
126                                 }
127                         } );
128                 }
129
130                 src = settings.source || {};
131
132                 if ( src.concatemoji ) {
133                         addScript( src.concatemoji );
134                 } else if ( src.wpemoji && src.twemoji ) {
135                         addScript( src.twemoji );
136                         addScript( src.wpemoji );
137                 }
138         }
139
140 } )( window, document, window._wpemojiSettings );