Skip to content

Conversation

@westonruter
Copy link
Member

@westonruter westonruter commented Jan 31, 2026

Ensure that wp.sanitize.stripTags() returns an empty string when the input is null or undefined, preventing it from being converted to the string "null" by DOMParser.

Added regression tests.

Trac ticket: https://core.trac.wordpress.org/ticket/64574


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

…efined.

Ensure that `wp.sanitize.stripTags()` returns an empty string when the input is `null` or `undefined`, preventing it from being converted to the string "null" by `DOMParser`.

Added regression tests.

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@github-actions
Copy link

github-actions bot commented Jan 31, 2026

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props westonruter, mukesh27.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions
Copy link

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

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

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes a regression in wp.sanitize.stripTags() where null/undefined inputs could be coerced into the strings "null"/"undefined" via DOMParser, by returning an empty string instead, and adds QUnit regression tests.

Changes:

  • Add an early-return in wp.sanitize.stripTags() for null/undefined.
  • Add QUnit coverage for stripTags() and stripTagsAndEncodeText() with null/undefined inputs.
  • Register the new QUnit test file in the QUnit runner index.html.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
src/js/_enqueues/wp/sanitize.js Adds a null/undefined guard to prevent "null"/"undefined" string coercion through DOMParser.
tests/qunit/wp-includes/js/wp-sanitize.js Introduces regression tests covering null/undefined and basic behavior for sanitize helpers.
tests/qunit/index.html Ensures the new QUnit test file is loaded by the test runner.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 25 to +28
stripTags: function( text ) {
if ( null === text || 'undefined' === typeof text ) {
return '';
}
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.

@mukeshpanchal27
Copy link
Member

@westonruter Can you share testing steps please?

@westonruter
Copy link
Member Author

@mukeshpanchal27 the ticket description has the reproduction info. I guess go to a screen where that script is enqueued, or enqueued the script yourself, and then run the function in the console.

* @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

@sirreal sirreal left a comment

Choose a reason for hiding this comment

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

The test are running and passing and appear correct.

I'll leave the suggestion to your discretion: https://github.com/WordPress/wordpress-develop/pull/10833/changes#r2753604980

@github-actions
Copy link

github-actions bot commented Feb 2, 2026

A commit was made that fixes the Trac ticket referenced in the description of this pull request.

SVN changeset: 61578
GitHub commit: f8c0213

This PR will be closed, but please confirm the accuracy of this and reopen if there is more work to be done.

@github-actions github-actions bot closed this Feb 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants