Skip to content
Closed
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
4 changes: 4 additions & 0 deletions src/js/_enqueues/wp/sanitize.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
* @return {string} Stripped text.
*/
stripTags: function( text ) {
if ( null === text || 'undefined' === typeof text ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: I consider null == text to be the canonical "null or undefined" check, it's well specified behavior.

Is there a reason to use a typeof check? I associate 'undefined' === typeof variable with a test where we don't know whether a variable is defined.

Suggested change
if ( null === text || 'undefined' === typeof text ) {
if ( null == text ) {

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. This is part of WordPress's JavaScript Coding Standards on Equality:

Strict equality checks (===) must be used in favor of abstract equality checks (==).

So avoiding the loose comparison is in keeping with the coding standards.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My suggestion is called out specifically as the preferred null or undefined check in those guidelines:

These are the preferred ways of checking the type of an object:

  • null or undefined: object == null

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I missed that. I'll follow up.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #10856

return '';
}
Comment on lines 25 to +28
Copy link

Copilot AI Feb 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The JSDoc for stripTags() still declares text as {string}, but the function now explicitly supports null/undefined by returning an empty string (and tests also pass a number). Please update the parameter type (and ideally stripTagsAndEncodeText()'s matching JSDoc) so the documented contract matches the behavior.

Copilot uses AI. Check for mistakes.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is necessary. We don't want a non-string value to be passed. Accepting null or undefined is a back-compat consideration.


const domParser = new DOMParser();
const htmlDocument = domParser.parseFromString(
text,
Expand Down
1 change: 1 addition & 0 deletions tests/qunit/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@
<script src="wp-includes/js/api-request.js"></script>
<script src="wp-includes/js/jquery.js"></script>
<script src="wp-includes/js/wp-api.js"></script>
<script src="wp-includes/js/wp-sanitize.js"></script>
<script src="wp-admin/js/customize-controls.js"></script>
<script src="wp-admin/js/customize-controls-utils.js"></script>
<script src="wp-admin/js/customize-nav-menus.js"></script>
Expand Down
40 changes: 40 additions & 0 deletions tests/qunit/wp-includes/js/wp-sanitize.js
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gemini happily generated all these tests.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about adding tests for wp.sanitize.stripTags( 0 ), wp.sanitize.stripTags( '0' ) and wp.sanitize.stripTags( '' )? That way, more edge cases would be covered.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Hug0-Drelon It's good that you requested this because it revealed additional back-compat breaks in 7.0, namely that in 6.9 wp.sanitize.stripTags( 0 ) and wp.sanitize.stripTags( false ) both return empty strings. In 7.0-alpha. however, it was returning '0' and 'false'. I've opened #10856 to correct this.

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/* global wp */

QUnit.module( 'wp.sanitize.stripTags' );

QUnit.test( 'stripTags should return empty string for null input', function( assert ) {
const result = wp.sanitize.stripTags( null );
assert.strictEqual( result, '', 'stripTags( null ) should return ""' );
} );

QUnit.test( 'stripTags should return empty string for undefined input', function( assert ) {
const result = wp.sanitize.stripTags( undefined );
assert.strictEqual( result, '', 'stripTags( undefined ) should return ""' );
} );

QUnit.test( 'stripTags should strip tags from string', function( assert ) {
const result = wp.sanitize.stripTags( '<p>Hello <b>World</b></p>' );
assert.strictEqual( result, 'Hello World', 'stripTags( "<p>Hello <b>World</b></p>" ) should return "Hello World"' );
} );

QUnit.test( 'stripTags should convert numbers to strings', function( assert ) {
const result = wp.sanitize.stripTags( 123 );
assert.strictEqual( result, '123', 'stripTags( 123 ) should return "123"' );
} );

QUnit.module( 'wp.sanitize.stripTagsAndEncodeText' );

QUnit.test( 'stripTagsAndEncodeText should return empty string for null input', function( assert ) {
const result = wp.sanitize.stripTagsAndEncodeText( null );
assert.strictEqual( result, '', 'stripTagsAndEncodeText( null ) should return ""' );
} );

QUnit.test( 'stripTagsAndEncodeText should return empty string for undefined input', function( assert ) {
const result = wp.sanitize.stripTagsAndEncodeText( undefined );
assert.strictEqual( result, '', 'stripTagsAndEncodeText( undefined ) should return ""' );
} );

QUnit.test( 'stripTagsAndEncodeText should strip tags and encode text', function( assert ) {
const result = wp.sanitize.stripTagsAndEncodeText( '<p>Hello & <b>World</b></p>' );
assert.strictEqual( result, 'Hello &amp; World', 'stripTagsAndEncodeText( "<p>Hello & <b>World</b></p>" ) should return "Hello &amp; World"' );
} );
Loading