-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Fix regression in wp.sanitize.stripTags() when input is null or undefined
#10833
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,10 @@ | |
| * @return {string} Stripped text. | ||
| */ | ||
| stripTags: function( text ) { | ||
| if ( null === text || 'undefined' === typeof text ) { | ||
| return ''; | ||
| } | ||
|
Comment on lines
25
to
+28
|
||
|
|
||
| const domParser = new DOMParser(); | ||
| const htmlDocument = domParser.parseFromString( | ||
| text, | ||
|
|
||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Gemini happily generated all these tests. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you think about adding tests for
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
| 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 & World', 'stripTagsAndEncodeText( "<p>Hello & <b>World</b></p>" ) should return "Hello & World"' ); | ||
| } ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor: I consider
null == textto be the canonical "null or undefined" check, it's well specified behavior.Is there a reason to use a
typeofcheck? I associate'undefined' === typeof variablewith a test where we don't know whether a variable is defined.There was a problem hiding this comment.
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:
So avoiding the loose comparison is in keeping with the coding standards.
There was a problem hiding this comment.
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:
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See #10856