Make WordPress Core

Ticket #35412: 35412.diff

File 35412.diff, 3.8 KB (added by dd32, 9 years ago)
  • src/wp-includes/js/wp-emoji-loader.js

     
    11( function( window, document, settings ) {
    22        var src, ready;
    33
    44        /**
    55         * Detect if the browser supports rendering emoji or flag emoji. Flag emoji are a single glyph
    66         * made of two characters, so some browsers (notably, Firefox OS X) don't support them.
    77         *
    88         * @since 4.2.0
    99         *
    1010         * @param type {String} Whether to test for support of "simple" or "flag" emoji.
    1111         * @return {Boolean} True if the browser can render emoji, false if it cannot.
    1212         */
    1313        function browserSupportsEmoji( type ) {
    1414                var canvas = document.createElement( 'canvas' ),
    1515                        context = canvas.getContext && canvas.getContext( '2d' ),
     16                        stringFromCharCode = String.fromCharCode,
    1617                        tone;
    1718
    1819                if ( ! context || ! context.fillText ) {
    1920                        return false;
    2021                }
    2122
    2223                /*
    2324                 * Chrome on OS X added native emoji rendering in M41. Unfortunately,
    2425                 * it doesn't work when the font is bolder than 500 weight. So, we
    2526                 * check for bold rendering support to avoid invisible emoji in Chrome.
    2627                 */
    2728                context.textBaseline = 'top';
    2829                context.font = '600 32px Arial';
    2930
    3031                if ( 'flag' === type ) {
    3132                        /*
    3233                         * This works because the image will be one of three things:
    3334                         * - Two empty squares, if the browser doesn't render emoji
    3435                         * - Two squares with 'A' and 'U' in them, if the browser doesn't render flag emoji
    3536                         * - The Australian flag
    3637                         *
    3738                         * The first two will encode to small images (1-2KB data URLs), the third will encode
    3839                         * to a larger image (4-5KB data URL).
    3940                         */
    40                         context.fillText( String.fromCharCode( 55356, 56806, 55356, 56826 ), 0, 0 );
     41                        context.fillText( stringFromCharCode( 55356, 56806, 55356, 56826 ), 0, 0 );
    4142                        return canvas.toDataURL().length > 3000;
    4243                } else if ( 'diversity' === type ) {
    4344                        /*
    4445                         * This tests if the browser supports the Emoji Diversity specification, by rendering an
    4546                         * emoji with no skin tone specified (in this case, Santa). It then adds a skin tone, and
    4647                         * compares if the emoji rendering has changed.
    4748                         */
    48                         context.fillText( String.fromCharCode( 55356, 57221 ), 0, 0 );
     49                        context.fillText( stringFromCharCode( 55356, 57221 ), 0, 0 );
    4950                        tone = context.getImageData( 16, 16, 1, 1 ).data.toString();
    50                         context.fillText( String.fromCharCode( 55356, 57221, 55356, 57343 ), 0, 0 );
     51                        context.fillText( stringFromCharCode( 55356, 57221, 55356, 57343 ), 0, 0 );
    5152                        // Chrome has issues comparing arrays, so we compare it as a  string, instead.
    5253                        return tone !== context.getImageData( 16, 16, 1, 1 ).data.toString();
    5354                } else {
    5455                        if ( 'simple' === type ) {
    5556                                /*
    5657                                 * This creates a smiling emoji, and checks to see if there is any image data in the
    5758                                 * center pixel. In browsers that don't support emoji, the character will be rendered
    5859                                 * as an empty square, so the center pixel will be blank.
    5960                                 */
    60                                 context.fillText( String.fromCharCode( 55357, 56835 ), 0, 0 );
     61                                context.fillText( stringFromCharCode( 55357, 56835 ), 0, 0 );
    6162                        } else {
    6263                                /*
    6364                                 * To check for Unicode 8 support, let's try rendering the most important advancement
    6465                                 * that the Unicode Consortium have made in years: the burrito.
    6566                                 */
    66                                 context.fillText( String.fromCharCode( 55356, 57135 ), 0, 0 );
     67                                context.fillText( stringFromCharCode( 55356, 57135 ), 0, 0 );
    6768                        }
    6869                        return context.getImageData( 16, 16, 1, 1 ).data[0] !== 0;
    6970                }
    7071        }
    7172
    7273        function addScript( src ) {
    7374                var script = document.createElement( 'script' );
    7475
    7576                script.src = src;
    7677                script.type = 'text/javascript';
    7778                document.getElementsByTagName( 'head' )[0].appendChild( script );
    7879        }
    7980
    8081        settings.supports = {
    8182                simple:    browserSupportsEmoji( 'simple' ),