<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[39738] trunk/src/wp-admin/js/editor.js: Docs: Add documentation for `wp-admin/js/editor.js`.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/39738">39738</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/39738","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>SergeyBiryukov</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2017-01-06 14:13:44 +0000 (Fri, 06 Jan 2017)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Docs: Add documentation for `wp-admin/js/editor.js`.

Props terwdan, ireneyoast.
Fixes <a href="https://core.trac.wordpress.org/ticket/38933">#38933</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpadminjseditorjs">trunk/src/wp-admin/js/editor.js</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpadminjseditorjs"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-admin/js/editor.js</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/js/editor.js   2017-01-06 14:04:32 UTC (rev 39737)
+++ trunk/src/wp-admin/js/editor.js     2017-01-06 14:13:44 UTC (rev 39738)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1,14 +1,45 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> ( function( $ ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        /**
+        * @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
+        */
</ins><span class="cx" style="display: block; padding: 0 10px">         function SwitchEditors() {
</span><span class="cx" style="display: block; padding: 0 10px">                var tinymce, $$,
</span><span class="cx" style="display: block; padding: 0 10px">                        exports = {};
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                /**
+                * @summary Initializes the event binding for switching editors.
+                *
+                * @since 4.3.0
+                *
+                * @returns {void}
+                */
</ins><span class="cx" style="display: block; padding: 0 10px">                 function init() {
</span><span class="cx" style="display: block; padding: 0 10px">                        if ( ! tinymce && window.tinymce ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                tinymce = window.tinymce;
</span><span class="cx" style="display: block; padding: 0 10px">                                $$ = tinymce.$;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                /**
+                                * @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}
+                                */
</ins><span class="cx" style="display: block; padding: 0 10px">                                 $$( document ).on( 'click', function( event ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                        var id, mode,
</span><span class="cx" style="display: block; padding: 0 10px">                                                target = $$( event.target );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -22,6 +53,16 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                /**
+                * @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.
+                */
</ins><span class="cx" style="display: block; padding: 0 10px">                 function getToolbarHeight( editor ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        var node = $$( '.mce-toolbar-grp', editor.getContainer() )[0],
</span><span class="cx" style="display: block; padding: 0 10px">                                height = node && node.clientHeight;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -33,6 +74,20 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        return 30;
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                /**
+                * @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}
+                */
</ins><span class="cx" style="display: block; padding: 0 10px">                 function switchEditor( id, mode ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        id = id || 'content';
</span><span class="cx" style="display: block; padding: 0 10px">                        mode = mode || 'toggle';
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -43,6 +98,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                $textarea = $$( '#' + id ),
</span><span class="cx" style="display: block; padding: 0 10px">                                textarea = $textarea[0];
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        // Toggle the mode between visual and textual representation.
</ins><span class="cx" style="display: block; padding: 0 10px">                         if ( 'toggle' === mode ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                if ( editor && ! editor.isHidden() ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                        mode = 'html';
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -51,11 +107,20 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                }
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                       // If the mode is tmce or tinymce, show the editor.
</ins><span class="cx" style="display: block; padding: 0 10px">                         if ( 'tmce' === mode || 'tinymce' === mode ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                               /*
+                                * 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.
+                                */
</ins><span class="cx" style="display: block; padding: 0 10px">                                 if ( editor && ! editor.isHidden() ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                        return false;
</span><span class="cx" style="display: block; padding: 0 10px">                                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                // Close the QuickTags toolbars if they are visible.
</ins><span class="cx" style="display: block; padding: 0 10px">                                 if ( typeof( window.QTags ) !== 'undefined' ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                        window.QTags.closeAllTags( id );
</span><span class="cx" style="display: block; padding: 0 10px">                                }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -65,12 +130,12 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                if ( editor ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                        editor.show();
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                        // No point resizing the iframe in iOS
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                 // Don't resize the iframe in iOS.
</ins><span class="cx" style="display: block; padding: 0 10px">                                         if ( ! tinymce.Env.iOS && editorHeight ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                                toolbarHeight = getToolbarHeight( editor );
</span><span class="cx" style="display: block; padding: 0 10px">                                                editorHeight = editorHeight - toolbarHeight + 14;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                                // height cannot be under 50 or over 5000
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                         // Height must be between 50 and 5000.
</ins><span class="cx" style="display: block; padding: 0 10px">                                                 if ( editorHeight > 50 && editorHeight < 5000 ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                                        editor.theme.resizeTo( null, editorHeight );
</span><span class="cx" style="display: block; padding: 0 10px">                                                }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -83,12 +148,21 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                $textarea.attr( 'aria-hidden', true );
</span><span class="cx" style="display: block; padding: 0 10px">                                window.setUserSetting( 'editor', 'tinymce' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        // Hide the editor if mode is html.
</ins><span class="cx" style="display: block; padding: 0 10px">                         } else if ( 'html' === mode ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                               /*
+                                * If the editor is hidden we are already in html mode and
+                                * we don't need to switch.
+                                * Return false to stop event bubbling.
+                                */
</ins><span class="cx" style="display: block; padding: 0 10px">                                 if ( editor && editor.isHidden() ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                        return false;
</span><span class="cx" style="display: block; padding: 0 10px">                                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                                if ( editor ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                                       // Don't resize the iframe in iOS.
</ins><span class="cx" style="display: block; padding: 0 10px">                                         if ( ! tinymce.Env.iOS ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                                iframe = editor.iframeElement;
</span><span class="cx" style="display: block; padding: 0 10px">                                                editorHeight = iframe ? parseInt( iframe.style.height, 10 ) : 0;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -97,7 +171,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                                        toolbarHeight = getToolbarHeight( editor );
</span><span class="cx" style="display: block; padding: 0 10px">                                                        editorHeight = editorHeight + toolbarHeight - 14;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                                        // height cannot be under 50 or over 5000
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                                 // Height must be between 50 and 5000.
</ins><span class="cx" style="display: block; padding: 0 10px">                                                         if ( editorHeight > 50 && editorHeight < 5000 ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                                                textarea.style.height = editorHeight + 'px';
</span><span class="cx" style="display: block; padding: 0 10px">                                                        }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -106,7 +180,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                                        editor.hide();
</span><span class="cx" style="display: block; padding: 0 10px">                                } else {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                        // The TinyMCE instance doesn't exist, show the textarea
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                 // The TinyMCE instance doesn't exist, show the textarea.
</ins><span class="cx" style="display: block; padding: 0 10px">                                         $textarea.css({ 'display': '', 'visibility': '' });
</span><span class="cx" style="display: block; padding: 0 10px">                                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -116,7 +190,22 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                // Replace paragraphs with double line breaks
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         /**
+                * @summary Replaces all paragraphs with double line breaks.
+                *
+                * Replaces all paragraphs with double line breaks. Taking into account
+                * the elements where the <p> 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.
+                */
</ins><span class="cx" style="display: block; padding: 0 10px">                 function removep( html ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        var blocklist = 'blockquote|ul|ol|li|dl|dt|dd|table|thead|tbody|tfoot|tr|th|td|h[1-6]|fieldset',
</span><span class="cx" style="display: block; padding: 0 10px">                                blocklist1 = blocklist + '|div|p',
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -129,7 +218,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                return '';
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        // Preserve script and style tags.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 /*
+                        * Protect script and style tags by replacing them with <wp-preserve>.
+                        * Push matches into the preserve array.
+                        */
</ins><span class="cx" style="display: block; padding: 0 10px">                         if ( html.indexOf( '<script' ) !== -1 || html.indexOf( '<style' ) !== -1 ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                html = html.replace( /<(script|style)[^>]*>[\s\S]*?<\/\1>/g, function( match ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                        preserve.push( match );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -137,7 +229,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                } );
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        // Protect pre tags.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 /*
+                        * Protect pre tags by replacing all newlines and
+                        * <br> tags with <wp-line-break>.
+                        */
</ins><span class="cx" style="display: block; padding: 0 10px">                         if ( html.indexOf( '<pre' ) !== -1 ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                preserve_linebreaks = true;
</span><span class="cx" style="display: block; padding: 0 10px">                                html = html.replace( /<pre[^>]*>[\s\S]+?<\/pre>/g, function( a ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -147,7 +242,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                });
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        // keep <br> tags inside captions and remove line breaks
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 /*
+                        * Keep <br> tags inside captions and remove line breaks by replacing
+                        * them with <wp-temp-br>.
+                        */
</ins><span class="cx" style="display: block; padding: 0 10px">                         if ( html.indexOf( '[caption' ) !== -1 ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                preserve_br = true;
</span><span class="cx" style="display: block; padding: 0 10px">                                html = html.replace( /\[caption[\s\S]+?\[\/caption\]/g, function( a ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -155,66 +253,96 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                });
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        // Pretty it up for the source editor
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 // Format the text to be readable in the source editor.
</ins><span class="cx" style="display: block; padding: 0 10px">                         html = html.replace( new RegExp( '\\s*</(' + blocklist1 + ')>\\s*', 'g' ), '</$1>\n' );
</span><span class="cx" style="display: block; padding: 0 10px">                        html = html.replace( new RegExp( '\\s*<((?:' + blocklist1 + ')(?: [^>]*)?)>', 'g' ), '\n<$1>' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        // Mark </p> if it has any attributes.
</span><span class="cx" style="display: block; padding: 0 10px">                        html = html.replace( /(<p [^>]+>.*?)<\/p>/g, '$1</p#>' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        // Separate <div> containing <p>
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 // If the content of a container starts with a paragraph, replace the <p> tag with 2 newlines.
</ins><span class="cx" style="display: block; padding: 0 10px">                         html = html.replace( /<div( [^>]*)?>\s*<p>/gi, '<div$1>\n\n' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        // Remove <p> and <br />
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 // Remove <p> and </p> tags.
</ins><span class="cx" style="display: block; padding: 0 10px">                         html = html.replace( /\s*<p>/gi, '' );
</span><span class="cx" style="display: block; padding: 0 10px">                        html = html.replace( /\s*<\/p>\s*/gi, '\n\n' );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                       // Remove white spaces between newlines. u00a0 is a no breaking space.
</ins><span class="cx" style="display: block; padding: 0 10px">                         html = html.replace( /\n[\s\u00a0]+\n/g, '\n\n' );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                       // Remove <br> tags.
</ins><span class="cx" style="display: block; padding: 0 10px">                         html = html.replace( /\s*<br ?\/?>\s*/gi, '\n' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        // Fix some block element newline issues
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 /*
+                        * Fix some block element newline issues.
+                        * Replace white spaces with newlines in combination with <div> tags.
+                        */
</ins><span class="cx" style="display: block; padding: 0 10px">                         html = html.replace( /\s*<div/g, '\n<div' );
</span><span class="cx" style="display: block; padding: 0 10px">                        html = html.replace( /<\/div>\s*/g, '</div>\n' );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                       // Replace white spaces with newlines in combination with [caption] shortcodes.
</ins><span class="cx" style="display: block; padding: 0 10px">                         html = html.replace( /\s*\[caption([^\[]+)\[\/caption\]\s*/gi, '\n\n[caption$1[/caption]\n\n' );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                       /*
+                        * Limit the newlines in combination with [caption]'s to a maximum of
+                        * two consecutive occurrences.
+                        * .
+                        */
</ins><span class="cx" style="display: block; padding: 0 10px">                         html = html.replace( /caption\]\n\n+\[caption/g, 'caption]\n\n[caption' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        /*
+                        * Replace white spaces with newlines in combination with
+                        * all elements listed in blocklist2.
+                        */
</ins><span class="cx" style="display: block; padding: 0 10px">                         html = html.replace( new RegExp('\\s*<((?:' + blocklist2 + ')(?: [^>]*)?)\\s*>', 'g' ), '\n<$1>' );
</span><span class="cx" style="display: block; padding: 0 10px">                        html = html.replace( new RegExp('\\s*</(' + blocklist2 + ')>\\s*', 'g' ), '</$1>\n' );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                       // Add indentation by adding a tab in front of <li>, <dt> and <dd> tags.
</ins><span class="cx" style="display: block; padding: 0 10px">                         html = html.replace( /<((li|dt|dd)[^>]*)>/g, ' \t<$1>' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        // Replace white spaces with newlines in combination with <select> and <option> tags.
</ins><span class="cx" style="display: block; padding: 0 10px">                         if ( html.indexOf( '<option' ) !== -1 ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                html = html.replace( /\s*<option/g, '\n<option' );
</span><span class="cx" style="display: block; padding: 0 10px">                                html = html.replace( /\s*<\/select>/g, '\n</select>' );
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        // Replace white spaces with 2 newlines in combination with <hr> tags.
</ins><span class="cx" style="display: block; padding: 0 10px">                         if ( html.indexOf( '<hr' ) !== -1 ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                html = html.replace( /\s*<hr( [^>]*)?>\s*/g, '\n\n<hr$1>\n\n' );
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        // Remove newlines in <object> tags.
</ins><span class="cx" style="display: block; padding: 0 10px">                         if ( html.indexOf( '<object' ) !== -1 ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                html = html.replace( /<object[\s\S]+?<\/object>/g, function( a ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                        return a.replace( /[\r\n]+/g, '' );
</span><span class="cx" style="display: block; padding: 0 10px">                                });
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        // Unmark special paragraph closing tags
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 // Unmark special paragraph closing tags.
</ins><span class="cx" style="display: block; padding: 0 10px">                         html = html.replace( /<\/p#>/g, '</p>\n' );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+
+                       // Add a new line before <p> tags when there is content inside the paragraph
</ins><span class="cx" style="display: block; padding: 0 10px">                         html = html.replace( /\s*(<p [^>]+>[\s\S]*?<\/p>)/g, '\n$1' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        // Trim whitespace
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 /*
+                        * Remove whitespaces at the start and end of a string.
+                        * u00a0 is a no breaking space.
+                        */
</ins><span class="cx" style="display: block; padding: 0 10px">                         html = html.replace( /^\s+/, '' );
</span><span class="cx" style="display: block; padding: 0 10px">                        html = html.replace( /[\s\u00a0]+$/, '' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        // put back the line breaks in pre|script
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 // Replace <wp-line-break> tags with a newline.
</ins><span class="cx" style="display: block; padding: 0 10px">                         if ( preserve_linebreaks ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                html = html.replace( /<wp-line-break>/g, '\n' );
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        // and the <br> tags in captions
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 // Restore the <wp-temp-br> with <br> tags.
</ins><span class="cx" style="display: block; padding: 0 10px">                         if ( preserve_br ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                html = html.replace( /<wp-temp-br([^>]*)>/g, '<br$1>' );
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        // Put back preserved tags.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 // Restore preserved tags.
</ins><span class="cx" style="display: block; padding: 0 10px">                         if ( preserve.length ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                html = html.replace( /<wp-preserve>/g, function() {
</span><span class="cx" style="display: block; padding: 0 10px">                                        return preserve.shift();
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -224,32 +352,52 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        return html;
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                // Similar to `wpautop()` in formatting.php
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         /**
+                * @summary Adds paragraph tags to the text.
+                *
+                * Adds paragraph tags to the text taking into account block level elements.
+                * Normalizes the whitespaces and newlines.
+                *
+                * Similar to `wpautop()` in formatting.php.
+                *
+                * @since 4.3.0
+                *
+                * @memberof switchEditors
+                *
+                * @param {string} text The text input.
+                * @returns {string} The formatted text.
+                */
</ins><span class="cx" style="display: block; padding: 0 10px">                 function autop( text ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        var preserve_linebreaks = false,
</span><span class="cx" style="display: block; padding: 0 10px">                                preserve_br = false,
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                               // A list containing all block level elements.
</ins><span class="cx" style="display: block; padding: 0 10px">                                 blocklist = 'table|thead|tfoot|caption|col|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre' +
</span><span class="cx" style="display: block; padding: 0 10px">                                        '|form|map|area|blockquote|address|math|style|p|h[1-6]|hr|fieldset|legend|section' +
</span><span class="cx" style="display: block; padding: 0 10px">                                        '|article|aside|hgroup|header|footer|nav|figure|figcaption|details|menu|summary';
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        // Normalize line breaks
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 // Normalize line breaks.
</ins><span class="cx" style="display: block; padding: 0 10px">                         text = text.replace( /\r\n|\r/g, '\n' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        // If there are no newlines, return the text.
</ins><span class="cx" style="display: block; padding: 0 10px">                         if ( text.indexOf( '\n' ) === -1 ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                return text;
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        if ( text.indexOf( '<object' ) !== -1 ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                               // If there are multiple newlines in an <object>, remove them.
</ins><span class="cx" style="display: block; padding: 0 10px">                                 text = text.replace( /<object[\s\S]+?<\/object>/g, function( a ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                        return a.replace( /\n+/g, '' );
</span><span class="cx" style="display: block; padding: 0 10px">                                });
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        // Replace all new lines and tabs with spaces inside tags.
</ins><span class="cx" style="display: block; padding: 0 10px">                         text = text.replace( /<[^<>]+>/g, function( a ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                return a.replace( /[\n\t ]+/g, ' ' );
</span><span class="cx" style="display: block; padding: 0 10px">                        });
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        // Protect pre|script tags
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 // Protect <pre> and <script> tags by replacing them with <wp-line-break>.
</ins><span class="cx" style="display: block; padding: 0 10px">                         if ( text.indexOf( '<pre' ) !== -1 || text.indexOf( '<script' ) !== -1 ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                preserve_linebreaks = true;
</span><span class="cx" style="display: block; padding: 0 10px">                                text = text.replace( /<(pre|script)[^>]*>[\s\S]*?<\/\1>/g, function( a ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -257,61 +405,138 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                });
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        // keep <br> tags inside captions and convert line breaks
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 // Keep <br> tags inside captions.
</ins><span class="cx" style="display: block; padding: 0 10px">                         if ( text.indexOf( '[caption' ) !== -1 ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                preserve_br = true;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                               // Replace all white spaces and <br> tags with <wp-temp-br>.
</ins><span class="cx" style="display: block; padding: 0 10px">                                 text = text.replace( /\[caption[\s\S]+?\[\/caption\]/g, function( a ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                        // keep existing <br>
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                                       // Protect <br> tags by converting them to <wp-temp-br> tags.
</ins><span class="cx" style="display: block; padding: 0 10px">                                         a = a.replace( /<br([^>]*)>/g, '<wp-temp-br$1>' );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                        // no line breaks inside HTML tags
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                                       // Replace all new lines and tabs with spaces inside HTML tags.
</ins><span class="cx" style="display: block; padding: 0 10px">                                         a = a.replace( /<[^<>]+>/g, function( b ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                                               // Replace newlines and tabs with a space.
</ins><span class="cx" style="display: block; padding: 0 10px">                                                 return b.replace( /[\n\t ]+/, ' ' );
</span><span class="cx" style="display: block; padding: 0 10px">                                        });
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                        // convert remaining line breaks to <br>
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                                       // Convert remaining line breaks to <wp-temp-br />.
</ins><span class="cx" style="display: block; padding: 0 10px">                                         return a.replace( /\s*\n\s*/g, '<wp-temp-br />' );
</span><span class="cx" style="display: block; padding: 0 10px">                                });
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        // Append 2 newlines at the end of the text.
</ins><span class="cx" style="display: block; padding: 0 10px">                         text = text + '\n\n';
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                       /*
+                        * Replace a <br> tag followed by 1 or more spaces
+                        * and another <br> tag with 2 newlines.
+                        */
</ins><span class="cx" style="display: block; padding: 0 10px">                         text = text.replace( /<br \/>\s*<br \/>/gi, '\n\n' );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                       /*
+                        * Replace a block level element open tag with 2 newlines
+                        * followed by the captured block level element.
+                        */
</ins><span class="cx" style="display: block; padding: 0 10px">                         text = text.replace( new RegExp( '(<(?:' + blocklist + ')(?: [^>]*)?>)', 'gi' ), '\n\n$1' );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                       /*
+                        * Replace a block level element closing tag with the captured
+                        * block level element followed by 2 newlines.
+                        */
</ins><span class="cx" style="display: block; padding: 0 10px">                         text = text.replace( new RegExp( '(</(?:' + blocklist + ')>)', 'gi' ), '$1\n\n' );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        text = text.replace( /<hr( [^>]*)?>/gi, '<hr$1>\n\n' ); // hr is self closing block element
-                       text = text.replace( /\s*<option/gi, '<option' ); // No <p> or <br> around <option>
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                       // Add 2 newlines to a <hr> tag. <hr> is a self closing block element.
+                       text = text.replace( /<hr( [^>]*)?>/gi, '<hr$1>\n\n' );
+
+                       // Remove the spaces before an <option> tag.
+                       text = text.replace( /\s*<option/gi, '<option' );
+
+                       // Remove the spaces after an <option> closing tag.
</ins><span class="cx" style="display: block; padding: 0 10px">                         text = text.replace( /<\/option>\s*/gi, '</option>' );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                       // Remove the spaces between two newlines.
</ins><span class="cx" style="display: block; padding: 0 10px">                         text = text.replace( /\n\s*\n+/g, '\n\n' );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                       // Convert 2 newlines to a paragraph and a single newline.
</ins><span class="cx" style="display: block; padding: 0 10px">                         text = text.replace( /([\s\S]+?)\n\n/g, '<p>$1</p>\n' );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                       // Remove empty paragraphs.
</ins><span class="cx" style="display: block; padding: 0 10px">                         text = text.replace( /<p>\s*?<\/p>/gi, '');
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                       // Remove spaces and <p> tags around block level elements.
</ins><span class="cx" style="display: block; padding: 0 10px">                         text = text.replace( new RegExp( '<p>\\s*(</?(?:' + blocklist + ')(?: [^>]*)?>)\\s*</p>', 'gi' ), '$1' );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                       // Remove <p> tags around li elements.
</ins><span class="cx" style="display: block; padding: 0 10px">                         text = text.replace( /<p>(<li.+?)<\/p>/gi, '$1');
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                       // Remove spaces and <p> tags from blockquotes.
</ins><span class="cx" style="display: block; padding: 0 10px">                         text = text.replace( /<p>\s*<blockquote([^>]*)>/gi, '<blockquote$1><p>');
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                       // Place the <blockquote> outside of the paragraph.
</ins><span class="cx" style="display: block; padding: 0 10px">                         text = text.replace( /<\/blockquote>\s*<\/p>/gi, '</p></blockquote>');
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                       // Remove spaces at the start and <p> tags from block level elements.
</ins><span class="cx" style="display: block; padding: 0 10px">                         text = text.replace( new RegExp( '<p>\\s*(</?(?:' + blocklist + ')(?: [^>]*)?>)', 'gi' ), '$1' );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                       // Remove spaces at the end and <p> tags from block level elements.
</ins><span class="cx" style="display: block; padding: 0 10px">                         text = text.replace( new RegExp( '(</?(?:' + blocklist + ')(?: [^>]*)?>)\\s*</p>', 'gi' ), '$1' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        // Remove redundant spaces and line breaks after existing <br /> tags
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 // Remove spaces and newlines after a <br> tag.
</ins><span class="cx" style="display: block; padding: 0 10px">                         text = text.replace( /(<br[^>]*>)\s*\n/gi, '$1' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        // Create <br /> from the remaining line breaks
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 // Replace spaces followed by a newline with a <br> tag followed by a new line.
</ins><span class="cx" style="display: block; padding: 0 10px">                         text = text.replace( /\s*\n/g, '<br />\n');
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        // Remove <br> tag that follows a block element directly, ignoring spaces.
</ins><span class="cx" style="display: block; padding: 0 10px">                         text = text.replace( new RegExp( '(</?(?:' + blocklist + ')[^>]*>)\\s*<br />', 'gi' ), '$1' );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                       /*
+                        * Remove a br tag preceding white spaces followed by a
+                        * <p>, <li>, <div>, <dl>, <dd>, <dt>, <th>, <pre>, <td>, <ul>, or <ol> tag.
+                        */
</ins><span class="cx" style="display: block; padding: 0 10px">                         text = text.replace( /<br \/>(\s*<\/?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)>)/gi, '$1' );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                       // Remove white spaces, <p> and <br> tags in captions.
</ins><span class="cx" style="display: block; padding: 0 10px">                         text = text.replace( /(?:<p>|<br ?\/?>)*\s*\[caption([^\[]+)\[\/caption\]\s*(?:<\/p>|<br ?\/?>)*/gi, '[caption$1[/caption]' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        /**
+                        * @summary Makes sure there is a paragraph open tag for a close tag.
+                        *
+                        * @since 2.9.0
+                        *
+                        * Makes sure there is a paragraph open tag when there is a paragraph close tag
+                        * in a div, th, td, form, fieldset or dd element.
+                        * @param {string} a The complete match.
+                        * @param {string} b The first capture group.
+                        * @param {string} c The second capture group.
+                        * @returns {string} The string in paragraph tags.
+                        */
</ins><span class="cx" style="display: block; padding: 0 10px">                         text = text.replace( /(<(?:div|th|td|form|fieldset|dd)[^>]*>)(.*?)<\/p>/g, function( a, b, c ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                               /*
+                                * Check if the matched group has a p open tag in it. If so, we don't need to
+                                * enclose it with a paragraph.
+                                */
</ins><span class="cx" style="display: block; padding: 0 10px">                                 if ( c.match( /<p( [^>]*)?>/ ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                        return a;
</span><span class="cx" style="display: block; padding: 0 10px">                                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                /*
+                                * If there is no p open tag in the matched string,
+                                * add it and return the string including p tags.
+                                */
</ins><span class="cx" style="display: block; padding: 0 10px">                                 return b + '<p>' + c + '</p>';
</span><span class="cx" style="display: block; padding: 0 10px">                        });
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        // put back the line breaks in pre|script
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 // Restore the line breaks in <pre> and <script> tags.
</ins><span class="cx" style="display: block; padding: 0 10px">                         if ( preserve_linebreaks ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                text = text.replace( /<wp-line-break>/g, '\n' );
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        // Restore the <br> tags in captions.
</ins><span class="cx" style="display: block; padding: 0 10px">                         if ( preserve_br ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                text = text.replace( /<wp-temp-br([^>]*)>/g, '<br$1>' );
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -319,7 +544,21 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        return text;
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                // Add old events
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         /**
+                * @summary Modifies the data when a switch is made from HTML to text.
+                *
+                * Modifies the data when a switch is made.
+                * Remove the <p> tags from text.
+                * Returns the modified text.
+                * Adds a trigger on beforePreWpautop and afterPreWpautop.
+                *
+                * @since 2.5.0
+                *
+                * @memberof switchEditors
+                *
+                * @param {String} html The content from the visual editor.
+                * @returns {String} the modified text.
+                */
</ins><span class="cx" style="display: block; padding: 0 10px">                 function pre_wpautop( html ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        var obj = { o: exports, data: html, unfiltered: html };
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -336,6 +575,19 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        return obj.data;
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                /**
+                * @summary Modifies the data when a switch is made from text to HTML.
+                *
+                * Modifies the data when a switch is made. Runs autop to add p tags from text.
+                * Returns the modified text. Adds a trigger on beforeWpautop and afterWpautop.
+                *
+                * @since 2.5.0
+                *
+                * @memberof switchEditors
+                *
+                * @param {String} text The content from the text editor.
+                * @returns {String} the modified text.
+                */
</ins><span class="cx" style="display: block; padding: 0 10px">                 function wpautop( text ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        var obj = { o: exports, data: text, unfiltered: text };
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -352,12 +604,18 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        return obj.data;
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                // Bind the init function to be run when the document is loaded.
</ins><span class="cx" style="display: block; padding: 0 10px">                 if ( $ ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $( document ).ready( init );
</span><span class="cx" style="display: block; padding: 0 10px">                } else if ( document.addEventListener ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                       // Use the addEventListener to bind the init event on document load.
</ins><span class="cx" style="display: block; padding: 0 10px">                         document.addEventListener( 'DOMContentLoaded', init, false );
</span><span class="cx" style="display: block; padding: 0 10px">                        window.addEventListener( 'load', init, false );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px">                 } else if ( window.attachEvent ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                       // Use the addEvent to bind the init event on document load.
</ins><span class="cx" style="display: block; padding: 0 10px">                         window.attachEvent( 'onload', init );
</span><span class="cx" style="display: block; padding: 0 10px">                        document.attachEvent( 'onreadystatechange', function() {
</span><span class="cx" style="display: block; padding: 0 10px">                                if ( 'complete' === document.readyState ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -366,6 +624,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        } );
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                /*
+                * Make sure the window.wp object exists so autop and removep
+                * can be bound to it.
+                */
</ins><span class="cx" style="display: block; padding: 0 10px">                 window.wp = window.wp || {};
</span><span class="cx" style="display: block; padding: 0 10px">                window.wp.editor = window.wp.editor || {};
</span><span class="cx" style="display: block; padding: 0 10px">                window.wp.editor.autop = wpautop;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -382,5 +644,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">                return exports;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        /**
+        * @namespace {SwitchEditors} switchEditors
+        * Expose the switch editors to be used globally.
+        */
</ins><span class="cx" style="display: block; padding: 0 10px">         window.switchEditors = new SwitchEditors();
</span><span class="cx" style="display: block; padding: 0 10px"> }( window.jQuery ));
</span></span></pre>
</div>
</div>

</body>
</html>