( function( $ ) { /** * @summary Creates the tinyMCE editors. * * Creates the tinyMCE editor and binds all events used for switching * from visual to text mode. * * @since 4.3.0 * * @class */ function SwitchEditors() { var tinymce, $$, exports = {}; /** * @summary Initializes the event binding for switching editors. * * @since 4.3.0 * * @returns {void} */ function init() { if ( ! tinymce && window.tinymce ) { tinymce = window.tinymce; $$ = tinymce.$; /** * @summary Handles onclick events for the editor buttons. * * @since 4.3.0 * * Handles an onclick event on the document. * Switches the editor between visual and text, * if the clicked element has the 'wp-switch-editor' class. * If the class name is switch-html switches to the HTML editor, * if the class name is switch-tmce * switches to the TMCE editor. * * @returns {void} */ $$( 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 ); } }); } } /** * @summary Retrieves the height of the toolbar based on the container the * editor is placed in. * * @since 3.9.0 * * @param {Object} editor The tinyMCE editor. * @returns {number} If the height is between 10 and 200 return the height, * else return 30. */ 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; } /** * @summary Switches the editor between visual and text. * * @since 4.3.0 * * @memberof switchEditors * * @param {string} id The id of the editor you want to change the editor mode for. * If no id is given, it defaults to content. * @param {string} mode The mode you want to switch to. * If an undefined mode is given, it defaults to toggle. * * @returns {void} */ 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]; // Toggle the mode between visual and textual representation. if ( 'toggle' === mode ) { if ( editor && ! editor.isHidden() ) { mode = 'html'; } else { mode = 'tmce'; } } // If the mode is tmce or tinymce, show the editor. if ( 'tmce' === mode || 'tinymce' === mode ) { /* * If the editor isn't hidden we are already in tmce mode * and we don't need to switch. * Return false to stop event bubbling. */ if ( editor && ! editor.isHidden() ) { return false; } // Close the QuickTags toolbars if they are visible. if ( typeof( window.QTags ) !== 'undefined' ) { window.QTags.closeAllTags( id ); } editorHeight = parseInt( textarea.style.height, 10 ) || 0; if ( editor ) { editor.show(); // Don't resize the iframe in iOS. if ( ! tinymce.Env.iOS && editorHeight ) { toolbarHeight = getToolbarHeight( editor ); editorHeight = editorHeight - toolbarHeight + 14; // Height must be between 50 and 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' ); // Hide the editor if mode is html. } else if ( 'html' === mode ) { /* * If the editor is hidden we are already in html mode and * we don't need to switch. * Return false to stop event bubbling. */ if ( editor && editor.isHidden() ) { return false; } if ( editor ) { // Don't resize the iframe in iOS. 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 must be between 50 and 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' ); } } /** * @summary Replaces all paragraphs with double line breaks. * * Replaces all paragraphs with double line breaks. Taking into account * the elements where the
should be preserved.
* Unifies all whitespaces.
* Adds indenting with tabs to li, dt and dd elements.
* Trims whitespaces from beginning and end of the html input.
*
* @since 4.3.0
*
* @memberof switchEditors
*
* @param {string} html The content from the editor.
* @return {string} The formatted html string.
*/
function removep( html ) {
var blocklist = 'blockquote|ul|ol|li|dl|dt|dd|table|thead|tbody|tfoot|tr|th|td|h[1-6]|fieldset',
blocklist1 = blocklist + '|div|p',
blocklist2 = blocklist + '|pre',
preserve_linebreaks = false,
preserve_br = false,
preserve = [];
if ( ! html ) {
return '';
}
/*
* Protect script and style tags by replacing them with