Changeset 43360
- Timestamp:
- 06/16/2018 12:53:07 PM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/js/_enqueues/wp/emoji.js
r43347 r43360 1 1 /** 2 * wp-emoji.js is used to replace emoji with images in browsers when the browser 3 * doesn't support emoji natively. 4 * 2 5 * @output wp-includes/js/wp-emoji.js 3 6 */ 4 7 5 8 ( 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 */ 6 22 function wpEmoji() { 7 23 var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver, … … 20 36 * 21 37 * @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. 24 44 */ 25 45 function browserSupportsSvgAsImage() { 26 46 if ( !! document.implementation.hasFeature ) { 27 // Source: Modernizr28 // https://github.com/Modernizr/Modernizr/blob/master/feature-detects/svg/asimg.js29 47 return document.implementation.hasFeature( 'http://www.w3.org/TR/SVG11/feature#Image', '1.1' ); 30 48 } … … 36 54 37 55 /** 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. 39 61 * 40 62 * @since 4.2.0 63 * @private 41 64 */ 42 65 function load() { … … 45 68 } 46 69 70 // Ensure twemoji is available on the global window before proceeding. 47 71 if ( typeof window.twemoji === 'undefined' ) { 48 72 // Break if waiting for longer than 30 sec. … … 62 86 loaded = true; 63 87 88 // Initialize the mutation observer, which checks all added nodes for 89 // replaceable emoji characters. 64 90 if ( MutationObserver ) { 65 91 new MutationObserver( function( mutationRecords ) { … … 72 98 ii = addedNodes.length; 73 99 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 */ 74 110 if ( 75 111 ii === 1 && removedNodes.length === 1 && … … 82 118 } 83 119 120 // Loop through all the added nodes. 84 121 while ( ii-- ) { 85 122 node = addedNodes[ ii ]; 86 123 124 // Node type 3 is a TEXT_NODE. 87 125 if ( node.nodeType === 3 ) { 88 126 if ( ! node.parentNode ) { … … 96 134 * template interpolation symbol ( "{{", for example ). So, we 97 135 * join the text nodes back together as a work-around. 136 * 137 * Node type 3 is a TEXT_NODE. 98 138 */ 99 139 while( node.nextSibling && 3 === node.nextSibling.nodeType ) { … … 106 146 } 107 147 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 */ 108 153 if ( ! node || node.nodeType !== 1 || 109 154 ( node.className && typeof node.className === 'string' && node.className.indexOf( 'wp-exclude-emoji' ) !== -1 ) ) { … … 127 172 128 173 /** 129 * Test if a text string contains emoji characters.174 * Tests if a text string contains emoji characters. 130 175 * 131 176 * @since 4.3.0 132 177 * 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. 136 183 */ 137 184 function test( text ) { … … 149 196 150 197 /** 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. 152 203 * 153 204 * @since 4.2.0 154 205 * 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. 157 213 */ 158 214 function parse( object, args ) { 159 215 var params; 160 216 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 */ 161 221 if ( settings.supports.everything || ! twemoji || ! object || 162 222 ( 'string' !== typeof object && ( ! object.childNodes || ! object.childNodes.length ) ) ) { … … 165 225 } 166 226 227 // Compose the params for the twitter emoji library. 167 228 args = args || {}; 168 229 params = { … … 228 289 229 290 window.wp = window.wp || {}; 291 292 /** 293 * @namespace wp.emoji 294 */ 230 295 window.wp.emoji = new wpEmoji(); 231 296
Note: See TracChangeset
for help on using the changeset viewer.