Skip to content

Commit f8c0213

Browse files
committed
General: Preserve back-compat for wp.sanitize.stripTags() to return empty string when passed null/undefined.
This also bumps the `esversion` to 11 in `tests/qunit/.jshintrc` to match the root `.jshintrc`, following [61544] for #64562. Follow-up to [61347], [60907]. Props westonruter, jonsurrell, mukesh27, hugod. See #64274. Fixes #64574. git-svn-id: https://develop.svn.wordpress.org/trunk@61578 602fd350-edb4-49c9-b593-d223f7449a82
1 parent f0aa4ec commit f8c0213

4 files changed

Lines changed: 46 additions & 0 deletions

File tree

src/js/_enqueues/wp/sanitize.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
* @return {string} Stripped text.
2424
*/
2525
stripTags: function( text ) {
26+
if ( null === text || 'undefined' === typeof text ) {
27+
return '';
28+
}
29+
2630
const domParser = new DOMParser();
2731
const htmlDocument = domParser.parseFromString(
2832
text,

tests/qunit/.jshintrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"esversion": 11,
23
"globals": {
34
"QUnit" : false,
45
"_" : false,

tests/qunit/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@
155155
<script src="wp-includes/js/api-request.js"></script>
156156
<script src="wp-includes/js/jquery.js"></script>
157157
<script src="wp-includes/js/wp-api.js"></script>
158+
<script src="wp-includes/js/wp-sanitize.js"></script>
158159
<script src="wp-admin/js/customize-controls.js"></script>
159160
<script src="wp-admin/js/customize-controls-utils.js"></script>
160161
<script src="wp-admin/js/customize-nav-menus.js"></script>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/* global wp */
2+
3+
QUnit.module( 'wp.sanitize.stripTags' );
4+
5+
QUnit.test( 'stripTags should return empty string for null input', function( assert ) {
6+
const result = wp.sanitize.stripTags( null );
7+
assert.strictEqual( result, '', 'stripTags( null ) should return ""' );
8+
} );
9+
10+
QUnit.test( 'stripTags should return empty string for undefined input', function( assert ) {
11+
const result = wp.sanitize.stripTags( undefined );
12+
assert.strictEqual( result, '', 'stripTags( undefined ) should return ""' );
13+
} );
14+
15+
QUnit.test( 'stripTags should strip tags from string', function( assert ) {
16+
const result = wp.sanitize.stripTags( '<p>Hello <b>World</b></p>' );
17+
assert.strictEqual( result, 'Hello World', 'stripTags( "<p>Hello <b>World</b></p>" ) should return "Hello World"' );
18+
} );
19+
20+
QUnit.test( 'stripTags should convert numbers to strings', function( assert ) {
21+
const result = wp.sanitize.stripTags( 123 );
22+
assert.strictEqual( result, '123', 'stripTags( 123 ) should return "123"' );
23+
} );
24+
25+
QUnit.module( 'wp.sanitize.stripTagsAndEncodeText' );
26+
27+
QUnit.test( 'stripTagsAndEncodeText should return empty string for null input', function( assert ) {
28+
const result = wp.sanitize.stripTagsAndEncodeText( null );
29+
assert.strictEqual( result, '', 'stripTagsAndEncodeText( null ) should return ""' );
30+
} );
31+
32+
QUnit.test( 'stripTagsAndEncodeText should return empty string for undefined input', function( assert ) {
33+
const result = wp.sanitize.stripTagsAndEncodeText( undefined );
34+
assert.strictEqual( result, '', 'stripTagsAndEncodeText( undefined ) should return ""' );
35+
} );
36+
37+
QUnit.test( 'stripTagsAndEncodeText should strip tags and encode text', function( assert ) {
38+
const result = wp.sanitize.stripTagsAndEncodeText( '<p>Hello & <b>World</b></p>' );
39+
assert.strictEqual( result, 'Hello &amp; World', 'stripTagsAndEncodeText( "<p>Hello & <b>World</b></p>" ) should return "Hello &amp; World"' );
40+
} );

0 commit comments

Comments
 (0)