( function( $ ) {
function SwitchEditors() {
var tinymce, $$,
exports = {};
function init() {
if ( ! tinymce && window.tinymce ) {
tinymce = window.tinymce;
$$ = tinymce.$;
$$( document ).on( 'click', function( event ) {
var id, mode,
target = $$( event.target );
if ( target.hasClass( 'wp-switch-editor' ) ) {
id = target.attr( 'data-wp-editor-id' );
mode = target.hasClass( 'switch-tmce' ) ? 'tmce' : 'html';
switchEditor( id, mode );
}
});
}
}
function getToolbarHeight( editor ) {
var node = $$( '.mce-toolbar-grp', editor.getContainer() )[0],
height = node && node.clientHeight;
if ( height && height > 10 && height < 200 ) {
return parseInt( height, 10 );
}
return 30;
}
function switchEditor( id, mode ) {
id = id || 'content';
mode = mode || 'toggle';
var editorHeight, toolbarHeight, iframe,
editor = tinymce.get( id ),
wrap = $$( '#wp-' + id + '-wrap' ),
$textarea = $$( '#' + id ),
textarea = $textarea[0];
if ( 'toggle' === mode ) {
if ( editor && ! editor.isHidden() ) {
mode = 'html';
} else {
mode = 'tmce';
}
}
if ( 'tmce' === mode || 'tinymce' === mode ) {
if ( editor && ! editor.isHidden() ) {
return false;
}
if ( typeof( window.QTags ) !== 'undefined' ) {
window.QTags.closeAllTags( id );
}
editorHeight = parseInt( textarea.style.height, 10 ) || 0;
if ( editor ) {
editor.show();
// No point resizing the iframe in iOS
if ( ! tinymce.Env.iOS && editorHeight ) {
toolbarHeight = getToolbarHeight( editor );
editorHeight = editorHeight - toolbarHeight + 14;
// height cannot be under 50 or over 5000
if ( editorHeight > 50 && editorHeight < 5000 ) {
editor.theme.resizeTo( null, editorHeight );
}
}
} else {
tinymce.init( window.tinyMCEPreInit.mceInit[id] );
}
wrap.removeClass( 'html-active' ).addClass( 'tmce-active' );
$textarea.attr( 'aria-hidden', true );
window.setUserSetting( 'editor', 'tinymce' );
} else if ( 'html' === mode ) {
if ( editor && editor.isHidden() ) {
return false;
}
if ( editor ) {
if ( ! tinymce.Env.iOS ) {
iframe = editor.iframeElement;
editorHeight = iframe ? parseInt( iframe.style.height, 10 ) : 0;
if ( editorHeight ) {
toolbarHeight = getToolbarHeight( editor );
editorHeight = editorHeight + toolbarHeight - 14;
// height cannot be under 50 or over 5000
if ( editorHeight > 50 && editorHeight < 5000 ) {
textarea.style.height = editorHeight + 'px';
}
}
}
editor.hide();
} else {
// The TinyMCE instance doesn't exist, show the textarea
$textarea.css({ 'display': '', 'visibility': '' });
}
wrap.removeClass( 'tmce-active' ).addClass( 'html-active' );
$textarea.attr( 'aria-hidden', false );
window.setUserSetting( 'editor', 'html' );
}
}
// Replace paragraphs with double line breaks
function removep( html ) {
var blocklist = 'blockquote|ul|ol|li|table|thead|tbody|tfoot|tr|th|td|h[1-6]|fieldset',
blocklist1 = blocklist + '|div|p',
blocklist2 = blocklist + '|pre',
preserve_linebreaks = false,
preserve_br = false;
if ( ! html ) {
return '';
}
// Protect pre|script tags
if ( html.indexOf( '
]*>[\s\S]+?<\/\1>/g, function( a ) {
a = a.replace( /
(\r\n|\n)?/g, '' );
a = a.replace( /<\/?p( [^>]*)?>(\r\n|\n)?/g, '' );
return a.replace( /\r?\n/g, '' );
});
}
// keep
tags inside captions and remove line breaks
if ( html.indexOf( '[caption' ) !== -1 ) {
preserve_br = true;
html = html.replace( /\[caption[\s\S]+?\[\/caption\]/g, function( a ) {
return a.replace( /
]*)>/g, '' ).replace( /[\r\n\t]+/, '' );
});
}
// Pretty it up for the source editor
html = html.replace( new RegExp( '\\s*(' + blocklist1 + ')>\\s*', 'g' ), '$1>\n' );
html = html.replace( new RegExp( '\\s*<((?:' + blocklist1 + ')(?: [^>]*)?)>', 'g' ), '\n<$1>' );
// Mark if it has any attributes.
html = html.replace( /(]+>.*?)<\/p>/g, '$1
' );
// Separate containing
html = html.replace( /
]*)?>\s*
/gi, '
\n\n' );
// Remove
and
html = html.replace( /\s*
/gi, '' );
html = html.replace( /\s*<\/p>\s*/gi, '\n\n' );
html = html.replace( /\n[\s\u00a0]+\n/g, '\n\n' );
html = html.replace( /\s*
\s*/gi, '\n' );
// Fix some block element newline issues
html = html.replace( /\s*
\s*/g, '
\n' );
html = html.replace( /\s*\[caption([^\[]+)\[\/caption\]\s*/gi, '\n\n[caption$1[/caption]\n\n' );
html = html.replace( /caption\]\n\n+\[caption/g, 'caption]\n\n[caption' );
html = html.replace( new RegExp('\\s*<((?:' + blocklist2 + ')(?: [^>]*)?)\\s*>', 'g' ), '\n<$1>' );
html = html.replace( new RegExp('\\s*(' + blocklist2 + ')>\\s*', 'g' ), '$1>\n' );
html = html.replace( /
]*)>/g, '\t' );
if ( html.indexOf( '