Make WordPress Core

Ticket #25829: shortcode.diff

File shortcode.diff, 4.3 KB (added by seamusleahy, 13 years ago)

adds base function for both shortcodes and html

  • wp-includes/js/shortcode.js

     
    114114                // `name='value'`, or `name=value`. Numeric attributes can be formatted
    115115                // as `"value"` or just `value`.
    116116                attrs: _.memoize( function( text ) {
    117                         var named   = {},
    118                                 numeric = [],
    119                                 pattern, match;
    120 
    121                         // This regular expression is reused from `shortcode_parse_atts()`
    122                         // in `wp-includes/shortcodes.php`.
    123                         //
    124                         // Capture groups:
    125                         //
    126                         // 1. An attribute name, that corresponds to...
    127                         // 2. a value in double quotes.
    128                         // 3. An attribute name, that corresponds to...
    129                         // 4. a value in single quotes.
    130                         // 5. An attribute name, that corresponds to...
    131                         // 6. an unquoted value.
    132                         // 7. A numeric attribute in double quotes.
    133                         // 8. An unquoted numeric attribute.
    134                         pattern = /(\w+)\s*=\s*"([^"]*)"(?:\s|$)|(\w+)\s*=\s*\'([^\']*)\'(?:\s|$)|(\w+)\s*=\s*([^\s\'"]+)(?:\s|$)|"([^"]*)"(?:\s|$)|(\S+)(?:\s|$)/g;
    135 
    136                         // Map zero-width spaces to actual spaces.
    137                         text = text.replace( /[\u00a0\u200b]/g, ' ' );
    138 
    139                         // Match and normalize attributes.
    140                         while ( (match = pattern.exec( text )) ) {
    141                                 if ( match[1] ) {
    142                                         named[ match[1].toLowerCase() ] = match[2];
    143                                 } else if ( match[3] ) {
    144                                         named[ match[3].toLowerCase() ] = match[4];
    145                                 } else if ( match[5] ) {
    146                                         named[ match[5].toLowerCase() ] = match[6];
    147                                 } else if ( match[7] ) {
    148                                         numeric.push( match[7] );
    149                                 } else if ( match[8] ) {
    150                                         numeric.push( match[8] );
    151                                 }
    152                         }
    153 
    154                         return {
    155                                 named:   named,
    156                                 numeric: numeric
    157                         };
     117                        return wp.html._attrs( text, { dashesInNames: false } );
    158118                }),
    159119
    160120                // ### Generate a Shortcode Object from a RegExp match
     
    279239// Experimental. These functions may change or be removed in the future.
    280240(function(){
    281241        wp.html = _.extend( wp.html || {}, {
     242
     243                _attrs: function( text, options ) {
     244                        var named   = {},
     245                                numeric = [],
     246                                pattern, match, namePattern;
     247
     248                        options = options || {};
     249                        _.defaults( options, {
     250                                dashesInNames: true
     251                        } );
     252
     253                        // This regular expression is reused from `shortcode_parse_atts()`
     254                        // in `wp-includes/shortcodes.php`.
     255                        //
     256                        // Capture groups:
     257                        //
     258                        // 1. An attribute name, that corresponds to...
     259                        // 2. a value in double quotes.
     260                        // 3. An attribute name, that corresponds to...
     261                        // 4. a value in single quotes.
     262                        // 5. An attribute name, that corresponds to...
     263                        // 6. an unquoted value.
     264                        // 7. A numeric attribute in double quotes.
     265                        // 8. An unquoted numeric attribute.
     266                        namePattern = options.dashesInNames ? '[\\w\\-]' : '\\w';
     267                        pattern = new RegExp( '('+namePattern+'+)\\s*=\\s*"([^"]*)"(?:\\s|$)|(\\w+)\\s*=\\s*\'([^\']*)\'(?:\\s|$)|(\\w+)\\s*=\\s*([^\\s\'"]+)(?:\\s|$)|"([^"]*)"(?:\\s|$)|(\\S+)(?:\\s|$)', 'g' );
     268
     269                        // Map zero-width spaces to actual spaces.
     270                        text = text.replace( /[\u00a0\u200b]/g, ' ' );
     271
     272                        // Match and normalize attributes.
     273                        while ( (match = pattern.exec( text )) ) {
     274                                if ( match[1] ) {
     275                                        named[ match[1].toLowerCase() ] = match[2];
     276                                } else if ( match[3] ) {
     277                                        named[ match[3].toLowerCase() ] = match[4];
     278                                } else if ( match[5] ) {
     279                                        named[ match[5].toLowerCase() ] = match[6];
     280                                } else if ( match[7] ) {
     281                                        numeric.push( match[7] );
     282                                } else if ( match[8] ) {
     283                                        numeric.push( match[8] );
     284                                }
     285                        }
     286
     287                        return {
     288                                named:   named,
     289                                numeric: numeric
     290                        };
     291                },
     292
    282293                // ### Parse HTML attributes.
    283294                //
    284295                // Converts `content` to a set of parsed HTML attributes.
     
    287298                // object that contains the `attrs` with `key:value` mapping, and a record
    288299                // of the attributes that were entered using `empty` attribute syntax (i.e.
    289300                // with no value).
    290                 attrs: function( content ) {
     301                attrs: _.memoize( function( content ) {
    291302                        var result, attrs;
    292303
    293304                        // If `content` ends in a slash, strip it.
    294305                        if ( '/' === content[ content.length - 1 ] )
    295306                                content = content.slice( 0, -1 );
    296307
    297                         result = wp.shortcode.attrs( content );
     308                        result = wp.html._attrs( content );
    298309                        attrs  = result.named;
    299310
    300311                        _.each( result.numeric, function( key ) {
     
    305316                        });
    306317
    307318                        return attrs;
    308                 },
     319                } ),
    309320
    310321                // ### Convert an HTML-representation of an object to a string.
    311322                string: function( options ) {