- md5
- e99658aac6519fa130d67a1ab930e2c7
- sha1
- 077e480283d1e8ae94d93e999d47f2274b94d392
- sha256
- dbb2ddefd64b4a79810fe27a567e2fda6df7c08e0ee162458f2e4acc40634b6e
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
/**
* @output wp-includes/js/wp-emoji-loader.js
*/
// Note: This is loaded as a script module, so there is no need for an IIFE to prevent pollution of the global scope.
// Note: The WPEmojiSettings and EmojiSupports types are declared in typings/wp-emoji, since wp-emoji.js reads them back.
const selector = 'script#wp-emoji-settings';
const script = document.querySelector( selector );
if ( ! ( script instanceof HTMLScriptElement ) ) {
throw new Error( `Element missing: ${ selector }` );
}
const settings = /** @type {WPEmojiSettings} */ ( JSON.parse( script.text ) );
// For compatibility with other scripts that read from this global, in particular wp-includes/js/wp-emoji.js (source file: js/_enqueues/wp/emoji.js).
window._wpemojiSettings = settings;
/**
* Results of the emoji support tests.
*
* @typedef SupportTests
* @type {Object}
* @property {boolean} flag Whether the browser renders flag emoji.
* @property {boolean} emoji Whether the browser renders emoji.
*/
const sessionStorageKey = 'wpEmojiSettingsSupports';
/** @type {Array<keyof SupportTests>} */
const tests = [ 'flag', 'emoji' ];
/**
* Checks whether the browser supports offloading to a Worker.
*
* @since 6.3.0
*
* @private
*
* @return {boolean} True if the browser supports offloading to a Worker.
*/
function supportsWorkerOffloading() {
return (
typeof Worker !== 'undefined' &&
typeof OffscreenCanvas !== 'undefined' &&
typeof URL !== 'undefined' &&
typeof URL.createObjectURL === 'function' &&
typeof Blob !== 'undefined'
);
}
/**
* Support tests as they are stored in session storage.
*
* @typedef SessionSupportTests
* @type {Object}
* @property {number} timestamp When the tests were run, in milliseconds since the epoch.
* @property {SupportTests} supportTests What the tests found.
*/
/**
* Get support tests from session.
*
* @since 6.3.0
*
* @private
*
* @return {?SupportTests} Support tests, or null if not set or older than 1 week.
*/
function getSessionSupportTests() {
try {
const itemJson = sessionStorage.getItem( sessionStorageKey );
if ( null === itemJson ) {
return null;
}
/** @type {SessionSupportTests} */
const item = JSON.parse( itemJson );
if (
typeof item === 'object' &&
typeof item.timestamp === 'number' &&
new Date().valueOf() < item.timestamp + 604800 && // Note: Number is a week in seconds.
typeof item.supportTests === 'object'
) {
return item.supportTests;
}
} catch ( e ) {}
return null;
}
/**
* Persist the supports in session storage.
*
* @since 6.3.0
*
* @private
*
* @param {SupportTests} supportTests What the tests found.
*/
function setSessionSupportTests( supportTests ) {
try {
/** @type {SessionSupportTests} */
const item = {
supportTests: supportTests,
timestamp: new Date().valueOf()
};
sessionStorage.setItem(
sessionStorageKey,
JSON.stringify( item )
);
} catch ( e ) {}
}
/**
* A 2D context for the support tests.
*
* Which of the two it is depends on the kind of canvas it came from, and the tests use only what
* both provide.
*
* @typedef {CanvasRenderingContext2D|OffscreenCanvasRenderingContext2D} EmojiTestContext
*/
/**
* Checks if two sets of Emoji characters render the same visually.
*
* @callback EmojiSetsRenderIdentically
*
* @param {EmojiTestContext} context 2D Context.
* @param {string} set1 Set of Emoji to test.
* @param {string} set2 Set of Emoji to test.
*
* @return {boolean} True if the two sets render the same.
*/
/**
* Checks if the center point of a single emoji is empty.
*
* @callback EmojiRendersEmptyCenterPoint
*
* @param {EmojiTestContext} context 2D Context.
* @param {string} emoji Emoji to test.
*
* @return {boolean} True if the center point is empty.
*/
/**
* Determines if the browser properly renders Emoji that Twemoji can supplement.
*
* @callback BrowserSupportsEmoji
*
* @param {EmojiTestContext} context 2D Context.
* @param {keyof SupportTests} type Which support test to run.
* @param {EmojiSetsRenderIdentically} emojiSetsRenderIdentically Reference to emojiSetsRenderIdentically function, needed due to minification.
* @param {EmojiRendersEmptyCenterPoint} emojiRendersEmptyCenterPoint Reference to emojiRendersEmptyCenterPoint function, needed due to minification.
*
* @return {boolean} True if the browser can render emoji, false if it cannot.
*/
/**
* Checks if two sets of Emoji characters render the same visually.
*
* This is used to determine if the browser is rendering an emoji with multiple data points
* correctly. set1 is the emoji in the correct form, using a zero-width joiner. set2 is the emoji
* in the incorrect form, using a zero-width space. If the two sets render the same, then the browser
* does not support the emoji correctly.
*
* This function may be serialized to run in a Worker. Therefore, it cannot refer to variables from the containing
* scope. Everything must be passed by parameters.
*
* @since 4.9.0
*
* @private
*
* @param {EmojiTestContext} context 2D Context.
* @param {string} set1 Set of Emoji to test.
* @param {string} set2 Set of Emoji to test.
*
* @return {boolean} True if the two sets render the same.
*/
function emojiSetsRenderIdentically( context, set1, set2 ) {
// Cleanup from previous test.
context.clearRect( 0, 0, context.canvas.width, context.canvas.height );
context.fillText( set1, 0, 0 );
const rendered1 = new Uint32Array(
context.getImageData(
0,
0,
context.canvas.width,
context.canvas.height
).data
);
// Cleanup from previous test.
context.clearRect( 0, 0, context.canvas.width, context.canvas.height );
context.fillText( set2, 0, 0 );
const rendered2 = new Uint32Array(
context.getImageData(
0,
0,
context.canvas.width,
context.canvas.height
).data
);
return rendered1.every( ( rendered2Data, index ) => {
return rendered2Data === rendered2[ index ];
} );
}
/**
* Checks if the center point of a single emoji is empty.
*
* This is used to determine if the browser is rendering an emoji with a single data point
* correctly. The center point of an incorrectly rendered emoji will be empty. A correctly
* rendered emoji will have a non-zero value at the center point.
*
* This function may be serialized to run in a Worker. Therefore, it cannot refer to variables from the containing
* scope. Everything must be passed by parameters.
*
* @since 6.8.2
*
* @private
*
* @param {EmojiTestContext} context 2D Context.
* @param {string} emoji Emoji to test.
*
* @return {boolean} True if the center point is empty.
*/
function emojiRendersEmptyCenterPoint( context, emoji ) {
// Cleanup from previous test.
context.clearRect( 0, 0, context.canvas.width, context.canvas.height );
context.fillText( emoji, 0, 0 );
// Test if the center point (16, 16) is empty (0,0,0,0).
const centerPoint = context.getImageData( 16, 16, 1, 1 );
for ( let i = 0; i < centerPoint.data.length; i++ ) {
if ( centerPoint.data[ i ] !== 0 ) {
// Stop checking the moment it's known not to be empty.
return false;
}
}
return true;
}
/**
* Determines if the browser properly renders Emoji that Twemoji can supplement.
*
* This function may be serialized to run in a Worker. Therefore, it cannot refer to variables from the containing
* scope. Everything must be passed by parameters.
*
* @since 4.2.0
*
* @private
*
* @param {EmojiTestContext} context 2D Context.
* @param {keyof SupportTests} type Which support test to run.
* @param {EmojiSetsRenderIdentically} emojiSetsRenderIdentically Reference to emojiSetsRenderIdentically function, needed due to minification.
* @param {EmojiRendersEmptyCenterPoint} emojiRendersEmptyCenterPoint Reference to emojiRendersEmptyCenterPoint function, needed due to minification.
*
* @return {boolean} True if the browser can render emoji, false if it cannot.
*/
function browserSupportsEmoji( context, type, emojiSetsRenderIdentically, emojiRendersEmptyCenterPoint ) {
/** @type {boolean} */
let isIdentical;
switch ( type ) {
case 'flag':
/*
* Test for Transgender flag compatibility. Added in Unicode 13.
*
* To test for support, we try to render it, and compare the rendering to how it would look if
* the browser doesn't render it correctly (white flag emoji + transgender symbol).
*/
isIdentical = emojiSetsRenderIdentically(
context,
'\uD83C\uDFF3\uFE0F\u200D\u26A7\uFE0F', // as a zero-width joiner sequence
'\uD83C\uDFF3\uFE0F\u200B\u26A7\uFE0F' // separated by a zero-width space
);
if ( isIdentical ) {
return false;
}
/*
* Test for Sark flag compatibility. This is the least supported of the letter locale flags,
* so gives us an easy test for full support.
*
* To test for support, we try to render it, and compare the rendering to how it would look if
* the browser doesn't render it correctly ([C] + [Q]).
*/
isIdentical = emojiSetsRenderIdentically(
context,
'\uD83C\uDDE8\uD83C\uDDF6', // as the sequence of two code points
'\uD83C\uDDE8\u200B\uD83C\uDDF6' // as the two code points separated by a zero-width space
);
if ( isIdentical ) {
return false;
}
/*
* Test for English flag compatibility. England is a country in the United Kingdom, it
* does not have a two letter locale code but rather a five letter subdivision code.
*
* To test for support, we try to render it, and compare the rendering to how it would look if
* the browser doesn't render it correctly (black flag emoji + [G] + [B] + [E] + [N] + [G]).
*/
isIdentical = emojiSetsRenderIdentically(
context,
// as the flag sequence
'\uD83C\uDFF4\uDB40\uDC67\uDB40\uDC62\uDB40\uDC65\uDB40\uDC6E\uDB40\uDC67\uDB40\uDC7F',
// with each code point separated by a zero-width space
'\uD83C\uDFF4\u200B\uDB40\uDC67\u200B\uDB40\uDC62\u200B\uDB40\uDC65\u200B\uDB40\uDC6E\u200B\uDB40\uDC67\u200B\uDB40\uDC7F'
);
return ! isIdentical;
case 'emoji':
/*
* Is there a large, hairy, humanoid mythical creature living in the browser?
*
* To test for Emoji 17.0 support, try to render a new emoji: Hairy Creature.
*
* The hairy creature emoji is a single code point emoji. Testing for browser
* support required testing the center point of the emoji to see if it is empty.
*
* U+1FAC8 (\uD83E\uDEC8) == 🫈 Hairy creature.
*
* When updating this test, please ensure that the emoji is either a single code point
* or switch to using the emojiSetsRenderIdentically function and testing with a zero-width
* joiner vs a zero-width space.
*/
const notSupported = emojiRendersEmptyCenterPoint( context, '\uD83E\uDEC8' );
return ! notSupported;
}
}
/**
* Checks emoji support tests.
*
* This function may be serialized to run in a Worker. Therefore, it cannot refer to variables from the containing
* scope. Everything must be passed by parameters.
*
* @since 6.3.0
*
* @private
*
* @param {Array<keyof SupportTests>} tests Which support tests to run.
* @param {BrowserSupportsEmoji} browserSupportsEmoji Reference to browserSupportsEmoji function, needed due to minification.
* @param {EmojiSetsRenderIdentically} emojiSetsRenderIdentically Reference to emojiSetsRenderIdentically function, needed due to minification.
* @param {EmojiRendersEmptyCenterPoint} emojiRendersEmptyCenterPoint Reference to emojiRendersEmptyCenterPoint function, needed due to minification.
*
* @return {SupportTests} Support tests.
*/
function testEmojiSupports( tests, browserSupportsEmoji, emojiSetsRenderIdentically, emojiRendersEmptyCenterPoint ) {
/** @type {?EmojiTestContext} */
let context;
if (
typeof WorkerGlobalScope !== 'undefined' &&
self instanceof WorkerGlobalScope
) {
// Dimensions are default for HTMLCanvasElement.
context = new OffscreenCanvas( 300, 150 ).getContext( '2d', { willReadFrequently: true } );
} else {
context = document.createElement( 'canvas' ).getContext( '2d', { willReadFrequently: true } );
}
if ( ! context ) {
throw new Error( 'Unable to obtain a 2D context for the emoji support tests.' );
}
/*
* Chrome on OS X added native emoji rendering in M41. Unfortunately,
* it doesn't work when the font is bolder than 500 weight. So, we
* check for bold rendering support to avoid invisible emoji in Chrome.
*/
context.textBaseline = 'top';
context.font = '600 32px Arial';
const supports = /** @type {SupportTests} */ ( {} );
tests.forEach( ( test ) => {
supports[ test ] = browserSupportsEmoji( context, test, emojiSetsRenderIdentically, emojiRendersEmptyCenterPoint );
} );
return supports;
}
/**
* Adds a script to the head of the document.
*
* @ignore
*
* @since 4.2.0
*
* @param {string} src The url where the script is located.
*
* @return {void}
*/
function addScript( src ) {
const script = document.createElement( 'script' );
script.src = src;
script.defer = true;
document.head.appendChild( script );
}
settings.supports = {
everything: true,
everythingExceptFlag: true
};
// Obtain the emoji support from the browser, asynchronously when possible.
/** @type {Promise<SupportTests>} */
const supportTestsPromise = new Promise( ( resolve ) => {
const sessionSupportTests = getSessionSupportTests();
if ( sessionSupportTests ) {
resolve( sessionSupportTests );
return;
}
if ( supportsWorkerOffloading() ) {
try {
// Note that the functions are being passed as arguments due to minification.
const workerScript =
'postMessage(' +
testEmojiSupports.toString() +
'(' +
[
JSON.stringify( tests ),
browserSupportsEmoji.toString(),
emojiSetsRenderIdentically.toString(),
emojiRendersEmptyCenterPoint.toString()
].join( ',' ) +
'));';
const blob = new Blob( [ workerScript ], {
type: 'text/javascript'
} );
const worker = new Worker( URL.createObjectURL( blob ), { name: 'wpTestEmojiSupports' } );
worker.onmessage = ( /** @type {MessageEvent<SupportTests>} */ event ) => {
setSessionSupportTests( event.data );
worker.terminate();
resolve( event.data );
};
return;
} catch ( e ) {}
}
const testedSupportTests = testEmojiSupports( tests, browserSupportsEmoji, emojiSetsRenderIdentically, emojiRendersEmptyCenterPoint );
setSessionSupportTests( testedSupportTests );
resolve( testedSupportTests );
} );
supportTestsPromise
// Once the browser emoji support has been obtained from the session, finalize the settings.
.then( ( supportTests ) => {
/*
* Tests the browser support for flag emojis and other emojis, and adjusts the
* support settings accordingly.
*/
for ( const test in supportTests ) {
const key = /** @type {keyof SupportTests} */ ( test );
const supported = supportTests[ key ];
settings.supports[ key ] = supported;
settings.supports.everything =
settings.supports.everything && supported;
if ( 'flag' !== key ) {
settings.supports.everythingExceptFlag =
settings.supports.everythingExceptFlag && supported;
}
}
settings.supports.everythingExceptFlag =
settings.supports.everythingExceptFlag &&
! settings.supports.flag;
// When the browser can not render everything we need to load a polyfill.
if ( ! settings.supports.everything ) {
const src = settings.source || {};
if ( src.concatemoji ) {
addScript( src.concatemoji );
} else if ( src.wpemoji && src.twemoji ) {
addScript( src.twemoji );
addScript( src.wpemoji );
}
}
} );
The same content is in 7 releases, WordPress 7.2-beta1 to 7.2-RC2. See them all, or every version of this file.