diff --git a/src/js/_enqueues/wp/sanitize.js b/src/js/_enqueues/wp/sanitize.js index 4fec26ab30683..7b69d65416ca5 100644 --- a/src/js/_enqueues/wp/sanitize.js +++ b/src/js/_enqueues/wp/sanitize.js @@ -23,6 +23,10 @@ * @return {string} Stripped text. */ stripTags: function( text ) { + if ( null === text || 'undefined' === typeof text ) { + return ''; + } + const domParser = new DOMParser(); const htmlDocument = domParser.parseFromString( text, diff --git a/tests/qunit/index.html b/tests/qunit/index.html index bcd6d8c1c6ddd..6b4c4a1dd8811 100644 --- a/tests/qunit/index.html +++ b/tests/qunit/index.html @@ -155,6 +155,7 @@ + diff --git a/tests/qunit/wp-includes/js/wp-sanitize.js b/tests/qunit/wp-includes/js/wp-sanitize.js new file mode 100644 index 0000000000000..45e687845925e --- /dev/null +++ b/tests/qunit/wp-includes/js/wp-sanitize.js @@ -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( '

Hello World

' ); + assert.strictEqual( result, 'Hello World', 'stripTags( "

Hello World

" ) 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( '

Hello & World

' ); + assert.strictEqual( result, 'Hello & World', 'stripTagsAndEncodeText( "

Hello & World

" ) should return "Hello & World"' ); +} );