[wp-trac] [WordPress Trac] #36263: When building a shortcode with `wp.shortcode.string()` the `attrs` property is finnicky.

WordPress Trac noreply at wordpress.org
Wed Mar 16 17:48:08 UTC 2016


#36263: When building a shortcode with `wp.shortcode.string()` the `attrs` property
is finnicky.
-----------------------------+--------------------------------------
 Reporter:  georgestephanis  |      Owner:
     Type:  defect (bug)     |     Status:  new
 Priority:  low              |  Milestone:  Awaiting Review
Component:  Shortcodes       |    Version:  trunk
 Severity:  minor            |   Keywords:  dev-feedback 2nd-opinion
  Focuses:  javascript       |
-----------------------------+--------------------------------------
 If you pass it in as:


 {{{
 wp.shortcode.string({
         tag   : 'short',
         type  : 'single',
         attrs : {
                 named   : { foo : 'bar' },
                 numeric : [ 'abc123' ]
         }
 });
 }}}

 it works fine -- resulting in `[short abc123 foo="bar"]`, but if you flip
 the order so that numeric comes before name, like so:

 {{{
 wp.shortcode.string({
         tag   : 'short',
         type  : 'single',
         attrs : {
                 numeric : [ 'abc123' ],
                 named   : { foo : 'bar' }
         }
 });
 }}}

 the output is `[short numeric="abc123" named="[object Object]"]`

 This is because we're doing an overly basic array comparison of the keys
 in which order matters --

 https://core.trac.wordpress.org/browser/trunk/src/wp-
 includes/js/shortcode.js?rev=34933#L218

 {{{
 // Identify a correctly formatted `attrs` object.
 } else if ( _.isEqual( _.keys( attrs ), [ 'named', 'numeric' ] ) ) {
         this.attrs = attrs;
 }}}

 I wonder if we could do something as simple as using `_.difference`
 instead --

 {{{
 } else if ( _.difference( _.keys( attrs ), [ 'named', 'numeric' ] ).length
 === 0 ) {
         this.attrs = _.defaults( attrs, { named : {}, numeric : [] } );
 }}}

 or something?

 Open to suggestions.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/36263>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list