Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/js/_enqueues/wp/shortcode.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ window.wp = window.wp || {};

// Make sure to return any of the extra brackets if they
// weren't used to escape the shortcode.
return result ? left + result + right : match;
return result || '' === result ? left + result + right : match;
});
},

Expand Down
56 changes: 56 additions & 0 deletions tests/qunit/wp-includes/js/shortcode.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,62 @@ jQuery( function() {
assert.equal( result, 'this has the [foo param="bar"] shortcode', 'bar not found with params' );
});

QUnit.test( 'replace() should remove shortcodes when the callback returns an empty string', function( assert ) {
var examples = [
[ '[foo]', '' ],
[ 'before [foo /] after', 'before after' ],
[ 'before [foo param="bar"]remove me[/foo] after', 'before after' ],
[ 'before [foo]first[/foo] between [foo /] and [foo]remove me[/foo] after', 'before between and after' ]
];

jQuery.each( examples, function( index, example ) {
var result = wp.shortcode.replace( 'foo', example[0], function() {
return '';
} );

assert.strictEqual( result, example[1], example[0] );
} );
});

QUnit.test( 'replace() should preserve escaping when the callback returns an empty string', function( assert ) {
var calls = 0,
result = wp.shortcode.replace( 'foo', 'before [[foo]] [foo] after', function() {
calls++;
return '';
} );

assert.strictEqual( result, 'before [[foo]] after', 'Escaped shortcodes are preserved' );
assert.strictEqual( calls, 1, 'The callback only runs for unescaped shortcodes' );

jQuery.each( [ [ '[[foo]', '[' ], [ '[foo]]', ']' ] ], function( index, example ) {
result = wp.shortcode.replace( 'foo', example[0], function() {
return '';
} );

assert.strictEqual( result, example[1], 'Unmatched brackets are preserved in ' + example[0] );
} );
});

QUnit.test( 'replace() should preserve shortcodes for other falsey callback results', function( assert ) {
jQuery.each( [ false, null, undefined, 0, NaN ], function( index, value ) {
var result = wp.shortcode.replace( 'foo', 'before [foo] after', function() {
return value;
} );

assert.strictEqual( result, 'before [foo] after', 'Preserves the shortcode for ' + value );
} );
});

QUnit.test( 'replace() should accept nonempty callback strings', function( assert ) {
jQuery.each( [ '0', ' ' ], function( index, value ) {
var result = wp.shortcode.replace( 'foo', 'before [foo] after', function() {
return value;
} );

assert.strictEqual( result, 'before ' + value + ' after', 'Replaces the shortcode with "' + value + '"' );
} );
});

QUnit.test( 'replace() should replace the shortcode in all instances of its use', function( assert ) {
var result;

Expand Down
Loading