1 /* global tinymce, tinyMCEPreInit, QTags, setUserSetting */
3 window.switchEditors = {
5 switchto: function( el ) {
8 id = aid.substr( 0, l - 5 ),
9 mode = aid.substr( l - 4 );
14 // mode can be 'html', 'tmce', or 'toggle'; 'html' is used for the 'Text' editor tab.
15 go: function( id, mode ) {
16 var t = this, ed, wrap_id, txtarea_el, iframe, editorHeight, toolbarHeight,
17 DOM = tinymce.DOM; //DOMUtils outside the editor iframe
20 mode = mode || 'toggle';
22 ed = tinymce.get( id );
23 wrap_id = 'wp-' + id + '-wrap';
24 txtarea_el = DOM.get( id );
26 if ( 'toggle' === mode ) {
27 if ( ed && ! ed.isHidden() ) {
34 function getToolbarHeight() {
35 var node = DOM.select( '.mce-toolbar-grp', ed.getContainer() )[0],
36 height = node && node.clientHeight;
38 if ( height && height > 10 && height < 200 ) {
39 return parseInt( height, 10 );
45 if ( 'tmce' === mode || 'tinymce' === mode ) {
46 if ( ed && ! ed.isHidden() ) {
50 if ( typeof( QTags ) !== 'undefined' ) {
51 QTags.closeAllTags( id );
54 editorHeight = txtarea_el ? parseInt( txtarea_el.style.height, 10 ) : 0;
56 if ( tinyMCEPreInit.mceInit[ id ] && tinyMCEPreInit.mceInit[ id ].wpautop ) {
57 txtarea_el.value = t.wpautop( txtarea_el.value );
63 // No point resizing the iframe in iOS
64 if ( ! tinymce.Env.iOS && editorHeight ) {
65 toolbarHeight = getToolbarHeight();
66 editorHeight = editorHeight - toolbarHeight + 14;
68 // height cannot be under 50 or over 5000
69 if ( editorHeight > 50 && editorHeight < 5000 ) {
70 ed.theme.resizeTo( null, editorHeight );
74 tinymce.init( tinyMCEPreInit.mceInit[id] );
77 DOM.removeClass( wrap_id, 'html-active' );
78 DOM.addClass( wrap_id, 'tmce-active' );
79 DOM.setAttrib( txtarea_el, 'aria-hidden', true );
80 setUserSetting( 'editor', 'tinymce' );
82 } else if ( 'html' === mode ) {
84 if ( ed && ed.isHidden() ) {
89 if ( ! tinymce.Env.iOS ) {
90 iframe = DOM.get( id + '_ifr' );
91 editorHeight = iframe ? parseInt( iframe.style.height, 10 ) : 0;
94 toolbarHeight = getToolbarHeight();
95 editorHeight = editorHeight + toolbarHeight - 14;
97 // height cannot be under 50 or over 5000
98 if ( editorHeight > 50 && editorHeight < 5000 ) {
99 txtarea_el.style.height = editorHeight + 'px';
106 // The TinyMCE instance doesn't exist, run the content through 'pre_wpautop()' and show the textarea
107 if ( tinyMCEPreInit.mceInit[ id ] && tinyMCEPreInit.mceInit[ id ].wpautop ) {
108 txtarea_el.value = t.pre_wpautop( txtarea_el.value );
111 DOM.setStyles( txtarea_el, {'display': '', 'visibility': ''} );
114 DOM.removeClass( wrap_id, 'tmce-active' );
115 DOM.addClass( wrap_id, 'html-active' );
116 DOM.setAttrib( txtarea_el, 'aria-hidden', false );
117 setUserSetting( 'editor', 'html' );
122 _wp_Nop: function( content ) {
123 var blocklist1, blocklist2,
124 preserve_linebreaks = false,
127 // Protect pre|script tags
128 if ( content.indexOf( '<pre' ) !== -1 || content.indexOf( '<script' ) !== -1 ) {
129 preserve_linebreaks = true;
130 content = content.replace( /<(pre|script)[^>]*>[\s\S]+?<\/\1>/g, function( a ) {
131 a = a.replace( /<br ?\/?>(\r\n|\n)?/g, '<wp-line-break>' );
132 a = a.replace( /<\/?p( [^>]*)?>(\r\n|\n)?/g, '<wp-line-break>' );
133 return a.replace( /\r?\n/g, '<wp-line-break>' );
137 // keep <br> tags inside captions and remove line breaks
138 if ( content.indexOf( '[caption' ) !== -1 ) {
140 content = content.replace( /\[caption[\s\S]+?\[\/caption\]/g, function( a ) {
141 return a.replace( /<br([^>]*)>/g, '<wp-temp-br$1>' ).replace( /[\r\n\t]+/, '' );
145 // Pretty it up for the source editor
146 blocklist1 = 'blockquote|ul|ol|li|table|thead|tbody|tfoot|tr|th|td|div|h[1-6]|p|fieldset';
147 content = content.replace( new RegExp( '\\s*</(' + blocklist1 + ')>\\s*', 'g' ), '</$1>\n' );
148 content = content.replace( new RegExp( '\\s*<((?:' + blocklist1 + ')(?: [^>]*)?)>', 'g' ), '\n<$1>' );
150 // Mark </p> if it has any attributes.
151 content = content.replace( /(<p [^>]+>.*?)<\/p>/g, '$1</p#>' );
153 // Separate <div> containing <p>
154 content = content.replace( /<div( [^>]*)?>\s*<p>/gi, '<div$1>\n\n' );
156 // Remove <p> and <br />
157 content = content.replace( /\s*<p>/gi, '' );
158 content = content.replace( /\s*<\/p>\s*/gi, '\n\n' );
159 content = content.replace( /\n[\s\u00a0]+\n/g, '\n\n' );
160 content = content.replace( /\s*<br ?\/?>\s*/gi, '\n' );
162 // Fix some block element newline issues
163 content = content.replace( /\s*<div/g, '\n<div' );
164 content = content.replace( /<\/div>\s*/g, '</div>\n' );
165 content = content.replace( /\s*\[caption([^\[]+)\[\/caption\]\s*/gi, '\n\n[caption$1[/caption]\n\n' );
166 content = content.replace( /caption\]\n\n+\[caption/g, 'caption]\n\n[caption' );
168 blocklist2 = 'blockquote|ul|ol|li|table|thead|tbody|tfoot|tr|th|td|h[1-6]|pre|fieldset';
169 content = content.replace( new RegExp('\\s*<((?:' + blocklist2 + ')(?: [^>]*)?)\\s*>', 'g' ), '\n<$1>' );
170 content = content.replace( new RegExp('\\s*</(' + blocklist2 + ')>\\s*', 'g' ), '</$1>\n' );
171 content = content.replace( /<li([^>]*)>/g, '\t<li$1>' );
173 if ( content.indexOf( '<option' ) !== -1 ) {
174 content = content.replace( /\s*<option/g, '\n<option' );
175 content = content.replace( /\s*<\/select>/g, '\n</select>' );
178 if ( content.indexOf( '<hr' ) !== -1 ) {
179 content = content.replace( /\s*<hr( [^>]*)?>\s*/g, '\n\n<hr$1>\n\n' );
182 if ( content.indexOf( '<object' ) !== -1 ) {
183 content = content.replace( /<object[\s\S]+?<\/object>/g, function( a ) {
184 return a.replace( /[\r\n]+/g, '' );
188 // Unmark special paragraph closing tags
189 content = content.replace( /<\/p#>/g, '</p>\n' );
190 content = content.replace( /\s*(<p [^>]+>[\s\S]*?<\/p>)/g, '\n$1' );
193 content = content.replace( /^\s+/, '' );
194 content = content.replace( /[\s\u00a0]+$/, '' );
196 // put back the line breaks in pre|script
197 if ( preserve_linebreaks ) {
198 content = content.replace( /<wp-line-break>/g, '\n' );
201 // and the <br> tags in captions
203 content = content.replace( /<wp-temp-br([^>]*)>/g, '<br$1>' );
209 _wp_Autop: function(pee) {
210 var preserve_linebreaks = false,
212 blocklist = 'table|thead|tfoot|caption|col|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre' +
213 '|form|map|area|blockquote|address|math|style|p|h[1-6]|hr|fieldset|legend|section' +
214 '|article|aside|hgroup|header|footer|nav|figure|details|menu|summary';
216 if ( pee.indexOf( '<object' ) !== -1 ) {
217 pee = pee.replace( /<object[\s\S]+?<\/object>/g, function( a ) {
218 return a.replace( /[\r\n]+/g, '' );
222 pee = pee.replace( /<[^<>]+>/g, function( a ){
223 return a.replace( /[\r\n]+/g, ' ' );
226 // Protect pre|script tags
227 if ( pee.indexOf( '<pre' ) !== -1 || pee.indexOf( '<script' ) !== -1 ) {
228 preserve_linebreaks = true;
229 pee = pee.replace( /<(pre|script)[^>]*>[\s\S]+?<\/\1>/g, function( a ) {
230 return a.replace( /(\r\n|\n)/g, '<wp-line-break>' );
234 // keep <br> tags inside captions and convert line breaks
235 if ( pee.indexOf( '[caption' ) !== -1 ) {
237 pee = pee.replace( /\[caption[\s\S]+?\[\/caption\]/g, function( a ) {
238 // keep existing <br>
239 a = a.replace( /<br([^>]*)>/g, '<wp-temp-br$1>' );
240 // no line breaks inside HTML tags
241 a = a.replace( /<[a-zA-Z0-9]+( [^<>]+)?>/g, function( b ) {
242 return b.replace( /[\r\n\t]+/, ' ' );
244 // convert remaining line breaks to <br>
245 return a.replace( /\s*\n\s*/g, '<wp-temp-br />' );
250 pee = pee.replace( /<br \/>\s*<br \/>/gi, '\n\n' );
251 pee = pee.replace( new RegExp( '(<(?:' + blocklist + ')(?: [^>]*)?>)', 'gi' ), '\n$1' );
252 pee = pee.replace( new RegExp( '(</(?:' + blocklist + ')>)', 'gi' ), '$1\n\n' );
253 pee = pee.replace( /<hr( [^>]*)?>/gi, '<hr$1>\n\n' ); // hr is self closing block element
254 pee = pee.replace( /\s*<option/gi, '<option' ); // No <p> or <br> around <option>
255 pee = pee.replace( /<\/option>\s*/gi, '</option>' );
256 pee = pee.replace( /\r\n|\r/g, '\n' );
257 pee = pee.replace( /\n\s*\n+/g, '\n\n' );
258 pee = pee.replace( /([\s\S]+?)\n\n/g, '<p>$1</p>\n' );
259 pee = pee.replace( /<p>\s*?<\/p>/gi, '');
260 pee = pee.replace( new RegExp( '<p>\\s*(</?(?:' + blocklist + ')(?: [^>]*)?>)\\s*</p>', 'gi' ), '$1' );
261 pee = pee.replace( /<p>(<li.+?)<\/p>/gi, '$1');
262 pee = pee.replace( /<p>\s*<blockquote([^>]*)>/gi, '<blockquote$1><p>');
263 pee = pee.replace( /<\/blockquote>\s*<\/p>/gi, '</p></blockquote>');
264 pee = pee.replace( new RegExp( '<p>\\s*(</?(?:' + blocklist + ')(?: [^>]*)?>)', 'gi' ), '$1' );
265 pee = pee.replace( new RegExp( '(</?(?:' + blocklist + ')(?: [^>]*)?>)\\s*</p>', 'gi' ), '$1' );
266 pee = pee.replace( /\s*\n/gi, '<br />\n');
267 pee = pee.replace( new RegExp( '(</?(?:' + blocklist + ')[^>]*>)\\s*<br />', 'gi' ), '$1' );
268 pee = pee.replace( /<br \/>(\s*<\/?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)>)/gi, '$1' );
269 pee = pee.replace( /(?:<p>|<br ?\/?>)*\s*\[caption([^\[]+)\[\/caption\]\s*(?:<\/p>|<br ?\/?>)*/gi, '[caption$1[/caption]' );
271 pee = pee.replace( /(<(?:div|th|td|form|fieldset|dd)[^>]*>)(.*?)<\/p>/g, function( a, b, c ) {
272 if ( c.match( /<p( [^>]*)?>/ ) ) {
276 return b + '<p>' + c + '</p>';
279 // put back the line breaks in pre|script
280 if ( preserve_linebreaks ) {
281 pee = pee.replace( /<wp-line-break>/g, '\n' );
285 pee = pee.replace( /<wp-temp-br([^>]*)>/g, '<br$1>' );
291 pre_wpautop: function( content ) {
292 var t = this, o = { o: t, data: content, unfiltered: content },
293 q = typeof( jQuery ) !== 'undefined';
296 jQuery( 'body' ).trigger( 'beforePreWpautop', [ o ] );
299 o.data = t._wp_Nop( o.data );
302 jQuery('body').trigger('afterPreWpautop', [ o ] );
308 wpautop: function( pee ) {
309 var t = this, o = { o: t, data: pee, unfiltered: pee },
310 q = typeof( jQuery ) !== 'undefined';
313 jQuery( 'body' ).trigger('beforeWpautop', [ o ] );
316 o.data = t._wp_Autop( o.data );
319 jQuery( 'body' ).trigger('afterWpautop', [ o ] );