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,
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 if ( 'tmce' === mode || 'tinymce' === mode ) {
35 if ( ed && ! ed.isHidden() ) {
39 if ( typeof( QTags ) !== 'undefined' ) {
40 QTags.closeAllTags( id );
43 if ( tinyMCEPreInit.mceInit[ id ] && tinyMCEPreInit.mceInit[ id ].wpautop ) {
44 txtarea_el.value = t.wpautop( txtarea_el.value );
50 ed = new tinymce.Editor( id, tinyMCEPreInit.mceInit[ id ] );
54 dom.removeClass( wrap_id, 'html-active' );
55 dom.addClass( wrap_id, 'tmce-active' );
56 setUserSetting( 'editor', 'tinymce' );
58 } else if ( 'html' === mode ) {
60 if ( ed && ed.isHidden() ) {
67 // The TinyMCE instance doesn't exist, run the content through 'pre_wpautop()' and show the textarea
68 if ( tinyMCEPreInit.mceInit[ id ] && tinyMCEPreInit.mceInit[ id ].wpautop ) {
69 txtarea_el.value = t.pre_wpautop( txtarea_el.value );
72 dom.setStyles( txtarea_el, {'display': '', 'visibility': ''} );
75 dom.removeClass( wrap_id, 'tmce-active' );
76 dom.addClass( wrap_id, 'html-active' );
77 setUserSetting( 'editor', 'html' );
82 _wp_Nop: function( content ) {
83 var blocklist1, blocklist2,
84 preserve_linebreaks = false,
87 // Protect pre|script tags
88 if ( content.indexOf( '<pre' ) !== -1 || content.indexOf( '<script' ) !== -1 ) {
89 preserve_linebreaks = true;
90 content = content.replace( /<(pre|script)[^>]*>[\s\S]+?<\/\1>/g, function( a ) {
91 a = a.replace( /<br ?\/?>(\r\n|\n)?/g, '<wp-temp-lb>' );
92 return a.replace( /<\/?p( [^>]*)?>(\r\n|\n)?/g, '<wp-temp-lb>' );
96 // keep <br> tags inside captions and remove line breaks
97 if ( content.indexOf( '[caption' ) !== -1 ) {
99 content = content.replace( /\[caption[\s\S]+?\[\/caption\]/g, function( a ) {
100 return a.replace( /<br([^>]*)>/g, '<wp-temp-br$1>' ).replace( /[\r\n\t]+/, '' );
104 // Pretty it up for the source editor
105 blocklist1 = 'blockquote|ul|ol|li|table|thead|tbody|tfoot|tr|th|td|div|h[1-6]|p|fieldset';
106 content = content.replace( new RegExp( '\\s*</(' + blocklist1 + ')>\\s*', 'g' ), '</$1>\n' );
107 content = content.replace( new RegExp( '\\s*<((?:' + blocklist1 + ')(?: [^>]*)?)>', 'g' ), '\n<$1>' );
109 // Mark </p> if it has any attributes.
110 content = content.replace( /(<p [^>]+>.*?)<\/p>/g, '$1</p#>' );
112 // Separate <div> containing <p>
113 content = content.replace( /<div( [^>]*)?>\s*<p>/gi, '<div$1>\n\n' );
115 // Remove <p> and <br />
116 content = content.replace( /\s*<p>/gi, '' );
117 content = content.replace( /\s*<\/p>\s*/gi, '\n\n' );
118 content = content.replace( /\n[\s\u00a0]+\n/g, '\n\n' );
119 content = content.replace( /\s*<br ?\/?>\s*/gi, '\n' );
121 // Fix some block element newline issues
122 content = content.replace( /\s*<div/g, '\n<div' );
123 content = content.replace( /<\/div>\s*/g, '</div>\n' );
124 content = content.replace( /\s*\[caption([^\[]+)\[\/caption\]\s*/gi, '\n\n[caption$1[/caption]\n\n' );
125 content = content.replace( /caption\]\n\n+\[caption/g, 'caption]\n\n[caption' );
127 blocklist2 = 'blockquote|ul|ol|li|table|thead|tbody|tfoot|tr|th|td|h[1-6]|pre|fieldset';
128 content = content.replace( new RegExp('\\s*<((?:' + blocklist2 + ')(?: [^>]*)?)\\s*>', 'g' ), '\n<$1>' );
129 content = content.replace( new RegExp('\\s*</(' + blocklist2 + ')>\\s*', 'g' ), '</$1>\n' );
130 content = content.replace( /<li([^>]*)>/g, '\t<li$1>' );
132 if ( content.indexOf( '<hr' ) !== -1 ) {
133 content = content.replace( /\s*<hr( [^>]*)?>\s*/g, '\n\n<hr$1>\n\n' );
136 if ( content.indexOf( '<object' ) !== -1 ) {
137 content = content.replace( /<object[\s\S]+?<\/object>/g, function( a ) {
138 return a.replace( /[\r\n]+/g, '' );
142 // Unmark special paragraph closing tags
143 content = content.replace( /<\/p#>/g, '</p>\n' );
144 content = content.replace( /\s*(<p [^>]+>[\s\S]*?<\/p>)/g, '\n$1' );
147 content = content.replace( /^\s+/, '' );
148 content = content.replace( /[\s\u00a0]+$/, '' );
150 // put back the line breaks in pre|script
151 if ( preserve_linebreaks ) {
152 content = content.replace( /<wp-temp-lb>/g, '\n' );
155 // and the <br> tags in captions
157 content = content.replace( /<wp-temp-br([^>]*)>/g, '<br$1>' );
163 _wp_Autop: function(pee) {
164 var preserve_linebreaks = false,
166 blocklist = 'table|thead|tfoot|caption|col|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select' +
167 '|option|form|map|area|blockquote|address|math|style|p|h[1-6]|hr|fieldset|noscript|legend|section' +
168 '|article|aside|hgroup|header|footer|nav|figure|figcaption|details|menu|summary';
170 if ( pee.indexOf( '<object' ) !== -1 ) {
171 pee = pee.replace( /<object[\s\S]+?<\/object>/g, function( a ) {
172 return a.replace( /[\r\n]+/g, '' );
176 pee = pee.replace( /<[^<>]+>/g, function( a ){
177 return a.replace( /[\r\n]+/g, ' ' );
180 // Protect pre|script tags
181 if ( pee.indexOf( '<pre' ) !== -1 || pee.indexOf( '<script' ) !== -1 ) {
182 preserve_linebreaks = true;
183 pee = pee.replace( /<(pre|script)[^>]*>[\s\S]+?<\/\1>/g, function( a ) {
184 return a.replace( /(\r\n|\n)/g, '<wp-temp-lb>' );
188 // keep <br> tags inside captions and convert line breaks
189 if ( pee.indexOf( '[caption' ) !== -1 ) {
191 pee = pee.replace( /\[caption[\s\S]+?\[\/caption\]/g, function( a ) {
192 // keep existing <br>
193 a = a.replace( /<br([^>]*)>/g, '<wp-temp-br$1>' );
194 // no line breaks inside HTML tags
195 a = a.replace( /<[a-zA-Z0-9]+( [^<>]+)?>/g, function( b ) {
196 return b.replace( /[\r\n\t]+/, ' ' );
198 // convert remaining line breaks to <br>
199 return a.replace( /\s*\n\s*/g, '<wp-temp-br />' );
204 pee = pee.replace( /<br \/>\s*<br \/>/gi, '\n\n' );
205 pee = pee.replace( new RegExp( '(<(?:' + blocklist + ')(?: [^>]*)?>)', 'gi' ), '\n$1' );
206 pee = pee.replace( new RegExp( '(</(?:' + blocklist + ')>)', 'gi' ), '$1\n\n' );
207 pee = pee.replace( /<hr( [^>]*)?>/gi, '<hr$1>\n\n' ); // hr is self closing block element
208 pee = pee.replace( /\r\n|\r/g, '\n' );
209 pee = pee.replace( /\n\s*\n+/g, '\n\n' );
210 pee = pee.replace( /([\s\S]+?)\n\n/g, '<p>$1</p>\n' );
211 pee = pee.replace( /<p>\s*?<\/p>/gi, '');
212 pee = pee.replace( new RegExp( '<p>\\s*(</?(?:' + blocklist + ')(?: [^>]*)?>)\\s*</p>', 'gi' ), '$1' );
213 pee = pee.replace( /<p>(<li.+?)<\/p>/gi, '$1');
214 pee = pee.replace( /<p>\s*<blockquote([^>]*)>/gi, '<blockquote$1><p>');
215 pee = pee.replace( /<\/blockquote>\s*<\/p>/gi, '</p></blockquote>');
216 pee = pee.replace( new RegExp( '<p>\\s*(</?(?:' + blocklist + ')(?: [^>]*)?>)', 'gi' ), '$1' );
217 pee = pee.replace( new RegExp( '(</?(?:' + blocklist + ')(?: [^>]*)?>)\\s*</p>', 'gi' ), '$1' );
218 pee = pee.replace( /\s*\n/gi, '<br />\n');
219 pee = pee.replace( new RegExp( '(</?(?:' + blocklist + ')[^>]*>)\\s*<br />', 'gi' ), '$1' );
220 pee = pee.replace( /<br \/>(\s*<\/?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)>)/gi, '$1' );
221 pee = pee.replace( /(?:<p>|<br ?\/?>)*\s*\[caption([^\[]+)\[\/caption\]\s*(?:<\/p>|<br ?\/?>)*/gi, '[caption$1[/caption]' );
223 pee = pee.replace( /(<(?:div|th|td|form|fieldset|dd)[^>]*>)(.*?)<\/p>/g, function( a, b, c ) {
224 if ( c.match( /<p( [^>]*)?>/ ) ) {
228 return b + '<p>' + c + '</p>';
231 // put back the line breaks in pre|script
232 if ( preserve_linebreaks ) {
233 pee = pee.replace( /<wp-temp-lb>/g, '\n' );
237 pee = pee.replace( /<wp-temp-br([^>]*)>/g, '<br$1>' );
243 pre_wpautop: function( content ) {
244 var t = this, o = { o: t, data: content, unfiltered: content },
245 q = typeof( jQuery ) !== 'undefined';
248 jQuery( 'body' ).trigger( 'beforePreWpautop', [ o ] );
251 o.data = t._wp_Nop( o.data );
254 jQuery('body').trigger('afterPreWpautop', [ o ] );
260 wpautop: function( pee ) {
261 var t = this, o = { o: t, data: pee, unfiltered: pee },
262 q = typeof( jQuery ) !== 'undefined';
265 jQuery( 'body' ).trigger('beforeWpautop', [ o ] );
268 o.data = t._wp_Autop( o.data );
271 jQuery( 'body' ).trigger('afterWpautop', [ o ] );