]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/js/wp-emoji.js
WordPress 4.2.1-scripts
[autoinstalls/wordpress.git] / wp-includes / js / wp-emoji.js
1
2 ( function( window, settings ) {
3         function wpEmoji() {
4                 var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver,
5
6                 /**
7                  * Flag to determine if the browser and the OS support emoji.
8                  *
9                  * @since 4.2.0
10                  *
11                  * @var Boolean
12                  */
13                 supportsEmoji = false,
14
15                 /**
16                  * Flag to determine if the browser and the OS support flag (two character) emoji.
17                  *
18                  * @since 4.2.0
19                  *
20                  * @var Boolean
21                  */
22                 supportsFlagEmoji = false,
23
24                 /**
25                  * Flag to determine if we should replace emoji characters with images.
26                  *
27                  * @since 4.2.0
28                  *
29                  * @var Boolean
30                  */
31                 replaceEmoji = false,
32
33                 isIE8 = window.navigator.userAgent.indexOf( 'IE 8' ) !== -1,
34
35                 // Private
36                 twemoji, timer,
37                 count = 0;
38
39                 /**
40                  * Runs when the document load event is fired, so we can do our first parse of the page.
41                  *
42                  * @since 4.2.0
43                  */
44                 function load() {
45                         if ( typeof window.twemoji === 'undefined' ) {
46                                 // Break if waiting for longer than 30 sec.
47                                 if ( count > 600 ) {
48                                         return;
49                                 }
50
51                                 // Still waiting.
52                                 window.clearTimeout( timer );
53                                 timer = window.setTimeout( load, 50 );
54                                 count++;
55
56                                 return;
57                         }
58
59                         twemoji = window.twemoji;
60
61                         if ( MutationObserver ) {
62                                 new MutationObserver( function( mutationRecords ) {
63                                         var i = mutationRecords.length,
64                                                 ii, node;
65
66                                         while ( i-- ) {
67                                                 ii = mutationRecords[ i ].addedNodes.length;
68
69                                                 while ( ii-- ) {
70                                                         node = mutationRecords[ i ].addedNodes[ ii ];
71
72                                                         if ( node.nodeType === 3 ) {
73                                                                 node = node.parentNode;
74                                                         }
75
76                                                         if ( node && node.nodeType === 1 ) {
77                                                                 parse( node );
78                                                         }
79                                                 }
80                                         }
81                                 } ).observe( document.body, {
82                                         childList: true,
83                                         subtree: true
84                                 } );
85                         }
86
87                         parse( document.body );
88                 }
89
90                 /**
91                  * Given an element or string, parse any emoji characters into Twemoji images.
92                  *
93                  * @since 4.2.0
94                  *
95                  * @param {HTMLElement|String} object The element or string to parse.
96                  * @param {Object} args Additional options for Twemoji.
97                  */
98                 function parse( object, args ) {
99                         if ( ! replaceEmoji ) {
100                                 return object;
101                         }
102
103                         args = args || {};
104
105                         return twemoji.parse( object, {
106                                 base: settings.baseUrl,
107                                 ext: settings.ext,
108                                 className: args.className || 'emoji',
109                                 imgAttr: args.imgAttr,
110                                 callback: function( icon, options ) {
111                                         // Ignore some standard characters that TinyMCE recommends in its character map.
112                                         switch ( icon ) {
113                                                 case 'a9':
114                                                 case 'ae':
115                                                 case '2122':
116                                                 case '2194':
117                                                 case '2660':
118                                                 case '2663':
119                                                 case '2665':
120                                                 case '2666':
121                                                         return false;
122                                         }
123
124                                         if ( ! supportsFlagEmoji && supportsEmoji &&
125                                                 ! /^1f1(?:e[6-9a-f]|f[0-9a-f])-1f1(?:e[6-9a-f]|f[0-9a-f])$/.test( icon ) ) {
126
127                                                 return false;
128                                         }
129
130                                         return ''.concat( options.base, icon, options.ext );
131                                 }
132                         } );
133                 }
134
135                 // Load when the readyState changes to 'interactive', not 'complete'.
136                 function onLoad() {
137                         if ( ( ! isIE8 && 'interactive' === document.readyState ) || ( isIE8 && 'complete' === document.readyState ) ) {
138                                 load();
139                         }
140                 }
141
142                 /**
143                  * Initialize our emoji support, and set up listeners.
144                  */
145                 if ( settings ) {
146                         supportsEmoji = window._wpemojiSettings.supports.simple;
147                         supportsFlagEmoji = window._wpemojiSettings.supports.flag;
148                         replaceEmoji = ! supportsEmoji || ! supportsFlagEmoji;
149
150                         if ( ( ! isIE8 && 'loading' === document.readyState ) || ( isIE8 && 'complete' !== document.readyState ) ) {
151                                 if ( document.addEventListener ) {
152                                         document.addEventListener( 'readystatechange', onLoad, false );
153                                 } else if ( document.attachEvent ) {
154                                         document.attachEvent( 'onreadystatechange', onLoad );
155                                 }
156                         } else {
157                                 load();
158                         }
159                 }
160
161                 return {
162                         replaceEmoji: replaceEmoji,
163                         parse: parse
164                 };
165         }
166
167         window.wp = window.wp || {};
168         window.wp.emoji = new wpEmoji();
169
170 } )( window, window._wpemojiSettings );