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|dl|dt|dd|table|thead|tbody|tfoot|tr|th|td|h[1-6]|fieldset',
122 blocklist1 = blocklist + '|div|p',
123 blocklist2 = blocklist + '|pre',
124 preserve_linebreaks = false,
132 // Preserve script and style tags.
133 if ( html.indexOf( '<script' ) !== -1 || html.indexOf( '<style' ) !== -1 ) {
134 html = html.replace( /<(script|style)[^>]*>[\s\S]*?<\/\1>/g, function( match ) {
135 preserve.push( match );
136 return '<wp-preserve>';
141 if ( html.indexOf( '<pre' ) !== -1 ) {
142 preserve_linebreaks = true;
143 html = html.replace( /<pre[^>]*>[\s\S]+?<\/pre>/g, function( a ) {
144 a = a.replace( /<br ?\/?>(\r\n|\n)?/g, '<wp-line-break>' );
145 a = a.replace( /<\/?p( [^>]*)?>(\r\n|\n)?/g, '<wp-line-break>' );
146 return a.replace( /\r?\n/g, '<wp-line-break>' );
150 // keep <br> tags inside captions and remove line breaks
151 if ( html.indexOf( '[caption' ) !== -1 ) {
153 html = html.replace( /\[caption[\s\S]+?\[\/caption\]/g, function( a ) {
154 return a.replace( /<br([^>]*)>/g, '<wp-temp-br$1>' ).replace( /[\r\n\t]+/, '' );
158 // Pretty it up for the source editor
159 html = html.replace( new RegExp( '\\s*</(' + blocklist1 + ')>\\s*', 'g' ), '</$1>\n' );
160 html = html.replace( new RegExp( '\\s*<((?:' + blocklist1 + ')(?: [^>]*)?)>', 'g' ), '\n<$1>' );
162 // Mark </p> if it has any attributes.
163 html = html.replace( /(<p [^>]+>.*?)<\/p>/g, '$1</p#>' );
165 // Separate <div> containing <p>
166 html = html.replace( /<div( [^>]*)?>\s*<p>/gi, '<div$1>\n\n' );
168 // Remove <p> and <br />
169 html = html.replace( /\s*<p>/gi, '' );
170 html = html.replace( /\s*<\/p>\s*/gi, '\n\n' );
171 html = html.replace( /\n[\s\u00a0]+\n/g, '\n\n' );
172 html = html.replace( /\s*<br ?\/?>\s*/gi, '\n' );
174 // Fix some block element newline issues
175 html = html.replace( /\s*<div/g, '\n<div' );
176 html = html.replace( /<\/div>\s*/g, '</div>\n' );
177 html = html.replace( /\s*\[caption([^\[]+)\[\/caption\]\s*/gi, '\n\n[caption$1[/caption]\n\n' );
178 html = html.replace( /caption\]\n\n+\[caption/g, 'caption]\n\n[caption' );
180 html = html.replace( new RegExp('\\s*<((?:' + blocklist2 + ')(?: [^>]*)?)\\s*>', 'g' ), '\n<$1>' );
181 html = html.replace( new RegExp('\\s*</(' + blocklist2 + ')>\\s*', 'g' ), '</$1>\n' );
182 html = html.replace( /<((li|dt|dd)[^>]*)>/g, ' \t<$1>' );
184 if ( html.indexOf( '<option' ) !== -1 ) {
185 html = html.replace( /\s*<option/g, '\n<option' );
186 html = html.replace( /\s*<\/select>/g, '\n</select>' );
189 if ( html.indexOf( '<hr' ) !== -1 ) {
190 html = html.replace( /\s*<hr( [^>]*)?>\s*/g, '\n\n<hr$1>\n\n' );
193 if ( html.indexOf( '<object' ) !== -1 ) {
194 html = html.replace( /<object[\s\S]+?<\/object>/g, function( a ) {
195 return a.replace( /[\r\n]+/g, '' );
199 // Unmark special paragraph closing tags
200 html = html.replace( /<\/p#>/g, '</p>\n' );
201 html = html.replace( /\s*(<p [^>]+>[\s\S]*?<\/p>)/g, '\n$1' );
204 html = html.replace( /^\s+/, '' );
205 html = html.replace( /[\s\u00a0]+$/, '' );
207 // put back the line breaks in pre|script
208 if ( preserve_linebreaks ) {
209 html = html.replace( /<wp-line-break>/g, '\n' );
212 // and the <br> tags in captions
214 html = html.replace( /<wp-temp-br([^>]*)>/g, '<br$1>' );
217 // Put back preserved tags.
218 if ( preserve.length ) {
219 html = html.replace( /<wp-preserve>/g, function() {
220 return preserve.shift();
227 // Similar to `wpautop()` in formatting.php
228 function autop( text ) {
229 var preserve_linebreaks = false,
231 blocklist = 'table|thead|tfoot|caption|col|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre' +
232 '|form|map|area|blockquote|address|math|style|p|h[1-6]|hr|fieldset|legend|section' +
233 '|article|aside|hgroup|header|footer|nav|figure|figcaption|details|menu|summary';
235 // Normalize line breaks
236 text = text.replace( /\r\n|\r/g, '\n' );
238 if ( text.indexOf( '\n' ) === -1 ) {
242 if ( text.indexOf( '<object' ) !== -1 ) {
243 text = text.replace( /<object[\s\S]+?<\/object>/g, function( a ) {
244 return a.replace( /\n+/g, '' );
248 text = text.replace( /<[^<>]+>/g, function( a ) {
249 return a.replace( /[\n\t ]+/g, ' ' );
252 // Protect pre|script tags
253 if ( text.indexOf( '<pre' ) !== -1 || text.indexOf( '<script' ) !== -1 ) {
254 preserve_linebreaks = true;
255 text = text.replace( /<(pre|script)[^>]*>[\s\S]*?<\/\1>/g, function( a ) {
256 return a.replace( /\n/g, '<wp-line-break>' );
260 // keep <br> tags inside captions and convert line breaks
261 if ( text.indexOf( '[caption' ) !== -1 ) {
263 text = text.replace( /\[caption[\s\S]+?\[\/caption\]/g, function( a ) {
264 // keep existing <br>
265 a = a.replace( /<br([^>]*)>/g, '<wp-temp-br$1>' );
266 // no line breaks inside HTML tags
267 a = a.replace( /<[^<>]+>/g, function( b ) {
268 return b.replace( /[\n\t ]+/, ' ' );
270 // convert remaining line breaks to <br>
271 return a.replace( /\s*\n\s*/g, '<wp-temp-br />' );
275 text = text + '\n\n';
276 text = text.replace( /<br \/>\s*<br \/>/gi, '\n\n' );
277 text = text.replace( new RegExp( '(<(?:' + blocklist + ')(?: [^>]*)?>)', 'gi' ), '\n$1' );
278 text = text.replace( new RegExp( '(</(?:' + blocklist + ')>)', 'gi' ), '$1\n\n' );
279 text = text.replace( /<hr( [^>]*)?>/gi, '<hr$1>\n\n' ); // hr is self closing block element
280 text = text.replace( /\s*<option/gi, '<option' ); // No <p> or <br> around <option>
281 text = text.replace( /<\/option>\s*/gi, '</option>' );
282 text = text.replace( /\n\s*\n+/g, '\n\n' );
283 text = text.replace( /([\s\S]+?)\n\n/g, '<p>$1</p>\n' );
284 text = text.replace( /<p>\s*?<\/p>/gi, '');
285 text = text.replace( new RegExp( '<p>\\s*(</?(?:' + blocklist + ')(?: [^>]*)?>)\\s*</p>', 'gi' ), '$1' );
286 text = text.replace( /<p>(<li.+?)<\/p>/gi, '$1');
287 text = text.replace( /<p>\s*<blockquote([^>]*)>/gi, '<blockquote$1><p>');
288 text = text.replace( /<\/blockquote>\s*<\/p>/gi, '</p></blockquote>');
289 text = text.replace( new RegExp( '<p>\\s*(</?(?:' + blocklist + ')(?: [^>]*)?>)', 'gi' ), '$1' );
290 text = text.replace( new RegExp( '(</?(?:' + blocklist + ')(?: [^>]*)?>)\\s*</p>', 'gi' ), '$1' );
292 // Remove redundant spaces and line breaks after existing <br /> tags
293 text = text.replace( /(<br[^>]*>)\s*\n/gi, '$1' );
295 // Create <br /> from the remaining line breaks
296 text = text.replace( /\s*\n/g, '<br />\n');
298 text = text.replace( new RegExp( '(</?(?:' + blocklist + ')[^>]*>)\\s*<br />', 'gi' ), '$1' );
299 text = text.replace( /<br \/>(\s*<\/?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)>)/gi, '$1' );
300 text = text.replace( /(?:<p>|<br ?\/?>)*\s*\[caption([^\[]+)\[\/caption\]\s*(?:<\/p>|<br ?\/?>)*/gi, '[caption$1[/caption]' );
302 text = text.replace( /(<(?:div|th|td|form|fieldset|dd)[^>]*>)(.*?)<\/p>/g, function( a, b, c ) {
303 if ( c.match( /<p( [^>]*)?>/ ) ) {
307 return b + '<p>' + c + '</p>';
310 // put back the line breaks in pre|script
311 if ( preserve_linebreaks ) {
312 text = text.replace( /<wp-line-break>/g, '\n' );
316 text = text.replace( /<wp-temp-br([^>]*)>/g, '<br$1>' );
323 function pre_wpautop( html ) {
324 var obj = { o: exports, data: html, unfiltered: html };
327 $( 'body' ).trigger( 'beforePreWpautop', [ obj ] );
330 obj.data = removep( obj.data );
333 $( 'body' ).trigger( 'afterPreWpautop', [ obj ] );
339 function wpautop( text ) {
340 var obj = { o: exports, data: text, unfiltered: text };
343 $( 'body' ).trigger( 'beforeWpautop', [ obj ] );
346 obj.data = autop( obj.data );
349 $( 'body' ).trigger( 'afterWpautop', [ obj ] );
356 $( document ).ready( init );
357 } else if ( document.addEventListener ) {
358 document.addEventListener( 'DOMContentLoaded', init, false );
359 window.addEventListener( 'load', init, false );
360 } else if ( window.attachEvent ) {
361 window.attachEvent( 'onload', init );
362 document.attachEvent( 'onreadystatechange', function() {
363 if ( 'complete' === document.readyState ) {
369 window.wp = window.wp || {};
370 window.wp.editor = window.wp.editor || {};
371 window.wp.editor.autop = wpautop;
372 window.wp.editor.removep = pre_wpautop;
377 pre_wpautop: pre_wpautop,
385 window.switchEditors = new SwitchEditors();