Make WordPress Core

Changeset 43360


Ignore:
Timestamp:
06/16/2018 12:53:07 PM (8 years ago)
Author:
atimmer
Message:

Docs: Improve JSDoc for emoji.js.

Props lisannekluitmans, hansjovisyoast, igorsch, nicollle.
Fixes #44367.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/js/_enqueues/wp/emoji.js

    r43347 r43360  
    11/**
     2 * wp-emoji.js is used to replace emoji with images in browsers when the browser
     3 * doesn't support emoji natively.
     4 *
    25 * @output wp-includes/js/wp-emoji.js
    36 */
    47
    58( function( window, settings ) {
     9        /**
     10         * Replaces emoji with images when browsers don't support emoji.
     11         *
     12         * @since      4.2.0
     13         * @access     private
     14         *
     15         * @class
     16         *
     17         * @see  Twitter Emoji library
     18         * @link https://github.com/twitter/twemoji
     19         *
     20         * @return {Object} The wpEmoji parse and test functions.
     21         */
    622        function wpEmoji() {
    723                var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver,
     
    2036                 *
    2137                 * @since 4.6.0
    22                  *
    23                  * @return {Boolean} True if the browser supports svg, false if not.
     38                 * @private
     39                 *
     40                 * @see Modernizr
     41                 * @link https://github.com/Modernizr/Modernizr/blob/master/feature-detects/svg/asimg.js
     42                 *
     43                 * @return {boolean} True if the browser supports svg, false if not.
    2444                 */
    2545                function browserSupportsSvgAsImage() {
    2646                        if ( !! document.implementation.hasFeature ) {
    27                                 // Source: Modernizr
    28                                 // https://github.com/Modernizr/Modernizr/blob/master/feature-detects/svg/asimg.js
    2947                                return document.implementation.hasFeature( 'http://www.w3.org/TR/SVG11/feature#Image', '1.1' );
    3048                        }
     
    3654
    3755                /**
    38                  * Runs when the document load event is fired, so we can do our first parse of the page.
     56                 * Runs when the document load event is fired, so we can do our first parse of
     57                 * the page.
     58                 *
     59                 * Listens to all the DOM mutations and checks for added nodes that contain
     60                 * emoji characters and replaces those with twitter emoji images.
    3961                 *
    4062                 * @since 4.2.0
     63                 * @private
    4164                 */
    4265                function load() {
     
    4568                        }
    4669
     70                        // Ensure twemoji is available on the global window before proceeding.
    4771                        if ( typeof window.twemoji === 'undefined' ) {
    4872                                // Break if waiting for longer than 30 sec.
     
    6286                        loaded = true;
    6387
     88                        // Initialize the mutation observer, which checks all added nodes for
     89                        // replaceable emoji characters.
    6490                        if ( MutationObserver ) {
    6591                                new MutationObserver( function( mutationRecords ) {
     
    7298                                                ii = addedNodes.length;
    7399
     100                                                /*
     101                                                 * Checks if an image has been replaced by a text element
     102                                                 * with the same text as the alternate description of the replaced image.
     103                                                 * (presumably because the image could not be loaded).
     104                                                 * If it is, do absolutely nothing.
     105                                                 *
     106                                                 * Node type 3 is a TEXT_NODE.
     107                                                 *
     108                                                 * @link https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeType
     109                                                 */
    74110                                                if (
    75111                                                        ii === 1 && removedNodes.length === 1 &&
     
    82118                                                }
    83119
     120                                                // Loop through all the added nodes.
    84121                                                while ( ii-- ) {
    85122                                                        node = addedNodes[ ii ];
    86123
     124                                                        // Node type 3 is a TEXT_NODE.
    87125                                                        if ( node.nodeType === 3 ) {
    88126                                                                if ( ! node.parentNode ) {
     
    96134                                                                         * template interpolation symbol ( "{{", for example ). So, we
    97135                                                                         * join the text nodes back together as a work-around.
     136                                                                         *
     137                                                                         * Node type 3 is a TEXT_NODE.
    98138                                                                         */
    99139                                                                        while( node.nextSibling && 3 === node.nextSibling.nodeType ) {
     
    106146                                                        }
    107147
     148                                                        /*
     149                                                         * If the class name of a non-element node contains 'wp-exclude-emoji' ignore it.
     150                                                         *
     151                                                         * Node type 1 is an ELEMENT_NODE.
     152                                                         */
    108153                                                        if ( ! node || node.nodeType !== 1 ||
    109154                                                                ( node.className && typeof node.className === 'string' && node.className.indexOf( 'wp-exclude-emoji' ) !== -1 ) ) {
     
    127172
    128173                /**
    129                  * Test if a text string contains emoji characters.
     174                 * Tests if a text string contains emoji characters.
    130175                 *
    131176                 * @since 4.3.0
    132177                 *
    133                  * @param {String} text The string to test
    134                  *
    135                  * @return {Boolean} Whether the string contains emoji characters.
     178                 * @memberOf wp.emoji
     179                 *
     180                 * @param {string} text The string to test.
     181                 *
     182                 * @return {boolean} Whether the string contains emoji characters.
    136183                 */
    137184                function test( text ) {
     
    149196
    150197                /**
    151                  * Given an element or string, parse any emoji characters into Twemoji images.
     198                 * Parses any emoji characters into Twemoji images.
     199                 *
     200                 * - When passed an element the emoji characters are replaced inline.
     201                 * - When passed a string the emoji characters are replaced and the result is
     202                 *   returned.
    152203                 *
    153204                 * @since 4.2.0
    154205                 *
    155                  * @param {HTMLElement|String} object The element or string to parse.
    156                  * @param {Object} args Additional options for Twemoji.
     206                 * @memberOf wp.emoji
     207                 *
     208                 * @param {HTMLElement|string} object The element or string to parse.
     209                 * @param {Object}             args   Additional options for Twemoji.
     210                 *
     211                 * @return {HTMLElement|string} A string where all emoji are now image tags of
     212                 *                              emoji. Or the element that was passed as the first argument.
    157213                 */
    158214                function parse( object, args ) {
    159215                        var params;
    160216
     217                        /*
     218                         * If the browser has full support, twemoji is not loaded or our
     219                         * object is not what was expected, we do not parse anything.
     220                         */
    161221                        if ( settings.supports.everything || ! twemoji || ! object ||
    162222                                ( 'string' !== typeof object && ( ! object.childNodes || ! object.childNodes.length ) ) ) {
     
    165225                        }
    166226
     227                        // Compose the params for the twitter emoji library.
    167228                        args = args || {};
    168229                        params = {
     
    228289
    229290        window.wp = window.wp || {};
     291
     292        /**
     293         * @namespace wp.emoji
     294         */
    230295        window.wp.emoji = new wpEmoji();
    231296
Note: See TracChangeset for help on using the changeset viewer.