]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/js/wp-emoji-loader.js
WordPress 4.4.2-scripts
[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                         stringFromCharCode = String.fromCharCode,
17                         tone;
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                 if ( 'flag' === type ) {
32                         /*
33                          * This works because the image will be one of three things:
34                          * - Two empty squares, if the browser doesn't render emoji
35                          * - Two squares with 'A' and 'U' in them, if the browser doesn't render flag emoji
36                          * - The Australian flag
37                          *
38                          * The first two will encode to small images (1-2KB data URLs), the third will encode
39                          * to a larger image (4-5KB data URL).
40                          */
41                         context.fillText( stringFromCharCode( 55356, 56806, 55356, 56826 ), 0, 0 );
42                         return canvas.toDataURL().length > 3000;
43                 } else if ( 'diversity' === type ) {
44                         /*
45                          * This tests if the browser supports the Emoji Diversity specification, by rendering an
46                          * emoji with no skin tone specified (in this case, Santa). It then adds a skin tone, and
47                          * compares if the emoji rendering has changed.
48                          */
49                         context.fillText( stringFromCharCode( 55356, 57221 ), 0, 0 );
50                         tone = context.getImageData( 16, 16, 1, 1 ).data.toString();
51                         context.fillText( stringFromCharCode( 55356, 57221, 55356, 57343 ), 0, 0 );
52                         // Chrome has issues comparing arrays, so we compare it as a  string, instead.
53                         return tone !== context.getImageData( 16, 16, 1, 1 ).data.toString();
54                 } else {
55                         if ( 'simple' === type ) {
56                                 /*
57                                  * This creates a smiling emoji, and checks to see if there is any image data in the
58                                  * center pixel. In browsers that don't support emoji, the character will be rendered
59                                  * as an empty square, so the center pixel will be blank.
60                                  */
61                                 context.fillText( stringFromCharCode( 55357, 56835 ), 0, 0 );
62                         } else {
63                                 /*
64                                  * To check for Unicode 8 support, let's try rendering the most important advancement
65                                  * that the Unicode Consortium have made in years: the burrito.
66                                  */
67                                 context.fillText( stringFromCharCode( 55356, 57135 ), 0, 0 );
68                         }
69                         return context.getImageData( 16, 16, 1, 1 ).data[0] !== 0;
70                 }
71         }
72
73         function addScript( src ) {
74                 var script = document.createElement( 'script' );
75
76                 script.src = src;
77                 script.type = 'text/javascript';
78                 document.getElementsByTagName( 'head' )[0].appendChild( script );
79         }
80
81         settings.supports = {
82                 simple:    browserSupportsEmoji( 'simple' ),
83                 flag:      browserSupportsEmoji( 'flag' ),
84                 unicode8:  browserSupportsEmoji( 'unicode8' ),
85                 diversity: browserSupportsEmoji( 'diversity' )
86         };
87
88         settings.DOMReady = false;
89         settings.readyCallback = function() {
90                 settings.DOMReady = true;
91         };
92
93         if ( ! settings.supports.simple || ! settings.supports.flag || ! settings.supports.unicode8 || ! settings.supports.diversity ) {
94                 ready = function() {
95                         settings.readyCallback();
96                 };
97
98                 if ( document.addEventListener ) {
99                         document.addEventListener( 'DOMContentLoaded', ready, false );
100                         window.addEventListener( 'load', ready, false );
101                 } else {
102                         window.attachEvent( 'onload', ready );
103                         document.attachEvent( 'onreadystatechange', function() {
104                                 if ( 'complete' === document.readyState ) {
105                                         settings.readyCallback();
106                                 }
107                         } );
108                 }
109
110                 src = settings.source || {};
111
112                 if ( src.concatemoji ) {
113                         addScript( src.concatemoji );
114                 } else if ( src.wpemoji && src.twemoji ) {
115                         addScript( src.twemoji );
116                         addScript( src.wpemoji );
117                 }
118         }
119
120 } )( window, document, window._wpemojiSettings );