From 7abc076e133ca39c3144ff9046e5e6c20b755462 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Sat, 21 Dec 2019 18:32:45 +0000 Subject: [PATCH] Shortcodes: Make sure `wp.shortcode.string()` accepts the `attrs` array keys in any order. Props yale01, georgestephanis, adamsilverstein, zsusag, mircoraffinetti, SergeyBiryukov. Fixes #36263. git-svn-id: https://develop.svn.wordpress.org/trunk@47003 602fd350-edb4-49c9-b593-d223f7449a82 --- src/js/_enqueues/wp/shortcode.js | 4 ++-- tests/qunit/wp-includes/js/shortcode.js | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/js/_enqueues/wp/shortcode.js b/src/js/_enqueues/wp/shortcode.js index 8f02273ba7..bf3e1e21a0 100644 --- a/src/js/_enqueues/wp/shortcode.js +++ b/src/js/_enqueues/wp/shortcode.js @@ -227,8 +227,8 @@ window.wp = window.wp || {}; this.attrs = wp.shortcode.attrs( attrs ); // Identify a correctly formatted `attrs` object. - } else if ( _.isEqual( _.keys( attrs ), [ 'named', 'numeric' ] ) ) { - this.attrs = attrs; + } else if ( _.difference( _.keys( attrs ), [ 'named', 'numeric' ] ).length === 0 ) { + this.attrs = _.defaults( attrs, this.attrs ); // Handle a flat object of attributes. } else { diff --git a/tests/qunit/wp-includes/js/shortcode.js b/tests/qunit/wp-includes/js/shortcode.js index 2fd3c54d4c..c7e269f955 100644 --- a/tests/qunit/wp-includes/js/shortcode.js +++ b/tests/qunit/wp-includes/js/shortcode.js @@ -212,4 +212,29 @@ jQuery( function() { deepEqual( wp.shortcode.attrs('a="foo" b=\'bar\' c=baz foo "bar" \'baz\''), expected, 'attr parsed numeric attributes'); }); + + test( 'string() should accept attrs in any order', function() { + var expected = '[short abc123 foo="bar"]'; + var result; + + result = wp.shortcode.string({ + tag : 'short', + type : 'single', + attrs : { + named : { foo : 'bar' }, + numeric : [ 'abc123' ] + } + }); + deepEqual( result, expected, 'attributes are accepted in any order' ); + + result = wp.shortcode.string({ + tag : 'short', + type : 'single', + attrs : { + numeric : [ 'abc123' ], + named : { foo : 'bar' } + } + }); + deepEqual( result, expected, 'attributes are accepted in any order' ); + }); });