]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/js/wp-emoji-loader.js
WordPress 4.4.1
[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                         tone;
17
18                 if ( ! context || ! context.fillText ) {
19                         return false;
20                 }
21
22                 /*
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.
26                  */
27                 context.textBaseline = 'top';
28                 context.font = '600 32px Arial';
29
30                 if ( 'flag' === type ) {
31                         /*
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
36                          *
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).
39                          */
40                         context.fillText( String.fromCharCode( 55356, 56806, 55356, 56826 ), 0, 0 );
41                         return canvas.toDataURL().length > 3000;
42                 } else if ( 'diversity' === type ) {
43                         /*
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.
47                          */
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();
53                 } else {
54                         if ( 'simple' === type ) {
55                                 /*
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.
59                                  */
60                                 context.fillText( String.fromCharCode( 55357, 56835 ), 0, 0 );
61                         } else {
62                                 /*
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.
65                                  */
66                                 context.fillText( String.fromCharCode( 55356, 57135 ), 0, 0 );
67                         }
68                         return context.getImageData( 16, 16, 1, 1 ).data[0] !== 0;
69                 }
70         }
71
72         function addScript( src ) {
73                 var script = document.createElement( 'script' );
74
75                 script.src = src;
76                 script.type = 'text/javascript';
77                 document.getElementsByTagName( 'head' )[0].appendChild( script );
78         }
79
80         settings.supports = {
81                 simple:    browserSupportsEmoji( 'simple' ),
82                 flag:      browserSupportsEmoji( 'flag' ),
83                 unicode8:  browserSupportsEmoji( 'unicode8' ),
84                 diversity: browserSupportsEmoji( 'diversity' )
85         };
86
87         settings.DOMReady = false;
88         settings.readyCallback = function() {
89                 settings.DOMReady = true;
90         };
91
92         if ( ! settings.supports.simple || ! settings.supports.flag || ! settings.supports.unicode8 || ! settings.supports.diversity ) {
93                 ready = function() {
94                         settings.readyCallback();
95                 };
96
97                 if ( document.addEventListener ) {
98                         document.addEventListener( 'DOMContentLoaded', ready, false );
99                         window.addEventListener( 'load', ready, false );
100                 } else {
101                         window.attachEvent( 'onload', ready );
102                         document.attachEvent( 'onreadystatechange', function() {
103                                 if ( 'complete' === document.readyState ) {
104                                         settings.readyCallback();
105                                 }
106                         } );
107                 }
108
109                 src = settings.source || {};
110
111                 if ( src.concatemoji ) {
112                         addScript( src.concatemoji );
113                 } else if ( src.wpemoji && src.twemoji ) {
114                         addScript( src.twemoji );
115                         addScript( src.wpemoji );
116                 }
117         }
118
119 } )( window, document, window._wpemojiSettings );