3 function SwitchEditors() {
8 if ( ! tinymce && window.tinymce ) {
9 tinymce = window.tinymce;
12 $$( document ).on( 'click', function( event ) {
14 target = $$( event.target );
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 );
25 function getToolbarHeight( editor ) {
26 var node = $$( '.mce-toolbar-grp', editor.getContainer() )[0],
27 height = node && node.clientHeight;
29 if ( height && height > 10 && height < 200 ) {
30 return parseInt( height, 10 );
36 function switchEditor( id, mode ) {
38 mode = mode || 'toggle';
40 var editorHeight, toolbarHeight, iframe,
41 editor = tinymce.get( id ),
42 wrap = $$( '#wp-' + id + '-wrap' ),
43 $textarea = $$( '#' + id ),
44 textarea = $textarea[0];
46 if ( 'toggle' === mode ) {
47 if ( editor && ! editor.isHidden() ) {
54 if ( 'tmce' === mode || 'tinymce' === mode ) {
55 if ( editor && ! editor.isHidden() ) {
59 if ( typeof( window.QTags ) !== 'undefined' ) {
60 window.QTags.closeAllTags( id );
63 editorHeight = parseInt( textarea.style.height, 10 ) || 0;
68 // No point resizing the iframe in iOS
69 if ( ! tinymce.Env.iOS && editorHeight ) {
70 toolbarHeight = getToolbarHeight( editor );
71 editorHeight = editorHeight - toolbarHeight + 14;
73 // height cannot be under 50 or over 5000
74 if ( editorHeight > 50 && editorHeight < 5000 ) {
75 editor.theme.resizeTo( null, editorHeight );
79 tinymce.init( window.tinyMCEPreInit.mceInit[id] );
82 wrap.removeClass( 'html-active' ).addClass( 'tmce-active' );
83 $textarea.attr( 'aria-hidden', true );
84 window.setUserSetting( 'editor', 'tinymce' );
86 } else if ( 'html' === mode ) {
87 if ( editor && editor.isHidden() ) {
92 if ( ! tinymce.Env.iOS ) {
93 iframe = editor.iframeElement;
94 editorHeight = iframe ? parseInt( iframe.style.height, 10 ) : 0;
97 toolbarHeight = getToolbarHeight( editor );
98 editorHeight = editorHeight + toolbarHeight - 14;
100 // height cannot be under 50 or over 5000
101 if ( editorHeight > 50 && editorHeight < 5000 ) {
102 textarea.style.height = editorHeight + 'px';
109 // The TinyMCE instance doesn't exist, show the textarea
110 $textarea.css({ 'display': '', 'visibility': '' });
113 wrap.removeClass( 'tmce-active' ).addClass( 'html-active' );
114 $textarea.attr( 'aria-hidden', false );
115 window.setUserSetting( 'editor', 'html' );
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,
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>' );
141 // keep <br> tags inside captions and remove line breaks
142 if ( html.indexOf( '[caption' ) !== -1 ) {
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]+/, '' );
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>' );
153 // Mark </p> if it has any attributes.
154 html = html.replace( /(<p [^>]+>.*?)<\/p>/g, '$1</p#>' );
156 // Separate <div> containing <p>
157 html = html.replace( /<div( [^>]*)?>\s*<p>/gi, '<div$1>\n\n' );
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' );
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' );
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>' );
175 if ( html.indexOf( '<option' ) !== -1 ) {
176 html = html.replace( /\s*<option/g, '\n<option' );
177 html = html.replace( /\s*<\/select>/g, '\n</select>' );
180 if ( html.indexOf( '<hr' ) !== -1 ) {
181 html = html.replace( /\s*<hr( [^>]*)?>\s*/g, '\n\n<hr$1>\n\n' );
184 if ( html.indexOf( '<object' ) !== -1 ) {
185 html = html.replace( /<object[\s\S]+?<\/object>/g, function( a ) {
186 return a.replace( /[\r\n]+/g, '' );
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' );
195 html = html.replace( /^\s+/, '' );
196 html = html.replace( /[\s\u00a0]+$/, '' );
198 // put back the line breaks in pre|script
199 if ( preserve_linebreaks ) {
200 html = html.replace( /<wp-line-break>/g, '\n' );
203 // and the <br> tags in captions
205 html = html.replace( /<wp-temp-br([^>]*)>/g, '<br$1>' );
211 // Similar to `wpautop()` in formatting.php
212 function autop( text ) {
213 var preserve_linebreaks = 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';
219 // Normalize line breaks
220 text = text.replace( /\r\n|\r/g, '\n' );
222 if ( text.indexOf( '\n' ) === -1 ) {
226 if ( text.indexOf( '<object' ) !== -1 ) {
227 text = text.replace( /<object[\s\S]+?<\/object>/g, function( a ) {
228 return a.replace( /\n+/g, '' );
232 text = text.replace( /<[^<>]+>/g, function( a ) {
233 return a.replace( /[\n\t ]+/g, ' ' );
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>' );
244 // keep <br> tags inside captions and convert line breaks
245 if ( text.indexOf( '[caption' ) !== -1 ) {
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 ]+/, ' ' );
254 // convert remaining line breaks to <br>
255 return a.replace( /\s*\n\s*/g, '<wp-temp-br />' );
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' );
276 // Remove redundant spaces and line breaks after existing <br /> tags
277 text = text.replace( /(<br[^>]*>)\s*\n/gi, '$1' );
279 // Create <br /> from the remaining line breaks
280 text = text.replace( /\s*\n/g, '<br />\n');
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]' );
286 text = text.replace( /(<(?:div|th|td|form|fieldset|dd)[^>]*>)(.*?)<\/p>/g, function( a, b, c ) {
287 if ( c.match( /<p( [^>]*)?>/ ) ) {
291 return b + '<p>' + c + '</p>';
294 // put back the line breaks in pre|script
295 if ( preserve_linebreaks ) {
296 text = text.replace( /<wp-line-break>/g, '\n' );
300 text = text.replace( /<wp-temp-br([^>]*)>/g, '<br$1>' );
307 function pre_wpautop( html ) {
308 var obj = { o: exports, data: html, unfiltered: html };
311 $( 'body' ).trigger( 'beforePreWpautop', [ obj ] );
314 obj.data = removep( obj.data );
317 $( 'body' ).trigger( 'afterPreWpautop', [ obj ] );
323 function wpautop( text ) {
324 var obj = { o: exports, data: text, unfiltered: text };
327 $( 'body' ).trigger( 'beforeWpautop', [ obj ] );
330 obj.data = autop( obj.data );
333 $( 'body' ).trigger( 'afterWpautop', [ obj ] );
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 ) {
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;
361 pre_wpautop: pre_wpautop,
369 window.switchEditors = new SwitchEditors();