From a3ce18ce2c11d8a7a5bebbefb61560b1aefd8952 Mon Sep 17 00:00:00 2001 From: salvatorecorsi <86927009+salvatorecorsi@users.noreply.github.com> Date: Mon, 2 Nov 2026 07:32:56 +0100 Subject: [PATCH] Shortcodes: Honor empty-string replacements in wp.shortcode.replace() --- src/js/_enqueues/wp/shortcode.js | 2 +- tests/qunit/wp-includes/js/shortcode.js | 56 +++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/src/js/_enqueues/wp/shortcode.js b/src/js/_enqueues/wp/shortcode.js index 1818d20c5192d..05de6dbea841b 100644 --- a/src/js/_enqueues/wp/shortcode.js +++ b/src/js/_enqueues/wp/shortcode.js @@ -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; }); }, diff --git a/tests/qunit/wp-includes/js/shortcode.js b/tests/qunit/wp-includes/js/shortcode.js index af1b25847cb8e..456cf5ffabd10 100644 --- a/tests/qunit/wp-includes/js/shortcode.js +++ b/tests/qunit/wp-includes/js/shortcode.js @@ -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;