Make WordPress Core

Changeset 22023


Ignore:
Timestamp:
09/26/2012 10:20:15 PM (14 years ago)
Author:
koopersmith
Message:

Remove internal TinyMCE attributes when generating the HTML for attachment views.

Moves the HTML utility functions to the top of mce-views.js. Also adds wp.mce.view.removeInternalAttrs( attrs ) and wp.mce.view.attrs( content ).

see #21390, #21812, #21813.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/js/mce-view.js

    r22012 r22023  
    33        var wp = {};
    44
     5// HTML utility functions
     6// ----------------------
     7(function(){
     8        wp.html = _.extend( wp.html || {}, {
     9                // ### Parse HTML attributes.
     10                //
     11                // Converts `content` to a set of parsed HTML attributes.
     12                // Utilizes `wp.shortcode.attrs( content )`, which is a valid superset of
     13                // the HTML attribute specification. Reformats the attributes into an
     14                // object that contains the `attrs` with `key:value` mapping, and a record
     15                // of the attributes that were entered using `empty` attribute syntax (i.e.
     16                // with no value).
     17                attrs: function( content ) {
     18                        var result, attrs;
     19
     20                        // If `content` ends in a slash, strip it.
     21                        if ( '/' === content[ content.length - 1 ] )
     22                                content = content.slice( 0, -1 );
     23
     24                        result = wp.shortcode.attrs( content );
     25                        attrs  = result.named;
     26
     27                        _.each( result.numeric, function( key ) {
     28                                if ( /\s/.test( key ) )
     29                                        return;
     30
     31                                attrs[ key ] = '';
     32                        });
     33
     34                        return attrs;
     35                },
     36
     37                // ### Convert an HTML-representation of an object to a string.
     38                string: function( options ) {
     39                        var text = '<' + options.tag,
     40                                content = options.content || '';
     41
     42                        _.each( options.attrs, function( value, attr ) {
     43                                text += ' ' + attr;
     44
     45                                // Use empty attribute notation where possible.
     46                                if ( '' === value )
     47                                        return;
     48
     49                                // Convert boolean values to strings.
     50                                if ( _.isBoolean( value ) )
     51                                        value = value ? 'true' : 'false';
     52
     53                                text += '="' + value + '"';
     54                        });
     55
     56                        // Return the result if it is a self-closing tag.
     57                        if ( options.single )
     58                                return text + ' />';
     59
     60                        // Complete the opening tag.
     61                        text += '>';
     62
     63                        // If `content` is an object, recursively call this function.
     64                        text += _.isObject( content ) ? wp.html.string( content ) : content;
     65
     66                        return text + '</' + options.tag + '>';
     67                }
     68        });
     69}());
     70
    571(function($){
    672        var views = {},
     
    1278        // wp.mce.view
    1379        // -----------
    14         //
    1580        // A set of utilities that simplifies adding custom UI within a TinyMCE editor.
    1681        // At its core, it serves as a series of converters, transforming text to a
     
    271336                },
    272337
     338                // ### Remove internal TinyMCE attributes.
     339                removeInternalAttrs: function( attrs ) {
     340                        var result = {};
     341                        _.each( attrs, function( value, attr ) {
     342                                if ( -1 === attr.indexOf('data-mce') )
     343                                        result[ attr ] = value;
     344                        });
     345                        return result;
     346                },
     347
     348                // ### Parse an attribute string and removes internal TinyMCE attributes.
     349                attrs: function( content ) {
     350                        return wp.mce.view.removeInternalAttrs( wp.html.attrs( content ) );
     351                },
     352
    273353                // Link any localized strings.
    274354                l10n: _.isUndefined( _wpMceViewL10n ) ? {} : _wpMceViewL10n
     
    281361(function($){
    282362        var mceview = wp.mce.view,
    283                 attrs;
    284 
    285         wp.html = _.extend( wp.html || {}, {
    286                 // ### Parse HTML attributes.
    287                 //
    288                 // Converts `content` to a set of parsed HTML attributes.
    289                 // Utilizes `wp.shortcode.attrs( content )`, which is a valid superset of
    290                 // the HTML attribute specification. Reformats the attributes into an
    291                 // object that contains the `attrs` with `key:value` mapping, and a record
    292                 // of the attributes that were entered using `empty` attribute syntax (i.e.
    293                 // with no value).
    294                 attrs: function( content ) {
    295                         var result, attrs;
    296 
    297                         // If `content` ends in a slash, strip it.
    298                         if ( '/' === content[ content.length - 1 ] )
    299                                 content = content.slice( 0, -1 );
    300 
    301                         result = wp.shortcode.attrs( content );
    302                         attrs  = result.named;
    303 
    304                         _.each( result.numeric, function( key ) {
    305                                 if ( /\s/.test( key ) )
    306                                         return;
    307 
    308                                 attrs[ key ] = '';
    309                         });
    310 
    311                         return attrs;
    312                 },
    313 
    314                 string: function( options ) {
    315                         var text = '<' + options.tag,
    316                                 content = options.content || '';
    317 
    318                         _.each( options.attrs, function( value, attr ) {
    319                                 text += ' ' + attr;
    320 
    321                                 // Use empty attribute notation where possible.
    322                                 if ( '' === value )
    323                                         return;
    324 
    325                                 // Convert boolean values to strings.
    326                                 if ( _.isBoolean( value ) )
    327                                         value = value ? 'true' : 'false';
    328 
    329                                 text += '="' + value + '"';
    330                         });
    331 
    332                         // Return the result if it is a self-closing tag.
    333                         if ( options.single )
    334                                 return text + ' />';
    335 
    336                         // Complete the opening tag.
    337                         text += '>';
    338 
    339                         // If `content` is an object, recursively call this function.
    340                         text += _.isObject( content ) ? wp.html.string( content ) : content;
    341 
    342                         return text + '</' + options.tag + '>';
    343                 }
    344         });
     363                mceFreeAttrs;
    345364
    346365        mceview.add( 'attachment', {
     
    399418
    400419                                if ( results[1] )
    401                                         this.anchor = wp.html.attrs( results[1] );
    402 
    403                                 this.img = wp.html.attrs( results[2] );
     420                                        this.anchor = mceview.attrs( results[1] );
     421
     422                                this.img  = mceview.attrs( results[2] );
    404423                                className = this.img['class'];
    405424
Note: See TracChangeset for help on using the changeset viewer.