Make WordPress Core

Changeset 26224


Ignore:
Timestamp:
11/15/2013 09:02:56 PM (13 years ago)
Author:
azaozz
Message:

JSHint fixes for editor.js. Props dougwollison, fixes #25947

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/js/editor.js

    r25094 r26224  
    1 
    2 var switchEditors = {
    3 
    4     switchto: function(el) {
    5         var aid = el.id, l = aid.length, id = aid.substr(0, l - 5), mode = aid.substr(l - 4);
    6 
    7         this.go(id, mode);
    8     },
    9 
    10     go: function(id, mode) { // mode can be 'html', 'tmce', or 'toggle'; 'html' is used for the "Text" editor tab.
     1/* global tinymce, tinyMCEPreInit, QTags, setUserSetting */
     2
     3window.switchEditors = {
     4
     5    switchto: function( el ) {
     6        var aid = el.id,
     7            l = aid.length,
     8            id = aid.substr( 0, l - 5 ),
     9            mode = aid.substr( l - 4 );
     10
     11        this.go( id, mode );
     12    },
     13
     14    // mode can be 'html', 'tmce', or 'toggle'; 'html' is used for the 'Text' editor tab.
     15    go: function( id, mode ) {
     16        var t = this, ed, wrap_id, txtarea_el,
     17            dom = tinymce.DOM;
     18
    1119        id = id || 'content';
    1220        mode = mode || 'toggle';
    1321
    14         var t = this, ed = tinyMCE.get(id), wrap_id, txtarea_el, dom = tinymce.DOM;
    15 
    16         wrap_id = 'wp-'+id+'-wrap';
    17         txtarea_el = dom.get(id);
    18 
    19         if ( 'toggle' == mode ) {
    20             if ( ed && !ed.isHidden() )
     22        ed = tinymce.get( id );
     23        wrap_id = 'wp-' + id + '-wrap';
     24        txtarea_el = dom.get( id );
     25
     26        if ( 'toggle' === mode ) {
     27            if ( ed && ! ed.isHidden() ) {
    2128                mode = 'html';
    22             else
     29            } else {
    2330                mode = 'tmce';
    24         }
    25 
    26         if ( 'tmce' == mode || 'tinymce' == mode ) {
    27             if ( ed && ! ed.isHidden() )
     31            }
     32        }
     33
     34        if ( 'tmce' === mode || 'tinymce' === mode ) {
     35            if ( ed && ! ed.isHidden() ) {
    2836                return false;
    29 
    30             if ( typeof(QTags) != 'undefined' )
    31                 QTags.closeAllTags(id);
    32 
    33             if ( tinyMCEPreInit.mceInit[id] && tinyMCEPreInit.mceInit[id].wpautop )
     37            }
     38
     39            if ( typeof( QTags ) !== 'undefined' ) {
     40                QTags.closeAllTags( id );
     41            }
     42
     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');
    46 
    47         } else if ( 'html' == mode ) {
    48 
    49             if ( ed && ed.isHidden() )
     54            dom.removeClass( wrap_id, 'html-active' );
     55            dom.addClass( wrap_id, 'tmce-active' );
     56            setUserSetting( 'editor', 'tinymce' );
     57
     58        } else if ( 'html' === mode ) {
     59
     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 );
    58 
    59                 dom.setStyles(txtarea_el, {'display': '', 'visibility': ''});
    60             }
    61 
    62             dom.removeClass(wrap_id, 'tmce-active');
    63             dom.addClass(wrap_id, 'html-active');
    64             setUserSetting('editor', 'html');
     70                }
     71
     72                dom.setStyles( txtarea_el, {'display': '', 'visibility': ''} );
     73            }
     74
     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, blocklist2,
     84            preserve_linebreaks = false,
     85            preserve_br = false;
    7186
    7287        // Protect pre|script tags
    73         if ( content.indexOf('<pre') != -1 || content.indexOf('<script') != -1 ) {
     88        if ( content.indexOf( '<pre' ) !== -1 || content.indexOf( '<script' ) !== -1 ) {
    7489            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>');
     90            content = content.replace( /<(pre|script)[^>]*>[\s\S]+?<\/\1>/g, function( a ) {
     91                a = a.replace( /<br ?\/?>(\r\n|\n)?/g, '<wp-temp-lb>' );
     92                return a.replace( /<\/?p( [^>]*)?>(\r\n|\n)?/g, '<wp-temp-lb>' );
    7893            });
    7994        }
    8095
    8196        // keep <br> tags inside captions and remove line breaks
    82         if ( content.indexOf('[caption') != -1 ) {
     97        if ( content.indexOf( '[caption' ) !== -1 ) {
    8398            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]+/, '');
     99            content = content.replace( /\[caption[\s\S]+?\[\/caption\]/g, function( a ) {
     100                return a.replace( /<br([^>]*)>/g, '<wp-temp-br$1>' ).replace( /[\r\n\t]+/, '' );
    86101            });
    87102        }
     
    89104        // Pretty it up for the source editor
    90105        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>');
     106        content = content.replace( new RegExp( '\\s*</(' + blocklist1 + ')>\\s*', 'g' ), '</$1>\n' );
     107        content = content.replace( new RegExp( '\\s*<((?:' + blocklist1 + ')(?: [^>]*)?)>', 'g' ), '\n<$1>' );
    93108
    94109        // Mark </p> if it has any attributes.
    95         content = content.replace(/(<p [^>]+>.*?)<\/p>/g, '$1</p#>');
     110        content = content.replace( /(<p [^>]+>.*?)<\/p>/g, '$1</p#>' );
    96111
    97112        // Separate <div> containing <p>
    98         content = content.replace(/<div( [^>]*)?>\s*<p>/gi, '<div$1>\n\n');
     113        content = content.replace( /<div( [^>]*)?>\s*<p>/gi, '<div$1>\n\n' );
    99114
    100115        // 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');
     116        content = content.replace( /\s*<p>/gi, '' );
     117        content = content.replace( /\s*<\/p>\s*/gi, '\n\n' );
     118        content = content.replace( /\n[\s\u00a0]+\n/g, '\n\n' );
     119        content = content.replace( /\s*<br ?\/?>\s*/gi, '\n' );
    105120
    106121        // 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');
     122        content = content.replace( /\s*<div/g, '\n<div' );
     123        content = content.replace( /<\/div>\s*/g, '</div>\n' );
     124        content = content.replace( /\s*\[caption([^\[]+)\[\/caption\]\s*/gi, '\n\n[caption$1[/caption]\n\n' );
     125        content = content.replace( /caption\]\n\n+\[caption/g, 'caption]\n\n[caption' );
    111126
    112127        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>');
    116 
    117         if ( content.indexOf('<hr') != -1 ) {
    118             content = content.replace(/\s*<hr( [^>]*)?>\s*/g, '\n\n<hr$1>\n\n');
    119         }
    120 
    121         if ( content.indexOf('<object') != -1 ) {
    122             content = content.replace(/<object[\s\S]+?<\/object>/g, function(a){
    123                 return a.replace(/[\r\n]+/g, '');
     128        content = content.replace( new RegExp('\\s*<((?:' + blocklist2 + ')(?: [^>]*)?)\\s*>', 'g' ), '\n<$1>' );
     129        content = content.replace( new RegExp('\\s*</(' + blocklist2 + ')>\\s*', 'g' ), '</$1>\n' );
     130        content = content.replace( /<li([^>]*)>/g, '\t<li$1>' );
     131
     132        if ( content.indexOf( '<hr' ) !== -1 ) {
     133            content = content.replace( /\s*<hr( [^>]*)?>\s*/g, '\n\n<hr$1>\n\n' );
     134        }
     135
     136        if ( content.indexOf( '<object' ) !== -1 ) {
     137            content = content.replace( /<object[\s\S]+?<\/object>/g, function( a ) {
     138                return a.replace( /[\r\n]+/g, '' );
    124139            });
    125140        }
    126141
    127142        // 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');
     143        content = content.replace( /<\/p#>/g, '</p>\n' );
     144        content = content.replace( /\s*(<p [^>]+>[\s\S]*?<\/p>)/g, '\n$1' );
    130145
    131146        // Trim whitespace
    132         content = content.replace(/^\s+/, '');
    133         content = content.replace(/[\s\u00a0]+$/, '');
     147        content = content.replace( /^\s+/, '' );
     148        content = content.replace( /[\s\u00a0]+$/, '' );
    134149
    135150        // put back the line breaks in pre|script
    136         if ( preserve_linebreaks )
    137             content = content.replace(/<wp-temp-lb>/g, '\n');
     151        if ( preserve_linebreaks ) {
     152            content = content.replace( /<wp-temp-lb>/g, '\n' );
     153        }
    138154
    139155        // and the <br> tags in captions
    140         if ( preserve_br )
    141             content = content.replace(/<wp-temp-br([^>]*)>/g, '<br$1>');
     156        if ( preserve_br ) {
     157            content = content.replace( /<wp-temp-br([^>]*)>/g, '<br$1>' );
     158        }
    142159
    143160        return content;
    144161    },
    145162
    146     _wp_Autop : function(pee) {
    147         var preserve_linebreaks = false, preserve_br = false,
    148             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';
    149 
    150         if ( pee.indexOf('<object') != -1 ) {
    151             pee = pee.replace(/<object[\s\S]+?<\/object>/g, function(a){
    152                 return a.replace(/[\r\n]+/g, '');
    153             });
    154         }
    155 
    156         pee = pee.replace(/<[^<>]+>/g, function(a){
    157             return a.replace(/[\r\n]+/g, ' ');
     163    _wp_Autop: function(pee) {
     164        var preserve_linebreaks = false,
     165            preserve_br = false,
     166            blocklist = 'table|thead|tfoot|caption|col|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select' +
     167                '|option|form|map|area|blockquote|address|math|style|p|h[1-6]|hr|fieldset|noscript|legend|section' +
     168                '|article|aside|hgroup|header|footer|nav|figure|figcaption|details|menu|summary';
     169
     170        if ( pee.indexOf( '<object' ) !== -1 ) {
     171            pee = pee.replace( /<object[\s\S]+?<\/object>/g, function( a ) {
     172                return a.replace( /[\r\n]+/g, '' );
     173            });
     174        }
     175
     176        pee = pee.replace( /<[^<>]+>/g, function( a ){
     177            return a.replace( /[\r\n]+/g, ' ' );
    158178        });
    159179
    160180        // Protect pre|script tags
    161         if ( pee.indexOf('<pre') != -1 || pee.indexOf('<script') != -1 ) {
     181        if ( pee.indexOf( '<pre' ) !== -1 || pee.indexOf( '<script' ) !== -1 ) {
    162182            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>');
     183            pee = pee.replace( /<(pre|script)[^>]*>[\s\S]+?<\/\1>/g, function( a ) {
     184                return a.replace( /(\r\n|\n)/g, '<wp-temp-lb>' );
    165185            });
    166186        }
    167187
    168188        // keep <br> tags inside captions and convert line breaks
    169         if ( pee.indexOf('[caption') != -1 ) {
     189        if ( pee.indexOf( '[caption' ) !== -1 ) {
    170190            preserve_br = true;
    171             pee = pee.replace(/\[caption[\s\S]+?\[\/caption\]/g, function(a) {
     191            pee = pee.replace( /\[caption[\s\S]+?\[\/caption\]/g, function( a ) {
    172192                // keep existing <br>
    173                 a = a.replace(/<br([^>]*)>/g, '<wp-temp-br$1>');
     193                a = a.replace( /<br([^>]*)>/g, '<wp-temp-br$1>' );
    174194                // no line breaks inside HTML tags
    175                 a = a.replace(/<[a-zA-Z0-9]+( [^<>]+)?>/g, function(b){
    176                     return b.replace(/[\r\n\t]+/, ' ');
     195                a = a.replace( /<[a-zA-Z0-9]+( [^<>]+)?>/g, function( b ) {
     196                    return b.replace( /[\r\n\t]+/, ' ' );
    177197                });
    178198                // convert remaining line breaks to <br>
    179                 return a.replace(/\s*\n\s*/g, '<wp-temp-br />');
     199                return a.replace( /\s*\n\s*/g, '<wp-temp-br />' );
    180200            });
    181201        }
    182202
    183203        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 
    203         pee = pee.replace(/(<(?:div|th|td|form|fieldset|dd)[^>]*>)(.*?)<\/p>/g, function(a, b, c) {
    204             if ( c.match(/<p( [^>]*)?>/) )
     204        pee = pee.replace( /<br \/>\s*<br \/>/gi, '\n\n' );
     205        pee = pee.replace( new RegExp( '(<(?:' + blocklist + ')(?: [^>]*)?>)', 'gi' ), '\n$1' );
     206        pee = pee.replace( new RegExp( '(</(?:' + blocklist + ')>)', 'gi' ), '$1\n\n' );
     207        pee = pee.replace( /<hr( [^>]*)?>/gi, '<hr$1>\n\n' ); // hr is self closing block element
     208        pee = pee.replace( /\r\n|\r/g, '\n' );
     209        pee = pee.replace( /\n\s*\n+/g, '\n\n' );
     210        pee = pee.replace( /([\s\S]+?)\n\n/g, '<p>$1</p>\n' );
     211        pee = pee.replace( /<p>\s*?<\/p>/gi, '');
     212        pee = pee.replace( new RegExp( '<p>\\s*(</?(?:' + blocklist + ')(?: [^>]*)?>)\\s*</p>', 'gi' ), '$1' );
     213        pee = pee.replace( /<p>(<li.+?)<\/p>/gi, '$1');
     214        pee = pee.replace( /<p>\s*<blockquote([^>]*)>/gi, '<blockquote$1><p>');
     215        pee = pee.replace( /<\/blockquote>\s*<\/p>/gi, '</p></blockquote>');
     216        pee = pee.replace( new RegExp( '<p>\\s*(</?(?:' + blocklist + ')(?: [^>]*)?>)', 'gi' ), '$1' );
     217        pee = pee.replace( new RegExp( '(</?(?:' + blocklist + ')(?: [^>]*)?>)\\s*</p>', 'gi' ), '$1' );
     218        pee = pee.replace( /\s*\n/gi, '<br />\n');
     219        pee = pee.replace( new RegExp( '(</?(?:' + blocklist + ')[^>]*>)\\s*<br />', 'gi' ), '$1' );
     220        pee = pee.replace( /<br \/>(\s*<\/?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)>)/gi, '$1' );
     221        pee = pee.replace( /(?:<p>|<br ?\/?>)*\s*\[caption([^\[]+)\[\/caption\]\s*(?:<\/p>|<br ?\/?>)*/gi, '[caption$1[/caption]' );
     222
     223        pee = pee.replace( /(<(?:div|th|td|form|fieldset|dd)[^>]*>)(.*?)<\/p>/g, function( a, b, c ) {
     224            if ( c.match( /<p( [^>]*)?>/ ) ) {
    205225                return a;
     226            }
    206227
    207228            return b + '<p>' + c + '</p>';
     
    209230
    210231        // put back the line breaks in pre|script
    211         if ( preserve_linebreaks )
    212             pee = pee.replace(/<wp-temp-lb>/g, '\n');
    213 
    214         if ( preserve_br )
    215             pee = pee.replace(/<wp-temp-br([^>]*)>/g, '<br$1>');
     232        if ( preserve_linebreaks ) {
     233            pee = pee.replace( /<wp-temp-lb>/g, '\n' );
     234        }
     235
     236        if ( preserve_br ) {
     237            pee = pee.replace( /<wp-temp-br([^>]*)>/g, '<br$1>' );
     238        }
    216239
    217240        return pee;
    218241    },
    219242
    220     pre_wpautop : function(content) {
     243    pre_wpautop: function( content ) {
    221244        var t = this, o = { o: t, data: content, unfiltered: content },
    222             q = typeof(jQuery) != 'undefined';
    223 
    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            q = typeof( jQuery ) !== 'undefined';
     246
     247        if ( q ) {
     248            jQuery( 'body' ).trigger( 'beforePreWpautop', [ o ] );
     249        }
     250
     251        o.data = t._wp_Nop( o.data );
     252
     253        if ( q ) {
     254            jQuery('body').trigger('afterPreWpautop', [ o ] );
     255        }
    229256
    230257        return o.data;
    231258    },
    232259
    233     wpautop : function(pee) {
     260    wpautop: function( pee ) {
    234261        var t = this, o = { o: t, data: pee, unfiltered: pee },
    235             q = typeof(jQuery) != 'undefined';
    236 
    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            q = typeof( jQuery ) !== 'undefined';
     263
     264        if ( q ) {
     265            jQuery( 'body' ).trigger('beforeWpautop', [ o ] );
     266        }
     267
     268        o.data = t._wp_Autop( o.data );
     269
     270        if ( q ) {
     271            jQuery( 'body' ).trigger('afterWpautop', [ o ] );
     272        }
    242273
    243274        return o.data;
    244275    }
    245 }
     276};
Note: See TracChangeset for help on using the changeset viewer.