wp/wp-includes/js/wp-emoji-loader.js
changeset 5 5e2f62d02dcd
child 7 cf61fcea0001
equal deleted inserted replaced
4:346c88efed21 5:5e2f62d02dcd
       
     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 ( type === 'flag' ) {
       
    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 'G' and 'B' in them, if the browser doesn't render flag emoji
       
    34 			 * - The British 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, 56812, 55356, 56807 ), 0, 0 );
       
    40 			return canvas.toDataURL().length > 3000;
       
    41 		} else {
       
    42 			/*
       
    43 			 * This creates a smiling emoji, and checks to see if there is any image data in the
       
    44 			 * center pixel. In browsers that don't support emoji, the character will be rendered
       
    45 			 * as an empty square, so the center pixel will be blank.
       
    46 			 */
       
    47 			context.fillText( String.fromCharCode( 55357, 56835 ), 0, 0 );
       
    48 			return context.getImageData( 16, 16, 1, 1 ).data[0] !== 0;
       
    49 		}
       
    50 	}
       
    51 
       
    52 	function addScript( src ) {
       
    53 		var script = document.createElement( 'script' );
       
    54 
       
    55 		script.src = src;
       
    56 		script.type = 'text/javascript';
       
    57 		document.getElementsByTagName( 'head' )[0].appendChild( script );
       
    58 	}
       
    59 
       
    60 	settings.supports = {
       
    61 		simple: browserSupportsEmoji( 'simple' ),
       
    62 		flag:   browserSupportsEmoji( 'flag' )
       
    63 	};
       
    64 
       
    65 	settings.DOMReady = false;
       
    66 	settings.readyCallback = function() {
       
    67 		settings.DOMReady = true;
       
    68 	};
       
    69 
       
    70 	if ( ! settings.supports.simple || ! settings.supports.flag ) {
       
    71 		ready = function() {
       
    72 			settings.readyCallback();
       
    73 		};
       
    74 
       
    75 		if ( document.addEventListener ) {
       
    76 			document.addEventListener( 'DOMContentLoaded', ready, false );
       
    77 			window.addEventListener( 'load', ready, false );
       
    78 		} else {
       
    79 			window.attachEvent( 'onload', ready );
       
    80 			document.attachEvent( 'onreadystatechange', function() {
       
    81 				if ( 'complete' === document.readyState ) {
       
    82 					settings.readyCallback();
       
    83 				}
       
    84 			} );
       
    85 		}
       
    86 
       
    87 		src = settings.source || {};
       
    88 
       
    89 		if ( src.concatemoji ) {
       
    90 			addScript( src.concatemoji );
       
    91 		} else if ( src.wpemoji && src.twemoji ) {
       
    92 			addScript( src.twemoji );
       
    93 			addScript( src.wpemoji );
       
    94 		}
       
    95 	}
       
    96 
       
    97 } )( window, document, window._wpemojiSettings );