window.wp = window.wp || {}; ( function( $, wp ) { wp.editor = wp.editor || {}; /** * @summary Utility functions for the editor. * * @since 2.5.0 */ function SwitchEditors() { var tinymce, $$, exports = {}; function init() { if ( ! tinymce && window.tinymce ) { tinymce = window.tinymce; $$ = tinymce.$; /** * @summary Handles onclick events for the Visual/Text tabs. * * @since 4.3.0 * * @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 Returns the height of the editor toolbar(s) in px. * * @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 mode. * * @since 2.5.0 * * @memberof switchEditors * * @param {string} id The id of the editor you want to change the editor mode for. Default: `content`. * @param {string} mode The mode you want to switch to. Default: `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]; if ( 'toggle' === mode ) { if ( editor && ! editor.isHidden() ) { mode = 'html'; } else { mode = 'tmce'; } } if ( 'tmce' === mode || 'tinymce' === mode ) { // If the editor is visible we are already in `tinymce` mode. if ( editor && ! editor.isHidden() ) { return false; } // Insert closing tags for any open tags in QuickTags. if ( typeof( window.QTags ) !== 'undefined' ) { window.QTags.closeAllTags( id ); } editorHeight = parseInt( textarea.style.height, 10 ) || 0; if ( editor ) { editor.show(); // No point to resize the iframe in iOS. if ( ! tinymce.Env.iOS && editorHeight ) { toolbarHeight = getToolbarHeight( editor ); editorHeight = editorHeight - toolbarHeight + 14; // Sane limit for the editor height. 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 the editor is hidden (Quicktags is shown) we don't need to switch. if ( editor && editor.isHidden() ) { return false; } if ( editor ) { // Don't resize the textarea in iOS. The iframe is forced to 100% height there, we shouldn't match it. if ( ! tinymce.Env.iOS ) { iframe = editor.iframeElement; editorHeight = iframe ? parseInt( iframe.style.height, 10 ) : 0; if ( editorHeight ) { toolbarHeight = getToolbarHeight( editor ); editorHeight = editorHeight + toolbarHeight - 14; // Sane limit for the textarea height. if ( editorHeight > 50 && editorHeight < 5000 ) { textarea.style.height = editorHeight + 'px'; } } } editor.hide(); } else { // There is probably a JS error on the page. The TinyMCE editor 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

tags with two line breaks. "Opposite" of wpautop(). * * Replaces

tags with two line breaks except where the

has attributes. * Unifies whitespace. * Indents

  • ,
    and
    for better readability. * * @since 2.5.0 * * @memberof switchEditors * * @param {string} html The content from the editor. * @return {string} The content with stripped paragraph tags. */ function removep( html ) { var blocklist = 'blockquote|ul|ol|li|dl|dt|dd|table|thead|tbody|tfoot|tr|th|td|h[1-6]|fieldset|figure', blocklist1 = blocklist + '|div|p', blocklist2 = blocklist + '|pre', preserve_linebreaks = false, preserve_br = false, preserve = []; if ( ! html ) { return ''; } // Protect script and style tags. if ( html.indexOf( ']*>[\s\S]*?<\/\1>/g, function( match ) { preserve.push( match ); return ''; } ); } // Protect pre tags. if ( html.indexOf( ']*>[\s\S]+?<\/pre>/g, function( a ) { a = a.replace( /
    (\r\n|\n)?/g, '' ); a = a.replace( /<\/?p( [^>]*)?>(\r\n|\n)?/g, '' ); return a.replace( /\r?\n/g, '' ); }); } // Remove line breaks but keep
    tags inside image captions. 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]+/, '' ); }); } // Normalize white space characters before and after block tags. html = html.replace( new RegExp( '\\s*\\s*', 'g' ), '\n' ); html = html.replace( new RegExp( '\\s*<((?:' + blocklist1 + ')(?: [^>]*)?)>', 'g' ), '\n<$1>' ); // Mark

    if it has any attributes. html = html.replace( /(

    ]+>.*?)<\/p>/g, '$1' ); // Preserve the first

    inside a

    . html = html.replace( /]*)?>\s*

    /gi, '\n\n' ); // Remove paragraph tags. html = html.replace( /\s*

    /gi, '' ); html = html.replace( /\s*<\/p>\s*/gi, '\n\n' ); // Normalize white space chars and remove multiple line breaks. html = html.replace( /\n[\s\u00a0]+\n/g, '\n\n' ); // Rrplace
    tags with a line break. html = html.replace( /\s*
    \s*/gi, '\n' ); // Fix line breaks around

    . html = html.replace( /\s*
    \s*/g, '
    \n' ); // Fix line breaks around caption shortcodes. 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' ); // Pad block elements tags with a line break. html = html.replace( new RegExp('\\s*<((?:' + blocklist2 + ')(?: [^>]*)?)\\s*>', 'g' ), '\n<$1>' ); html = html.replace( new RegExp('\\s*\\s*', 'g' ), '\n' ); // Indent
  • ,
    and
    tags. html = html.replace( /<((li|dt|dd)[^>]*)>/g, ' \t<$1>' ); // Fix line breaks around ' ); } // Pad
    with two line breaks. if ( html.indexOf( ']*)?>\s*/g, '\n\n\n\n' ); } // Remove line breaks in tags. if ( html.indexOf( '/g, function( a ) { return a.replace( /[\r\n]+/g, '' ); }); } // Unmark special paragraph closing tags. html = html.replace( /<\/p#>/g, '

    \n' ); // Pad remaining

    tags whit a line break. html = html.replace( /\s*(

    ]+>[\s\S]*?<\/p>)/g, '\n$1' ); // Trim. html = html.replace( /^\s+/, '' ); html = html.replace( /[\s\u00a0]+$/, '' ); if ( preserve_linebreaks ) { html = html.replace( //g, '\n' ); } if ( preserve_br ) { html = html.replace( /]*)>/g, '' ); } // Restore preserved tags. if ( preserve.length ) { html = html.replace( //g, function() { return preserve.shift(); } ); } return html; } /** * @summary Replaces two line breaks with a paragraph tag and one line break with a
    . * * Similar to `wpautop()` in formatting.php. * * @since 2.5.0 * * @memberof switchEditors * * @param {string} text The text input. * @returns {string} The formatted text. */ function autop( text ) { var preserve_linebreaks = false, preserve_br = false, blocklist = 'table|thead|tfoot|caption|col|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre' + '|form|map|area|blockquote|address|math|style|p|h[1-6]|hr|fieldset|legend|section' + '|article|aside|hgroup|header|footer|nav|figure|figcaption|details|menu|summary'; // Normalize line breaks. text = text.replace( /\r\n|\r/g, '\n' ); if ( text.indexOf( '\n' ) === -1 ) { return text; } // Remove line breaks from . if ( text.indexOf( '/g, function( a ) { return a.replace( /\n+/g, '' ); }); } // Remove line breaks from tags. text = text.replace( /<[^<>]+>/g, function( a ) { return a.replace( /[\n\t ]+/g, ' ' ); }); // Preserve line breaks in
     and