Make WordPress Core

Ticket #25947: 25947.3.diff

File 25947.3.diff, 15.6 KB (added by dougwollison, 13 years ago)

Corrected handling of globals. 1 error left; 'switchEditors' is defined but never used

  • src/wp-admin/js/editor.js

     
     1/* global tinymce:false */
     2/* global tinymcePreInit:false */
     3/* global QTags:false */
     4/* global setUserSetting:false */
    15
    26var switchEditors = {
    37
    4         switchto: function(el) {
    5                 var aid = el.id, l = aid.length, id = aid.substr(0, l - 5), mode = aid.substr(l - 4);
     8        switchto: function( el ) {
     9                var aid = el.id,
     10                        l = aid.length,
     11                        id = aid.substr( 0, l - 5 ),
     12                        mode = aid.substr( l - 4 );
    613
    7                 this.go(id, mode);
     14                this.go( id, mode );
    815        },
    916
    10         go: function(id, mode) { // mode can be 'html', 'tmce', or 'toggle'; 'html' is used for the "Text" editor tab.
     17        go: function( id, mode ) { // mode can be 'html', 'tmce', or 'toggle'; 'html' is used for the 'Text' editor tab.
    1118                id = id || 'content';
    1219                mode = mode || 'toggle';
    1320
    14                 var t = this, ed = tinyMCE.get(id), wrap_id, txtarea_el, dom = tinymce.DOM;
     21                var t = this, ed = tinymce.get( id ), wrap_id, txtarea_el, dom = tinymce.DOM;
    1522
    16                 wrap_id = 'wp-'+id+'-wrap';
    17                 txtarea_el = dom.get(id);
     23                wrap_id = 'wp-' + id + '-wrap';
     24                txtarea_el = dom.get( id );
    1825
    19                 if ( 'toggle' == mode ) {
    20                         if ( ed && !ed.isHidden() )
     26                if ( 'toggle' === mode ) {
     27                        if ( ed && ! ed.isHidden() ) {
    2128                                mode = 'html';
    22                         else
     29                        } else {
    2330                                mode = 'tmce';
     31                        }
    2432                }
    2533
    26                 if ( 'tmce' == mode || 'tinymce' == mode ) {
    27                         if ( ed && ! ed.isHidden() )
     34                if ( 'tmce' === mode || 'tinymce' === mode ) {
     35                        if ( ed && ! ed.isHidden() ) {
    2836                                return false;
     37                        }
    2938
    30                         if ( typeof(QTags) != 'undefined' )
    31                                 QTags.closeAllTags(id);
     39                        if ( typeof( QTags ) !== 'undefined' ) {
     40                                QTags.closeAllTags( id );
     41                        }
    3242
    33                         if ( tinyMCEPreInit.mceInit[id] && tinyMCEPreInit.mceInit[id].wpautop )
     43                        if ( tinymcePreInit.mceInit[ id ] && tinymcePreInit.mceInit[ id ].wpautop ) {
    3444                                txtarea_el.value = t.wpautop( txtarea_el.value );
     45                        }
    3546
    3647                        if ( ed ) {
    3748                                ed.show();
    3849                        } else {
    39                                 ed = new tinymce.Editor(id, tinyMCEPreInit.mceInit[id]);
     50                                ed = new tinymce.Editor( id, tinymcePreInit.mceInit[ id ] );
    4051                                ed.render();
    4152                        }
    4253
    43                         dom.removeClass(wrap_id, 'html-active');
    44                         dom.addClass(wrap_id, 'tmce-active');
    45                         setUserSetting('editor', 'tinymce');
     54                        dom.removeClass( wrap_id, 'html-active' );
     55                        dom.addClass( wrap_id, 'tmce-active' );
     56                        setUserSetting( 'editor', 'tinymce' );
    4657
    47                 } else if ( 'html' == mode ) {
     58                } else if ( 'html' === mode ) {
    4859
    49                         if ( ed && ed.isHidden() )
     60                        if ( ed && ed.isHidden() ) {
    5061                                return false;
     62                        }
    5163
    5264                        if ( ed ) {
    5365                                ed.hide();
    5466                        } else {
    55                                 // The TinyMCE instance doesn't exist, run the content through "pre_wpautop()" and show the textarea
    56                                 if ( tinyMCEPreInit.mceInit[id] && tinyMCEPreInit.mceInit[id].wpautop )
     67                                // The TinyMCE instance doesn't exist, run the content through 'pre_wpautop()' and show the textarea
     68                                if ( tinymcePreInit.mceInit[ id ] && tinymcePreInit.mceInit[ id ].wpautop ) {
    5769                                        txtarea_el.value = t.pre_wpautop( txtarea_el.value );
     70                                }
    5871
    59                                 dom.setStyles(txtarea_el, {'display': '', 'visibility': ''});
     72                                dom.setStyles( txtarea_el, {'display': '', 'visibility': ''} );
    6073                        }
    6174
    62                         dom.removeClass(wrap_id, 'tmce-active');
    63                         dom.addClass(wrap_id, 'html-active');
    64                         setUserSetting('editor', 'html');
     75                        dom.removeClass( wrap_id, 'tmce-active' );
     76                        dom.addClass( wrap_id, 'html-active' );
     77                        setUserSetting( 'editor', 'html' );
    6578                }
    6679                return false;
    6780        },
    6881
    69         _wp_Nop : function(content) {
    70                 var blocklist1, blocklist2, preserve_linebreaks = false, preserve_br = false;
     82        _wp_Nop: function( content ) {
     83                var blocklist1,
     84                        blocklist2,
     85                        preserve_linebreaks = false,
     86                        preserve_br = false;
    7187
    7288                // Protect pre|script tags
    73                 if ( content.indexOf('<pre') != -1 || content.indexOf('<script') != -1 ) {
     89                if ( content.indexOf( '<pre' ) !== -1 || content.indexOf( '<script' ) !== -1 ) {
    7490                        preserve_linebreaks = true;
    75                         content = content.replace(/<(pre|script)[^>]*>[\s\S]+?<\/\1>/g, function(a) {
    76                                 a = a.replace(/<br ?\/?>(\r\n|\n)?/g, '<wp-temp-lb>');
    77                                 return a.replace(/<\/?p( [^>]*)?>(\r\n|\n)?/g, '<wp-temp-lb>');
     91                        content = content.replace( /<(pre|script)[^>]*>[\s\S]+?<\/\1>/g, function( a ) {
     92                                a = a.replace( /<br ?\/?>(\r\n|\n)?/g, '<wp-temp-lb>' );
     93                                return a.replace( /<\/?p( [^>]*)?>(\r\n|\n)?/g, '<wp-temp-lb>' );
    7894                        });
    7995                }
    8096
    8197                // keep <br> tags inside captions and remove line breaks
    82                 if ( content.indexOf('[caption') != -1 ) {
     98                if ( content.indexOf( '[caption' ) !== -1 ) {
    8399                        preserve_br = true;
    84                         content = content.replace(/\[caption[\s\S]+?\[\/caption\]/g, function(a) {
    85                                 return a.replace(/<br([^>]*)>/g, '<wp-temp-br$1>').replace(/[\r\n\t]+/, '');
     100                        content = content.replace( /\[caption[\s\S]+?\[\/caption\]/g, function( a ) {
     101                                return a.replace( /<br([^>]*)>/g, '<wp-temp-br$1>' ).replace( /[\r\n\t]+/, '' );
    86102                        });
    87103                }
    88104
    89105                // Pretty it up for the source editor
    90106                blocklist1 = 'blockquote|ul|ol|li|table|thead|tbody|tfoot|tr|th|td|div|h[1-6]|p|fieldset';
    91                 content = content.replace(new RegExp('\\s*</('+blocklist1+')>\\s*', 'g'), '</$1>\n');
    92                 content = content.replace(new RegExp('\\s*<((?:'+blocklist1+')(?: [^>]*)?)>', 'g'), '\n<$1>');
     107                content = content.replace( new RegExp( '\\s*</(' + blocklist1 + ')>\\s*', 'g' ), '</$1>\n' );
     108                content = content.replace( new RegExp( '\\s*<((?:' + blocklist1 + ')(?: [^>]*)?)>', 'g' ), '\n<$1>' );
    93109
    94110                // Mark </p> if it has any attributes.
    95                 content = content.replace(/(<p [^>]+>.*?)<\/p>/g, '$1</p#>');
     111                content = content.replace( /(<p [^>]+>.*?)<\/p>/g, '$1</p#>' );
    96112
    97113                // Separate <div> containing <p>
    98                 content = content.replace(/<div( [^>]*)?>\s*<p>/gi, '<div$1>\n\n');
     114                content = content.replace( /<div( [^>]*)?>\s*<p>/gi, '<div$1>\n\n' );
    99115
    100116                // Remove <p> and <br />
    101                 content = content.replace(/\s*<p>/gi, '');
    102                 content = content.replace(/\s*<\/p>\s*/gi, '\n\n');
    103                 content = content.replace(/\n[\s\u00a0]+\n/g, '\n\n');
    104                 content = content.replace(/\s*<br ?\/?>\s*/gi, '\n');
     117                content = content.replace( /\s*<p>/gi, '' );
     118                content = content.replace( /\s*<\/p>\s*/gi, '\n\n' );
     119                content = content.replace( /\n[\s\u00a0]+\n/g, '\n\n' );
     120                content = content.replace( /\s*<br ?\/?>\s*/gi, '\n' );
    105121
    106122                // Fix some block element newline issues
    107                 content = content.replace(/\s*<div/g, '\n<div');
    108                 content = content.replace(/<\/div>\s*/g, '</div>\n');
    109                 content = content.replace(/\s*\[caption([^\[]+)\[\/caption\]\s*/gi, '\n\n[caption$1[/caption]\n\n');
    110                 content = content.replace(/caption\]\n\n+\[caption/g, 'caption]\n\n[caption');
     123                content = content.replace( /\s*<div/g, '\n<div' );
     124                content = content.replace( /<\/div>\s*/g, '</div>\n' );
     125                content = content.replace( /\s*\[caption([^\[]+)\[\/caption\]\s*/gi, '\n\n[caption$1[/caption]\n\n' );
     126                content = content.replace( /caption\]\n\n+\[caption/g, 'caption]\n\n[caption' );
    111127
    112128                blocklist2 = 'blockquote|ul|ol|li|table|thead|tbody|tfoot|tr|th|td|h[1-6]|pre|fieldset';
    113                 content = content.replace(new RegExp('\\s*<((?:'+blocklist2+')(?: [^>]*)?)\\s*>', 'g'), '\n<$1>');
    114                 content = content.replace(new RegExp('\\s*</('+blocklist2+')>\\s*', 'g'), '</$1>\n');
    115                 content = content.replace(/<li([^>]*)>/g, '\t<li$1>');
     129                content = content.replace( new RegExp('\\s*<((?:' + blocklist2 + ')(?: [^>]*)?)\\s*>', 'g' ), '\n<$1>' );
     130                content = content.replace( new RegExp('\\s*</(' + blocklist2 + ')>\\s*', 'g' ), '</$1>\n' );
     131                content = content.replace( /<li([^>]*)>/g, '\t<li$1>' );
    116132
    117                 if ( content.indexOf('<hr') != -1 ) {
    118                         content = content.replace(/\s*<hr( [^>]*)?>\s*/g, '\n\n<hr$1>\n\n');
     133                if ( content.indexOf( '<hr' ) !== -1 ) {
     134                        content = content.replace( /\s*<hr( [^>]*)?>\s*/g, '\n\n<hr$1>\n\n' );
    119135                }
    120136
    121                 if ( content.indexOf('<object') != -1 ) {
    122                         content = content.replace(/<object[\s\S]+?<\/object>/g, function(a){
    123                                 return a.replace(/[\r\n]+/g, '');
     137                if ( content.indexOf( '<object' ) !== -1 ) {
     138                        content = content.replace( /<object[\s\S]+?<\/object>/g, function( a ) {
     139                                return a.replace( /[\r\n]+/g, '' );
    124140                        });
    125141                }
    126142
    127143                // Unmark special paragraph closing tags
    128                 content = content.replace(/<\/p#>/g, '</p>\n');
    129                 content = content.replace(/\s*(<p [^>]+>[\s\S]*?<\/p>)/g, '\n$1');
     144                content = content.replace( /<\/p#>/g, '</p>\n' );
     145                content = content.replace( /\s*(<p [^>]+>[\s\S]*?<\/p>)/g, '\n$1' );
    130146
    131147                // Trim whitespace
    132                 content = content.replace(/^\s+/, '');
    133                 content = content.replace(/[\s\u00a0]+$/, '');
     148                content = content.replace( /^\s+/, '' );
     149                content = content.replace( /[\s\u00a0]+$/, '' );
    134150
    135151                // put back the line breaks in pre|script
    136                 if ( preserve_linebreaks )
    137                         content = content.replace(/<wp-temp-lb>/g, '\n');
     152                if ( preserve_linebreaks ) {
     153                        content = content.replace( /<wp-temp-lb>/g, '\n' );
     154                }
    138155
    139156                // and the <br> tags in captions
    140                 if ( preserve_br )
    141                         content = content.replace(/<wp-temp-br([^>]*)>/g, '<br$1>');
     157                if ( preserve_br ) {
     158                        content = content.replace( /<wp-temp-br([^>]*)>/g, '<br$1>' );
     159                }
    142160
    143161                return content;
    144162        },
    145163
    146         _wp_Autop : function(pee) {
     164        _wp_Autop: function(pee) {
    147165                var preserve_linebreaks = false, preserve_br = false,
    148166                        blocklist = 'table|thead|tfoot|caption|col|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|option|form|map|area|blockquote|address|math|style|p|h[1-6]|hr|fieldset|noscript|legend|section|article|aside|hgroup|header|footer|nav|figure|figcaption|details|menu|summary';
    149167
    150                 if ( pee.indexOf('<object') != -1 ) {
    151                         pee = pee.replace(/<object[\s\S]+?<\/object>/g, function(a){
    152                                 return a.replace(/[\r\n]+/g, '');
     168                if ( pee.indexOf( '<object' ) !== -1 ) {
     169                        pee = pee.replace( /<object[\s\S]+?<\/object>/g, function( a ) {
     170                                return a.replace( /[\r\n]+/g, '' );
    153171                        });
    154172                }
    155173
    156                 pee = pee.replace(/<[^<>]+>/g, function(a){
    157                         return a.replace(/[\r\n]+/g, ' ');
     174                pee = pee.replace( /<[^<>]+>/g, function( a ){
     175                        return a.replace( /[\r\n]+/g, ' ' );
    158176                });
    159177
    160178                // Protect pre|script tags
    161                 if ( pee.indexOf('<pre') != -1 || pee.indexOf('<script') != -1 ) {
     179                if ( pee.indexOf( '<pre' ) !== -1 || pee.indexOf( '<script' ) !== -1 ) {
    162180                        preserve_linebreaks = true;
    163                         pee = pee.replace(/<(pre|script)[^>]*>[\s\S]+?<\/\1>/g, function(a) {
    164                                 return a.replace(/(\r\n|\n)/g, '<wp-temp-lb>');
     181                        pee = pee.replace( /<(pre|script)[^>]*>[\s\S]+?<\/\1>/g, function( a ) {
     182                                return a.replace( /(\r\n|\n)/g, '<wp-temp-lb>' );
    165183                        });
    166184                }
    167185
    168186                // keep <br> tags inside captions and convert line breaks
    169                 if ( pee.indexOf('[caption') != -1 ) {
     187                if ( pee.indexOf( '[caption' ) !== -1 ) {
    170188                        preserve_br = true;
    171                         pee = pee.replace(/\[caption[\s\S]+?\[\/caption\]/g, function(a) {
     189                        pee = pee.replace( /\[caption[\s\S]+?\[\/caption\]/g, function( a ) {
    172190                                // keep existing <br>
    173                                 a = a.replace(/<br([^>]*)>/g, '<wp-temp-br$1>');
     191                                a = a.replace( /<br([^>]*)>/g, '<wp-temp-br$1>' );
    174192                                // no line breaks inside HTML tags
    175                                 a = a.replace(/<[a-zA-Z0-9]+( [^<>]+)?>/g, function(b){
    176                                         return b.replace(/[\r\n\t]+/, ' ');
     193                                a = a.replace( /<[a-zA-Z0-9]+( [^<>]+)?>/g, function( b ){
     194                                        return b.replace( /[\r\n\t]+/, ' ' );
    177195                                });
    178196                                // convert remaining line breaks to <br>
    179                                 return a.replace(/\s*\n\s*/g, '<wp-temp-br />');
     197                                return a.replace( /\s*\n\s*/g, '<wp-temp-br />' );
    180198                        });
    181199                }
    182200
    183201                pee = pee + '\n\n';
    184                 pee = pee.replace(/<br \/>\s*<br \/>/gi, '\n\n');
    185                 pee = pee.replace(new RegExp('(<(?:'+blocklist+')(?: [^>]*)?>)', 'gi'), '\n$1');
    186                 pee = pee.replace(new RegExp('(</(?:'+blocklist+')>)', 'gi'), '$1\n\n');
    187                 pee = pee.replace(/<hr( [^>]*)?>/gi, '<hr$1>\n\n'); // hr is self closing block element
    188                 pee = pee.replace(/\r\n|\r/g, '\n');
    189                 pee = pee.replace(/\n\s*\n+/g, '\n\n');
    190                 pee = pee.replace(/([\s\S]+?)\n\n/g, '<p>$1</p>\n');
    191                 pee = pee.replace(/<p>\s*?<\/p>/gi, '');
    192                 pee = pee.replace(new RegExp('<p>\\s*(</?(?:'+blocklist+')(?: [^>]*)?>)\\s*</p>', 'gi'), "$1");
    193                 pee = pee.replace(/<p>(<li.+?)<\/p>/gi, '$1');
    194                 pee = pee.replace(/<p>\s*<blockquote([^>]*)>/gi, '<blockquote$1><p>');
    195                 pee = pee.replace(/<\/blockquote>\s*<\/p>/gi, '</p></blockquote>');
    196                 pee = pee.replace(new RegExp('<p>\\s*(</?(?:'+blocklist+')(?: [^>]*)?>)', 'gi'), "$1");
    197                 pee = pee.replace(new RegExp('(</?(?:'+blocklist+')(?: [^>]*)?>)\\s*</p>', 'gi'), "$1");
    198                 pee = pee.replace(/\s*\n/gi, '<br />\n');
    199                 pee = pee.replace(new RegExp('(</?(?:'+blocklist+')[^>]*>)\\s*<br />', 'gi'), "$1");
    200                 pee = pee.replace(/<br \/>(\s*<\/?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)>)/gi, '$1');
    201                 pee = pee.replace(/(?:<p>|<br ?\/?>)*\s*\[caption([^\[]+)\[\/caption\]\s*(?:<\/p>|<br ?\/?>)*/gi, '[caption$1[/caption]');
     202                pee = pee.replace( /<br \/>\s*<br \/>/gi, '\n\n' );
     203                pee = pee.replace( new RegExp( '(<(?:' + blocklist + ')(?: [^>]*)?>)', 'gi' ), '\n$1' );
     204                pee = pee.replace( new RegExp( '(</(?:' + blocklist + ')>)', 'gi' ), '$1\n\n' );
     205                pee = pee.replace( /<hr( [^>]*)?>/gi, '<hr$1>\n\n' ); // hr is self closing block element
     206                pee = pee.replace( /\r\n|\r/g, '\n' );
     207                pee = pee.replace( /\n\s*\n+/g, '\n\n' );
     208                pee = pee.replace( /([\s\S]+?)\n\n/g, '<p>$1</p>\n' );
     209                pee = pee.replace( /<p>\s*?<\/p>/gi, '');
     210                pee = pee.replace( new RegExp( '<p>\\s*(</?(?:' + blocklist + ')(?: [^>]*)?>)\\s*</p>', 'gi' ), '$1' );
     211                pee = pee.replace( /<p>(<li.+?)<\/p>/gi, '$1');
     212                pee = pee.replace( /<p>\s*<blockquote([^>]*)>/gi, '<blockquote$1><p>');
     213                pee = pee.replace( /<\/blockquote>\s*<\/p>/gi, '</p></blockquote>');
     214                pee = pee.replace( new RegExp( '<p>\\s*(</?(?:' + blocklist + ')(?: [^>]*)?>)', 'gi' ), '$1' );
     215                pee = pee.replace( new RegExp( '(</?(?:' + blocklist + ')(?: [^>]*)?>)\\s*</p>', 'gi' ), '$1' );
     216                pee = pee.replace( /\s*\n/gi, '<br />\n');
     217                pee = pee.replace( new RegExp( '(</?(?:' + blocklist + ')[^>]*>)\\s*<br />', 'gi' ), '$1' );
     218                pee = pee.replace( /<br \/>(\s*<\/?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)>)/gi, '$1' );
     219                pee = pee.replace( /(?:<p>|<br ?\/?>)*\s*\[caption([^\[]+)\[\/caption\]\s*(?:<\/p>|<br ?\/?>)*/gi, '[caption$1[/caption]' );
    202220
    203                 pee = pee.replace(/(<(?:div|th|td|form|fieldset|dd)[^>]*>)(.*?)<\/p>/g, function(a, b, c) {
    204                         if ( c.match(/<p( [^>]*)?>/) )
     221                pee = pee.replace( /(<(?:div|th|td|form|fieldset|dd)[^>]*>)(.*?)<\/p>/g, function( a, b, c ) {
     222                        if ( c.match( /<p( [^>]*)?>/ ) ) {
    205223                                return a;
     224                        }
    206225
    207226                        return b + '<p>' + c + '</p>';
    208227                });
    209228
    210229                // put back the line breaks in pre|script
    211                 if ( preserve_linebreaks )
    212                         pee = pee.replace(/<wp-temp-lb>/g, '\n');
     230                if ( preserve_linebreaks ) {
     231                        pee = pee.replace( /<wp-temp-lb>/g, '\n' );
     232                }
    213233
    214                 if ( preserve_br )
    215                         pee = pee.replace(/<wp-temp-br([^>]*)>/g, '<br$1>');
     234                if ( preserve_br ) {
     235                        pee = pee.replace( /<wp-temp-br([^>]*)>/g, '<br$1>' );
     236                }
    216237
    217238                return pee;
    218239        },
    219240
    220         pre_wpautop : function(content) {
     241        pre_wpautop: function( content ) {
    221242                var t = this, o = { o: t, data: content, unfiltered: content },
    222                         q = typeof(jQuery) != 'undefined';
     243                        q = typeof( jQuery ) !== 'undefined';
    223244
    224                 if ( q )
    225                         jQuery('body').trigger('beforePreWpautop', [o]);
    226                 o.data = t._wp_Nop(o.data);
    227                 if ( q )
    228                         jQuery('body').trigger('afterPreWpautop', [o]);
     245                if ( q ) {
     246                        jQuery( 'body' ).trigger( 'beforePreWpautop', [ o ] );
     247                }
    229248
     249                o.data = t._wp_Nop( o.data );
     250
     251                if ( q ) {
     252                        jQuery('body').trigger('afterPreWpautop', [ o ] );
     253                }
     254
    230255                return o.data;
    231256        },
    232257
    233         wpautop : function(pee) {
     258        wpautop: function( pee ) {
    234259                var t = this, o = { o: t, data: pee, unfiltered: pee },
    235                         q = typeof(jQuery) != 'undefined';
     260                        q = typeof( jQuery ) !== 'undefined';
    236261
    237                 if ( q )
    238                         jQuery('body').trigger('beforeWpautop', [o]);
    239                 o.data = t._wp_Autop(o.data);
    240                 if ( q )
    241                         jQuery('body').trigger('afterWpautop', [o]);
     262                if ( q ) {
     263                        jQuery( 'body' ).trigger('beforeWpautop', [ o ] );
     264                }
    242265
     266                o.data = t._wp_Autop( o.data );
     267
     268                if ( q ) {
     269                        jQuery( 'body' ).trigger('afterWpautop', [ o ] );
     270                }
     271
    243272                return o.data;
    244273        }
    245 }
     274};