Make WordPress Core

Ticket #38933: editorjs.diff

File editorjs.diff, 20.2 KB (added by terwdan, 8 years ago)
  • src/wp-admin/js/editor.js

    diff --git a/src/wp-admin/js/editor.js b/src/wp-admin/js/editor.js
    index e3fbaab..e50799c 100644
    a b  
    11
    22( function( $ ) {
     3        /**
     4         * @summary Creates the tinyMCE editors.
     5         *
     6         * Creates the tinyMCE editor and binds all events used for switching
     7         * from visual to text mode.
     8         *
     9         * @since 3.7.9
     10         *
     11         * @class
     12         */
    313        function SwitchEditors() {
    414                var tinymce, $$,
    515                        exports = {};
    616
     17                /**
     18                 * @summary Initializes the event binding for switching editors.
     19                 *
     20                 * @since 3.7.10
     21                 *
     22                 * @returns {void}
     23                 */
    724                function init() {
    825                        if ( ! tinymce && window.tinymce ) {
    926                                tinymce = window.tinymce;
    1027                                $$ = tinymce.$;
    1128
     29                                /**
     30                                 * @summary Handles onclick events for the editor buttons.
     31                                 *
     32                                 * @since 3.7.10
     33                                 *
     34                                 * Handles an onclick event on the document.
     35                                 * Switches the editor between visual and text,
     36                                 * if the clicked element has the 'wp-switch-editor' class.
     37                                 * If the class name is switch-html switches to the HTML editor,
     38                                 * if the class name is switch-tmce
     39                                 * switches to the TMCE editor.
     40                                 *
     41                                 * @returns {void}
     42                                 */
    1243                                $$( document ).on( 'click', function( event ) {
    1344                                        var id, mode,
    1445                                                target = $$( event.target );
     
    2253                        }
    2354                }
    2455
     56                /**
     57                 * @summary Retrieves the height of the toolbar based on the container the
     58                 * editor is placed in.
     59                 *
     60                 * @since 3.7.2
     61                 *
     62                 * @param {Object} editor The tinyMCE editor.
     63                 * @returns {number} If the height is between 10 and 200 return the height,
     64                 * else return 30.
     65                 */
    2566                function getToolbarHeight( editor ) {
    2667                        var node = $$( '.mce-toolbar-grp', editor.getContainer() )[0],
    2768                                height = node && node.clientHeight;
     
    3374                        return 30;
    3475                }
    3576
     77                /**
     78                 * @summary Switches the editor between visual and text.
     79                 *
     80                 * @since 2.5
     81                 *
     82                 * @memberof switchEditors
     83                 *
     84                 * @param {string} id The id of the editor you want to change the editor mode for.
     85                 * If no id is given, it defaults to content.
     86                 * @param {string} mode The mode you want to switch to.
     87                 * If an undefined mode is given, it defaults to toggle.
     88                 * @returns {void}
     89                 */
    3690                function switchEditor( id, mode ) {
    3791                        id = id || 'content';
    3892                        mode = mode || 'toggle';
     
    4397                                $textarea = $$( '#' + id ),
    4498                                textarea = $textarea[0];
    4599
     100                        // Toggles the mode between visual and textual representation.
    46101                        if ( 'toggle' === mode ) {
    47102                                if ( editor && ! editor.isHidden() ) {
    48103                                        mode = 'html';
     
    51106                                }
    52107                        }
    53108
     109
     110                         // If the mode is tmce or tinymce, show the editor.
    54111                        if ( 'tmce' === mode || 'tinymce' === mode ) {
     112
     113                                /*
     114                                 * If the editor isn't hidden we are already in tmce mode
     115                                 * and we don't need to switch.
     116                                 * Returns false to stop event bubbling.
     117                                 */
    55118                                if ( editor && ! editor.isHidden() ) {
    56119                                        return false;
    57120                                }
    58121
     122                                // Closes the QuickTags toolbars if they are visible.
    59123                                if ( typeof( window.QTags ) !== 'undefined' ) {
    60124                                        window.QTags.closeAllTags( id );
    61125                                }
     
    65129                                if ( editor ) {
    66130                                        editor.show();
    67131
    68                                         // No point resizing the iframe in iOS
     132                                        // Don't resize the iframe in iOS.
    69133                                        if ( ! tinymce.Env.iOS && editorHeight ) {
    70134                                                toolbarHeight = getToolbarHeight( editor );
    71135                                                editorHeight = editorHeight - toolbarHeight + 14;
    72136
    73                                                 // height cannot be under 50 or over 5000
     137                                                // Height must be between 50 and 5000.
    74138                                                if ( editorHeight > 50 && editorHeight < 5000 ) {
    75139                                                        editor.theme.resizeTo( null, editorHeight );
    76140                                                }
     
    83147                                $textarea.attr( 'aria-hidden', true );
    84148                                window.setUserSetting( 'editor', 'tinymce' );
    85149
     150                        // Hides the editor if mode is html.
    86151                        } else if ( 'html' === mode ) {
     152
     153                                /*
     154                                 * If the editor is hidden we are already in html mode and
     155                                 * we don't need to switch.
     156                                 * Returns false to stop event bubbling.
     157                                 */
    87158                                if ( editor && editor.isHidden() ) {
    88159                                        return false;
    89160                                }
    90161
    91162                                if ( editor ) {
     163
     164                                        // Don't resize the iframe in iOS.
    92165                                        if ( ! tinymce.Env.iOS ) {
    93166                                                iframe = editor.iframeElement;
    94167                                                editorHeight = iframe ? parseInt( iframe.style.height, 10 ) : 0;
     
    97170                                                        toolbarHeight = getToolbarHeight( editor );
    98171                                                        editorHeight = editorHeight + toolbarHeight - 14;
    99172
    100                                                         // height cannot be under 50 or over 5000
     173                                                        // Height must be between 50 and 5000.
    101174                                                        if ( editorHeight > 50 && editorHeight < 5000 ) {
    102175                                                                textarea.style.height = editorHeight + 'px';
    103176                                                        }
     
    106179
    107180                                        editor.hide();
    108181                                } else {
    109                                         // The TinyMCE instance doesn't exist, show the textarea
     182                                        // The TinyMCE instance doesn't exist, show the textarea.
    110183                                        $textarea.css({ 'display': '', 'visibility': '' });
    111184                                }
    112185
     
    116189                        }
    117190                }
    118191
    119                 // Replace paragraphs with double line breaks
     192                /**
     193                 * @summary Replaces all paragraphs with double line breaks.
     194                 *
     195                 * Replaces all paragraphs with double line breaks. Taking into account
     196                 * the elements where the <p> should be preserved.
     197                 * Unifies all whitespaces.
     198                 * Adds indenting with tabs to li, dt and dd elements.
     199                 * Trims whitespaces from beginning and end of the html input.
     200                 *
     201                 * @since 3.7.10
     202                 *
     203                 * @memberof switchEditors
     204                 *
     205                 * @param {string} html The content from the editor.
     206                 * @return {string} The formatted html string.
     207                 */
    120208                function removep( html ) {
    121209                        var blocklist = 'blockquote|ul|ol|li|dl|dt|dd|table|thead|tbody|tfoot|tr|th|td|h[1-6]|fieldset',
    122210                                blocklist1 = blocklist + '|div|p',
     
    129217                                return '';
    130218                        }
    131219
    132                         // Preserve script and style tags.
     220                        /*
     221                         * Protects script and style tags by replacing them with <wp-preserve>.
     222                         * Pushes matches into the preserve array.
     223                         */
    133224                        if ( html.indexOf( '<script' ) !== -1 || html.indexOf( '<style' ) !== -1 ) {
    134225                                html = html.replace( /<(script|style)[^>]*>[\s\S]*?<\/\1>/g, function( match ) {
    135226                                        preserve.push( match );
     
    137228                                } );
    138229                        }
    139230
    140                         // Protect pre tags.
     231                        /*
     232                         * Protects pre tags by replacing all newlines and
     233                         * <br> tags with <wp-line-break>.
     234                         */
    141235                        if ( html.indexOf( '<pre' ) !== -1 ) {
    142236                                preserve_linebreaks = true;
    143237                                html = html.replace( /<pre[^>]*>[\s\S]+?<\/pre>/g, function( a ) {
     
    147241                                });
    148242                        }
    149243
    150                         // keep <br> tags inside captions and remove line breaks
     244                        /*
     245                         * Keeps <br> tags inside captions and remove line breaks by replacing
     246                         * them with <wp-temp-br>.
     247                         */
    151248                        if ( html.indexOf( '[caption' ) !== -1 ) {
    152249                                preserve_br = true;
    153250                                html = html.replace( /\[caption[\s\S]+?\[\/caption\]/g, function( a ) {
     
    155252                                });
    156253                        }
    157254
    158                         // Pretty it up for the source editor
     255                        // Formats the text to be readable in the source editor.
    159256                        html = html.replace( new RegExp( '\\s*</(' + blocklist1 + ')>\\s*', 'g' ), '</$1>\n' );
    160257                        html = html.replace( new RegExp( '\\s*<((?:' + blocklist1 + ')(?: [^>]*)?)>', 'g' ), '\n<$1>' );
    161258
    162                         // Mark </p> if it has any attributes.
     259                        // Marks </p> if it has any attributes.
    163260                        html = html.replace( /(<p [^>]+>.*?)<\/p>/g, '$1</p#>' );
    164261
    165                         // Separate <div> containing <p>
     262                        // If the content of a container starts with a paragraph, replace the <p> tag with 2 newlines.
    166263                        html = html.replace( /<div( [^>]*)?>\s*<p>/gi, '<div$1>\n\n' );
    167264
    168                         // Remove <p> and <br />
     265                        // Removes <p> and </p> tags.
    169266                        html = html.replace( /\s*<p>/gi, '' );
    170267                        html = html.replace( /\s*<\/p>\s*/gi, '\n\n' );
     268
     269                        // Removes white spaces between newlines. u00a0 is a no breaking space.
    171270                        html = html.replace( /\n[\s\u00a0]+\n/g, '\n\n' );
     271
     272                        // Removes <br> tags.
    172273                        html = html.replace( /\s*<br ?\/?>\s*/gi, '\n' );
    173274
    174                         // Fix some block element newline issues
     275                        /*
     276                         * Fixes some block element newline issues.
     277                         * Replaces white spaces with newlines in combination with <div> tags.
     278                         */
    175279                        html = html.replace( /\s*<div/g, '\n<div' );
    176280                        html = html.replace( /<\/div>\s*/g, '</div>\n' );
     281
     282                        // Replaces white spaces with newlines in combination with [caption] shortcodes.
    177283                        html = html.replace( /\s*\[caption([^\[]+)\[\/caption\]\s*/gi, '\n\n[caption$1[/caption]\n\n' );
     284
     285                        /*
     286                         * Limits the newlines in combination with [caption]'s to a maximum of
     287                         * two consecutive occurrences.
     288                         * .
     289                         */
    178290                        html = html.replace( /caption\]\n\n+\[caption/g, 'caption]\n\n[caption' );
    179291
     292                        /*
     293                         * Replaces white spaces with newlines in combination with
     294                         * all elements listed in blocklist2.
     295                         */
    180296                        html = html.replace( new RegExp('\\s*<((?:' + blocklist2 + ')(?: [^>]*)?)\\s*>', 'g' ), '\n<$1>' );
    181297                        html = html.replace( new RegExp('\\s*</(' + blocklist2 + ')>\\s*', 'g' ), '</$1>\n' );
     298
     299                        // Adds indentation by adding a tab in front of <li>, <dt> and <dd> tags.
    182300                        html = html.replace( /<((li|dt|dd)[^>]*)>/g, ' \t<$1>' );
    183301
     302                        // Replaces white spaces with newlines in combination with <select> and <option> tags.
    184303                        if ( html.indexOf( '<option' ) !== -1 ) {
    185304                                html = html.replace( /\s*<option/g, '\n<option' );
    186305                                html = html.replace( /\s*<\/select>/g, '\n</select>' );
    187306                        }
    188307
     308                        // Replaces white spaces with 2 newlines in combination with <hr> tags.
    189309                        if ( html.indexOf( '<hr' ) !== -1 ) {
    190310                                html = html.replace( /\s*<hr( [^>]*)?>\s*/g, '\n\n<hr$1>\n\n' );
    191311                        }
    192312
     313                        // Removes newlines in <object> tags.
    193314                        if ( html.indexOf( '<object' ) !== -1 ) {
    194315                                html = html.replace( /<object[\s\S]+?<\/object>/g, function( a ) {
    195316                                        return a.replace( /[\r\n]+/g, '' );
    196317                                });
    197318                        }
    198319
    199                         // Unmark special paragraph closing tags
     320                        // Unmarks special paragraph closing tags.
    200321                        html = html.replace( /<\/p#>/g, '</p>\n' );
     322
     323
     324                        // Adds a new line before <p> tags when there is content inside the paragraph
    201325                        html = html.replace( /\s*(<p [^>]+>[\s\S]*?<\/p>)/g, '\n$1' );
    202326
    203                         // Trim whitespace
     327                        /*
     328                         * Removes whitespaces at the start and end of a string.
     329                         * u00a0 is a no breaking space.
     330                         */
    204331                        html = html.replace( /^\s+/, '' );
    205332                        html = html.replace( /[\s\u00a0]+$/, '' );
    206333
    207                         // put back the line breaks in pre|script
     334                        // Replaces <wp-line-break> tags with a newline.
    208335                        if ( preserve_linebreaks ) {
    209336                                html = html.replace( /<wp-line-break>/g, '\n' );
    210337                        }
    211338
    212                         // and the <br> tags in captions
     339                        // Restores the <wp-temp-br> with <br> tags.
    213340                        if ( preserve_br ) {
    214341                                html = html.replace( /<wp-temp-br([^>]*)>/g, '<br$1>' );
    215342                        }
    216343
    217                         // Put back preserved tags.
     344                        // Restores preserved tags.
    218345                        if ( preserve.length ) {
    219346                                html = html.replace( /<wp-preserve>/g, function() {
    220347                                        return preserve.shift();
     
    224351                        return html;
    225352                }
    226353
    227                 // Similar to `wpautop()` in formatting.php
     354                /**
     355                 * @summary Adds paragraph tags to the text.
     356                 *
     357                 * Adds paragraph tags to the text taking into account block level elements.
     358                 * Normalizes the whitespaces and newlines.
     359                 *
     360                 * Similar to `wpautop()` in formatting.php.
     361                 *
     362                 * @since 3.7.10
     363                 *
     364                 * @memberof switchEditors
     365                 *
     366                 * @param {string} text The text input.
     367                 * @returns {string} The formatted text.
     368                 */
    228369                function autop( text ) {
    229370                        var preserve_linebreaks = false,
    230371                                preserve_br = false,
     372
     373                                // A list containing all block level elements.
    231374                                blocklist = 'table|thead|tfoot|caption|col|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre' +
    232375                                        '|form|map|area|blockquote|address|math|style|p|h[1-6]|hr|fieldset|legend|section' +
    233376                                        '|article|aside|hgroup|header|footer|nav|figure|figcaption|details|menu|summary';
    234377
    235                         // Normalize line breaks
     378                        // Normalizes line breaks.
    236379                        text = text.replace( /\r\n|\r/g, '\n' );
    237380
     381                        // If there are no newlines, return the text.
    238382                        if ( text.indexOf( '\n' ) === -1 ) {
    239383                                return text;
    240384                        }
    241385
    242386                        if ( text.indexOf( '<object' ) !== -1 ) {
     387
     388                                // If there are multiple newlines in an <object>, remove them.
    243389                                text = text.replace( /<object[\s\S]+?<\/object>/g, function( a ) {
    244390                                        return a.replace( /\n+/g, '' );
    245391                                });
    246392                        }
    247393
     394                        // Replaces all new lines and tabs with spaces inside tags.
    248395                        text = text.replace( /<[^<>]+>/g, function( a ) {
    249396                                return a.replace( /[\n\t ]+/g, ' ' );
    250397                        });
    251398
    252                         // Protect pre|script tags
     399                        // Protects <pre> and <script> tags by replacing them with <wp-line-break>.
    253400                        if ( text.indexOf( '<pre' ) !== -1 || text.indexOf( '<script' ) !== -1 ) {
    254401                                preserve_linebreaks = true;
    255402                                text = text.replace( /<(pre|script)[^>]*>[\s\S]*?<\/\1>/g, function( a ) {
     
    257404                                });
    258405                        }
    259406
    260                         // keep <br> tags inside captions and convert line breaks
     407                        // Keeps <br> tags inside captions.
    261408                        if ( text.indexOf( '[caption' ) !== -1 ) {
    262409                                preserve_br = true;
     410
     411                                // Replaces all white spaces and <br> tags with <wp-temp-br>.
    263412                                text = text.replace( /\[caption[\s\S]+?\[\/caption\]/g, function( a ) {
    264                                         // keep existing <br>
     413
     414                                        // Protects <br> tags by converting them to <wp-temp-br> tags.
    265415                                        a = a.replace( /<br([^>]*)>/g, '<wp-temp-br$1>' );
    266                                         // no line breaks inside HTML tags
     416
     417                                        // Replaces all new lines and tabs with spaces inside HTML tags.
    267418                                        a = a.replace( /<[^<>]+>/g, function( b ) {
     419
     420                                                // Replaces newlines and tabs with a space.
    268421                                                return b.replace( /[\n\t ]+/, ' ' );
    269422                                        });
    270                                         // convert remaining line breaks to <br>
     423
     424                                        // Converts remaining line breaks to <wp-temp-br />.
    271425                                        return a.replace( /\s*\n\s*/g, '<wp-temp-br />' );
    272426                                });
    273427                        }
    274428
     429                        // Appends 2 newlines at the end of the text.
    275430                        text = text + '\n\n';
     431
     432                        /*
     433                         * Replaces a <br> tag followed by 1 or more spaces
     434                         * and another <br> tag with 2 newlines.
     435                         */
    276436                        text = text.replace( /<br \/>\s*<br \/>/gi, '\n\n' );
     437
     438                        /*
     439                         * Replaces a block level element open tag with 2 newlines
     440                         * followed by the captured block level element.
     441                         */
    277442                        text = text.replace( new RegExp( '(<(?:' + blocklist + ')(?: [^>]*)?>)', 'gi' ), '\n\n$1' );
     443
     444                        /*
     445                         * Replaces a block level element closing tag with the captured
     446                         * block level element followed by 2 newlines.
     447                         */
    278448                        text = text.replace( new RegExp( '(</(?:' + blocklist + ')>)', 'gi' ), '$1\n\n' );
    279                         text = text.replace( /<hr( [^>]*)?>/gi, '<hr$1>\n\n' ); // hr is self closing block element
    280                         text = text.replace( /\s*<option/gi, '<option' ); // No <p> or <br> around <option>
     449
     450                        // Adds 2 newlines to a <hr> tag. <hr> is a self closing block element.
     451                        text = text.replace( /<hr( [^>]*)?>/gi, '<hr$1>\n\n' );
     452
     453                        // Removes the spaces before an <option> tag.
     454                        text = text.replace( /\s*<option/gi, '<option' );
     455
     456                        // Removes the spaces after an <option> closing tag.
    281457                        text = text.replace( /<\/option>\s*/gi, '</option>' );
     458
     459                        // Removes the spaces between two newlines.
    282460                        text = text.replace( /\n\s*\n+/g, '\n\n' );
     461
     462                        // Converts 2 newlines to a paragraph and a single newline.
    283463                        text = text.replace( /([\s\S]+?)\n\n/g, '<p>$1</p>\n' );
     464
     465                        // Removes empty paragraphs.
    284466                        text = text.replace( /<p>\s*?<\/p>/gi, '');
     467
     468                        // Removes spaces and <p> tags around block level elements.
    285469                        text = text.replace( new RegExp( '<p>\\s*(</?(?:' + blocklist + ')(?: [^>]*)?>)\\s*</p>', 'gi' ), '$1' );
     470
     471                        // Removes <p> tags around li elements.
    286472                        text = text.replace( /<p>(<li.+?)<\/p>/gi, '$1');
     473
     474                        // Removes spaces and <p> tags from blockquotes.
    287475                        text = text.replace( /<p>\s*<blockquote([^>]*)>/gi, '<blockquote$1><p>');
     476
     477                        // Places the <blockquote> outside of the paragraph.
    288478                        text = text.replace( /<\/blockquote>\s*<\/p>/gi, '</p></blockquote>');
     479
     480                        // Removes spaces at the start and <p> tags from block level elements.
    289481                        text = text.replace( new RegExp( '<p>\\s*(</?(?:' + blocklist + ')(?: [^>]*)?>)', 'gi' ), '$1' );
     482
     483                        // Removes spaces at the end and <p> tags from block level elements.
    290484                        text = text.replace( new RegExp( '(</?(?:' + blocklist + ')(?: [^>]*)?>)\\s*</p>', 'gi' ), '$1' );
    291485
    292                         // Remove redundant spaces and line breaks after existing <br /> tags
     486                        // Removes spaces and newlines after a <br> tag.
    293487                        text = text.replace( /(<br[^>]*>)\s*\n/gi, '$1' );
    294488
    295                         // Create <br /> from the remaining line breaks
     489                        // Replaces spaces followed by a newline with a <br> tag followed by a new line.
    296490                        text = text.replace( /\s*\n/g, '<br />\n');
    297491
     492                        // Removes <br> tag that follows a block element directly, ignoring spaces.
    298493                        text = text.replace( new RegExp( '(</?(?:' + blocklist + ')[^>]*>)\\s*<br />', 'gi' ), '$1' );
     494
     495                        /*
     496                         * Removes a br tag preceding white spaces followed by a
     497                         * <p>, <li>, <div>, <dl>, <dd>, <dt>, <th>, <pre>, <td>, <ul>, or <ol> tag.
     498                         */
    299499                        text = text.replace( /<br \/>(\s*<\/?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)>)/gi, '$1' );
     500
     501                        // Removes white spaces, <p> and <br> tags in captions.
    300502                        text = text.replace( /(?:<p>|<br ?\/?>)*\s*\[caption([^\[]+)\[\/caption\]\s*(?:<\/p>|<br ?\/?>)*/gi, '[caption$1[/caption]' );
    301503
     504                        /**
     505                         * @summary Makes sure there is a paragraph open tag for a close tag.
     506                         *
     507                         * @since 3.5.2
     508                         *
     509                         * Makes sure there is a paragraph open tag when there is a paragraph close tag
     510                         * in a div, th, td, form, fieldset or dd element.
     511                         * @param {string} a The complete match.
     512                         * @param {string} b The first capture group.
     513                         * @param {string} c The second capture group.
     514                         * @returns {string} The string in paragraph tags.
     515                         */
    302516                        text = text.replace( /(<(?:div|th|td|form|fieldset|dd)[^>]*>)(.*?)<\/p>/g, function( a, b, c ) {
     517
     518                                /*
     519                                 * Checks if the matched group has a p open tag in it. If so, we don't need to
     520                                 * enclose it with a paragraph.
     521                                 */
    303522                                if ( c.match( /<p( [^>]*)?>/ ) ) {
    304523                                        return a;
    305524                                }
    306525
     526                                /*
     527                                 * If there is no p open tag in the matched string,
     528                                 * add it and return the string including p tags.
     529                                 */
    307530                                return b + '<p>' + c + '</p>';
    308531                        });
    309532
    310                         // put back the line breaks in pre|script
     533                        // Restores the line breaks in <pre> and <script> tags.
    311534                        if ( preserve_linebreaks ) {
    312535                                text = text.replace( /<wp-line-break>/g, '\n' );
    313536                        }
    314537
     538                        // Restores the <br> tags in captions.
    315539                        if ( preserve_br ) {
    316540                                text = text.replace( /<wp-temp-br([^>]*)>/g, '<br$1>' );
    317541                        }
     
    319543                        return text;
    320544                }
    321545
    322                 // Add old events
     546                /**
     547                 * @summary Modifies the data when a switch is made from HTML to text.
     548                 *
     549                 * Modifies the data when a switch is made.
     550                 * Remove the <p> tags from text.
     551                 * Returns the modified text.
     552                 * Adds a trigger on beforePreWpautop and afterPreWpautop.
     553                 *
     554                 * @since 3.5.2
     555                 *
     556                 * @memberof switchEditors
     557                 *
     558                 * @param {String} html The content from the visual editor.
     559                 * @returns {String} the modified text.
     560                 */
    323561                function pre_wpautop( html ) {
    324562                        var obj = { o: exports, data: html, unfiltered: html };
    325563
     
    336574                        return obj.data;
    337575                }
    338576
     577                /**
     578                 * @summary Modifies the data when a switch is made from text to HTML.
     579                 *
     580                 * Modifies the data when a switch is made. Runs autop to add p tags from text.
     581                 * Returns the modified text. Adds a trigger on beforeWpautop and afterWpautop.
     582                 *
     583                 * @since 3.5.2
     584                 *
     585                 * @memberof switchEditors
     586                 *
     587                 * @param {String} text The content from the text editor.
     588                 * @returns {String} the modified text.
     589                 */
    339590                function wpautop( text ) {
    340591                        var obj = { o: exports, data: text, unfiltered: text };
    341592
     
    352603                        return obj.data;
    353604                }
    354605
     606                // Binds the init function to be run when the document is loaded.
    355607                if ( $ ) {
    356608                        $( document ).ready( init );
    357609                } else if ( document.addEventListener ) {
     610
     611                        // Uses the addEventListener to bind the init event on document load.
    358612                        document.addEventListener( 'DOMContentLoaded', init, false );
    359613                        window.addEventListener( 'load', init, false );
     614
    360615                } else if ( window.attachEvent ) {
     616
     617                        // Uses the addEvent to bind the init event on document load.
    361618                        window.attachEvent( 'onload', init );
    362619                        document.attachEvent( 'onreadystatechange', function() {
    363620                                if ( 'complete' === document.readyState ) {
     
    366623                        } );
    367624                }
    368625
     626                /*
     627                 * Makes sure the window.wp object exists so autop and removep
     628                 * can be bound to it.
     629                 */
    369630                window.wp = window.wp || {};
    370631                window.wp.editor = window.wp.editor || {};
    371632                window.wp.editor.autop = wpautop;
     
    382643                return exports;
    383644        }
    384645
     646        /**
     647         * @namespace {SwitchEditors} switchEditors
     648         * Expose the switch editors to be used globally.
     649         */
    385650        window.switchEditors = new SwitchEditors();
    386651}( window.jQuery ));