]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/js/editor.js
WordPress 4.4
[autoinstalls/wordpress.git] / wp-admin / js / editor.js
1
2 ( function( $ ) {
3         function SwitchEditors() {
4                 var tinymce, $$,
5                         exports = {};
6
7                 function init() {
8                         if ( ! tinymce && window.tinymce ) {
9                                 tinymce = window.tinymce;
10                                 $$ = tinymce.$;
11
12                                 $$( document ).on( 'click', function( event ) {
13                                         var id, mode,
14                                                 target = $$( event.target );
15
16                                         if ( target.hasClass( 'wp-switch-editor' ) ) {
17                                                 id = target.attr( 'data-wp-editor-id' );
18                                                 mode = target.hasClass( 'switch-tmce' ) ? 'tmce' : 'html';
19                                                 switchEditor( id, mode );
20                                         }
21                                 });
22                         }
23                 }
24
25                 function getToolbarHeight( editor ) {
26                         var node = $$( '.mce-toolbar-grp', editor.getContainer() )[0],
27                                 height = node && node.clientHeight;
28
29                         if ( height && height > 10 && height < 200 ) {
30                                 return parseInt( height, 10 );
31                         }
32
33                         return 30;
34                 }
35
36                 function switchEditor( id, mode ) {
37                         id = id || 'content';
38                         mode = mode || 'toggle';
39
40                         var editorHeight, toolbarHeight, iframe,
41                                 editor = tinymce.get( id ),
42                                 wrap = $$( '#wp-' + id + '-wrap' ),
43                                 $textarea = $$( '#' + id ),
44                                 textarea = $textarea[0];
45
46                         if ( 'toggle' === mode ) {
47                                 if ( editor && ! editor.isHidden() ) {
48                                         mode = 'html';
49                                 } else {
50                                         mode = 'tmce';
51                                 }
52                         }
53
54                         if ( 'tmce' === mode || 'tinymce' === mode ) {
55                                 if ( editor && ! editor.isHidden() ) {
56                                         return false;
57                                 }
58
59                                 if ( typeof( window.QTags ) !== 'undefined' ) {
60                                         window.QTags.closeAllTags( id );
61                                 }
62
63                                 editorHeight = parseInt( textarea.style.height, 10 ) || 0;
64
65                                 if ( editor ) {
66                                         editor.show();
67
68                                         // No point resizing the iframe in iOS
69                                         if ( ! tinymce.Env.iOS && editorHeight ) {
70                                                 toolbarHeight = getToolbarHeight( editor );
71                                                 editorHeight = editorHeight - toolbarHeight + 14;
72
73                                                 // height cannot be under 50 or over 5000
74                                                 if ( editorHeight > 50 && editorHeight < 5000 ) {
75                                                         editor.theme.resizeTo( null, editorHeight );
76                                                 }
77                                         }
78                                 } else {
79                                         tinymce.init( window.tinyMCEPreInit.mceInit[id] );
80                                 }
81
82                                 wrap.removeClass( 'html-active' ).addClass( 'tmce-active' );
83                                 $textarea.attr( 'aria-hidden', true );
84                                 window.setUserSetting( 'editor', 'tinymce' );
85
86                         } else if ( 'html' === mode ) {
87                                 if ( editor && editor.isHidden() ) {
88                                         return false;
89                                 }
90
91                                 if ( editor ) {
92                                         if ( ! tinymce.Env.iOS ) {
93                                                 iframe = editor.iframeElement;
94                                                 editorHeight = iframe ? parseInt( iframe.style.height, 10 ) : 0;
95
96                                                 if ( editorHeight ) {
97                                                         toolbarHeight = getToolbarHeight( editor );
98                                                         editorHeight = editorHeight + toolbarHeight - 14;
99
100                                                         // height cannot be under 50 or over 5000
101                                                         if ( editorHeight > 50 && editorHeight < 5000 ) {
102                                                                 textarea.style.height = editorHeight + 'px';
103                                                         }
104                                                 }
105                                         }
106
107                                         editor.hide();
108                                 } else {
109                                         // The TinyMCE instance doesn't exist, show the textarea
110                                         $textarea.css({ 'display': '', 'visibility': '' });
111                                 }
112
113                                 wrap.removeClass( 'tmce-active' ).addClass( 'html-active' );
114                                 $textarea.attr( 'aria-hidden', false );
115                                 window.setUserSetting( 'editor', 'html' );
116                         }
117                 }
118
119                 // Replace paragraphs with double line breaks
120                 function removep( html ) {
121                         var blocklist = 'blockquote|ul|ol|li|table|thead|tbody|tfoot|tr|th|td|h[1-6]|fieldset',
122                                 blocklist1 = blocklist + '|div|p',
123                                 blocklist2 = blocklist + '|pre',
124                                 preserve_linebreaks = false,
125                                 preserve_br = false;
126
127                         if ( ! html ) {
128                                 return '';
129                         }
130
131                         // Protect pre|script tags
132                         if ( html.indexOf( '<pre' ) !== -1 || html.indexOf( '<script' ) !== -1 ) {
133                                 preserve_linebreaks = true;
134                                 html = html.replace( /<(pre|script)[^>]*>[\s\S]+?<\/\1>/g, function( a ) {
135                                         a = a.replace( /<br ?\/?>(\r\n|\n)?/g, '<wp-line-break>' );
136                                         a = a.replace( /<\/?p( [^>]*)?>(\r\n|\n)?/g, '<wp-line-break>' );
137                                         return a.replace( /\r?\n/g, '<wp-line-break>' );
138                                 });
139                         }
140
141                         // keep <br> tags inside captions and remove line breaks
142                         if ( html.indexOf( '[caption' ) !== -1 ) {
143                                 preserve_br = true;
144                                 html = html.replace( /\[caption[\s\S]+?\[\/caption\]/g, function( a ) {
145                                         return a.replace( /<br([^>]*)>/g, '<wp-temp-br$1>' ).replace( /[\r\n\t]+/, '' );
146                                 });
147                         }
148
149                         // Pretty it up for the source editor
150                         html = html.replace( new RegExp( '\\s*</(' + blocklist1 + ')>\\s*', 'g' ), '</$1>\n' );
151                         html = html.replace( new RegExp( '\\s*<((?:' + blocklist1 + ')(?: [^>]*)?)>', 'g' ), '\n<$1>' );
152
153                         // Mark </p> if it has any attributes.
154                         html = html.replace( /(<p [^>]+>.*?)<\/p>/g, '$1</p#>' );
155
156                         // Separate <div> containing <p>
157                         html = html.replace( /<div( [^>]*)?>\s*<p>/gi, '<div$1>\n\n' );
158
159                         // Remove <p> and <br />
160                         html = html.replace( /\s*<p>/gi, '' );
161                         html = html.replace( /\s*<\/p>\s*/gi, '\n\n' );
162                         html = html.replace( /\n[\s\u00a0]+\n/g, '\n\n' );
163                         html = html.replace( /\s*<br ?\/?>\s*/gi, '\n' );
164
165                         // Fix some block element newline issues
166                         html = html.replace( /\s*<div/g, '\n<div' );
167                         html = html.replace( /<\/div>\s*/g, '</div>\n' );
168                         html = html.replace( /\s*\[caption([^\[]+)\[\/caption\]\s*/gi, '\n\n[caption$1[/caption]\n\n' );
169                         html = html.replace( /caption\]\n\n+\[caption/g, 'caption]\n\n[caption' );
170
171                         html = html.replace( new RegExp('\\s*<((?:' + blocklist2 + ')(?: [^>]*)?)\\s*>', 'g' ), '\n<$1>' );
172                         html = html.replace( new RegExp('\\s*</(' + blocklist2 + ')>\\s*', 'g' ), '</$1>\n' );
173                         html = html.replace( /<li([^>]*)>/g, '\t<li$1>' );
174
175                         if ( html.indexOf( '<option' ) !== -1 ) {
176                                 html = html.replace( /\s*<option/g, '\n<option' );
177                                 html = html.replace( /\s*<\/select>/g, '\n</select>' );
178                         }
179
180                         if ( html.indexOf( '<hr' ) !== -1 ) {
181                                 html = html.replace( /\s*<hr( [^>]*)?>\s*/g, '\n\n<hr$1>\n\n' );
182                         }
183
184                         if ( html.indexOf( '<object' ) !== -1 ) {
185                                 html = html.replace( /<object[\s\S]+?<\/object>/g, function( a ) {
186                                         return a.replace( /[\r\n]+/g, '' );
187                                 });
188                         }
189
190                         // Unmark special paragraph closing tags
191                         html = html.replace( /<\/p#>/g, '</p>\n' );
192                         html = html.replace( /\s*(<p [^>]+>[\s\S]*?<\/p>)/g, '\n$1' );
193
194                         // Trim whitespace
195                         html = html.replace( /^\s+/, '' );
196                         html = html.replace( /[\s\u00a0]+$/, '' );
197
198                         // put back the line breaks in pre|script
199                         if ( preserve_linebreaks ) {
200                                 html = html.replace( /<wp-line-break>/g, '\n' );
201                         }
202
203                         // and the <br> tags in captions
204                         if ( preserve_br ) {
205                                 html = html.replace( /<wp-temp-br([^>]*)>/g, '<br$1>' );
206                         }
207
208                         return html;
209                 }
210
211                 // Similar to `wpautop()` in formatting.php
212                 function autop( text ) {
213                         var preserve_linebreaks = false,
214                                 preserve_br = false,
215                                 blocklist = 'table|thead|tfoot|caption|col|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre' +
216                                         '|form|map|area|blockquote|address|math|style|p|h[1-6]|hr|fieldset|legend|section' +
217                                         '|article|aside|hgroup|header|footer|nav|figure|figcaption|details|menu|summary';
218
219                         // Normalize line breaks
220                         text = text.replace( /\r\n|\r/g, '\n' );
221
222                         if ( text.indexOf( '\n' ) === -1 ) {
223                                 return text;
224                         }
225
226                         if ( text.indexOf( '<object' ) !== -1 ) {
227                                 text = text.replace( /<object[\s\S]+?<\/object>/g, function( a ) {
228                                         return a.replace( /\n+/g, '' );
229                                 });
230                         }
231
232                         text = text.replace( /<[^<>]+>/g, function( a ) {
233                                 return a.replace( /[\n\t ]+/g, ' ' );
234                         });
235
236                         // Protect pre|script tags
237                         if ( text.indexOf( '<pre' ) !== -1 || text.indexOf( '<script' ) !== -1 ) {
238                                 preserve_linebreaks = true;
239                                 text = text.replace( /<(pre|script)[^>]*>[\s\S]*?<\/\1>/g, function( a ) {
240                                         return a.replace( /\n/g, '<wp-line-break>' );
241                                 });
242                         }
243
244                         // keep <br> tags inside captions and convert line breaks
245                         if ( text.indexOf( '[caption' ) !== -1 ) {
246                                 preserve_br = true;
247                                 text = text.replace( /\[caption[\s\S]+?\[\/caption\]/g, function( a ) {
248                                         // keep existing <br>
249                                         a = a.replace( /<br([^>]*)>/g, '<wp-temp-br$1>' );
250                                         // no line breaks inside HTML tags
251                                         a = a.replace( /<[^<>]+>/g, function( b ) {
252                                                 return b.replace( /[\n\t ]+/, ' ' );
253                                         });
254                                         // convert remaining line breaks to <br>
255                                         return a.replace( /\s*\n\s*/g, '<wp-temp-br />' );
256                                 });
257                         }
258
259                         text = text + '\n\n';
260                         text = text.replace( /<br \/>\s*<br \/>/gi, '\n\n' );
261                         text = text.replace( new RegExp( '(<(?:' + blocklist + ')(?: [^>]*)?>)', 'gi' ), '\n$1' );
262                         text = text.replace( new RegExp( '(</(?:' + blocklist + ')>)', 'gi' ), '$1\n\n' );
263                         text = text.replace( /<hr( [^>]*)?>/gi, '<hr$1>\n\n' ); // hr is self closing block element
264                         text = text.replace( /\s*<option/gi, '<option' ); // No <p> or <br> around <option>
265                         text = text.replace( /<\/option>\s*/gi, '</option>' );
266                         text = text.replace( /\n\s*\n+/g, '\n\n' );
267                         text = text.replace( /([\s\S]+?)\n\n/g, '<p>$1</p>\n' );
268                         text = text.replace( /<p>\s*?<\/p>/gi, '');
269                         text = text.replace( new RegExp( '<p>\\s*(</?(?:' + blocklist + ')(?: [^>]*)?>)\\s*</p>', 'gi' ), '$1' );
270                         text = text.replace( /<p>(<li.+?)<\/p>/gi, '$1');
271                         text = text.replace( /<p>\s*<blockquote([^>]*)>/gi, '<blockquote$1><p>');
272                         text = text.replace( /<\/blockquote>\s*<\/p>/gi, '</p></blockquote>');
273                         text = text.replace( new RegExp( '<p>\\s*(</?(?:' + blocklist + ')(?: [^>]*)?>)', 'gi' ), '$1' );
274                         text = text.replace( new RegExp( '(</?(?:' + blocklist + ')(?: [^>]*)?>)\\s*</p>', 'gi' ), '$1' );
275
276                         // Remove redundant spaces and line breaks after existing <br /> tags
277                         text = text.replace( /(<br[^>]*>)\s*\n/gi, '$1' );
278
279                         // Create <br /> from the remaining line breaks
280                         text = text.replace( /\s*\n/g, '<br />\n');
281
282                         text = text.replace( new RegExp( '(</?(?:' + blocklist + ')[^>]*>)\\s*<br />', 'gi' ), '$1' );
283                         text = text.replace( /<br \/>(\s*<\/?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)>)/gi, '$1' );
284                         text = text.replace( /(?:<p>|<br ?\/?>)*\s*\[caption([^\[]+)\[\/caption\]\s*(?:<\/p>|<br ?\/?>)*/gi, '[caption$1[/caption]' );
285
286                         text = text.replace( /(<(?:div|th|td|form|fieldset|dd)[^>]*>)(.*?)<\/p>/g, function( a, b, c ) {
287                                 if ( c.match( /<p( [^>]*)?>/ ) ) {
288                                         return a;
289                                 }
290
291                                 return b + '<p>' + c + '</p>';
292                         });
293
294                         // put back the line breaks in pre|script
295                         if ( preserve_linebreaks ) {
296                                 text = text.replace( /<wp-line-break>/g, '\n' );
297                         }
298
299                         if ( preserve_br ) {
300                                 text = text.replace( /<wp-temp-br([^>]*)>/g, '<br$1>' );
301                         }
302
303                         return text;
304                 }
305
306                 // Add old events
307                 function pre_wpautop( html ) {
308                         var obj = { o: exports, data: html, unfiltered: html };
309
310                         if ( $ ) {
311                                 $( 'body' ).trigger( 'beforePreWpautop', [ obj ] );
312                         }
313
314                         obj.data = removep( obj.data );
315
316                         if ( $ ) {
317                                 $( 'body' ).trigger( 'afterPreWpautop', [ obj ] );
318                         }
319
320                         return obj.data;
321                 }
322
323                 function wpautop( text ) {
324                         var obj = { o: exports, data: text, unfiltered: text };
325
326                         if ( $ ) {
327                                 $( 'body' ).trigger( 'beforeWpautop', [ obj ] );
328                         }
329
330                         obj.data = autop( obj.data );
331
332                         if ( $ ) {
333                                 $( 'body' ).trigger( 'afterWpautop', [ obj ] );
334                         }
335
336                         return obj.data;
337                 }
338
339                 if ( $ ) {
340                         $( document ).ready( init );
341                 } else if ( document.addEventListener ) {
342                         document.addEventListener( 'DOMContentLoaded', init, false );
343                         window.addEventListener( 'load', init, false );
344                 } else if ( window.attachEvent ) {
345                         window.attachEvent( 'onload', init );
346                         document.attachEvent( 'onreadystatechange', function() {
347                                 if ( 'complete' === document.readyState ) {
348                                         init();
349                                 }
350                         } );
351                 }
352
353                 window.wp = window.wp || {};
354                 window.wp.editor = window.wp.editor || {};
355                 window.wp.editor.autop = wpautop;
356                 window.wp.editor.removep = pre_wpautop;
357
358                 exports = {
359                         go: switchEditor,
360                         wpautop: wpautop,
361                         pre_wpautop: pre_wpautop,
362                         _wp_Autop: autop,
363                         _wp_Nop: removep
364                 };
365
366                 return exports;
367         }
368
369         window.switchEditors = new SwitchEditors();
370 }( window.jQuery ));