diff --git a/src/js/_enqueues/wp/sanitize.js b/src/js/_enqueues/wp/sanitize.js index 4252d0a014f7a..4fec26ab30683 100644 --- a/src/js/_enqueues/wp/sanitize.js +++ b/src/js/_enqueues/wp/sanitize.js @@ -23,22 +23,25 @@ * @return {string} Stripped text. */ stripTags: function( text ) { - let _text = text || ''; + const domParser = new DOMParser(); + const htmlDocument = domParser.parseFromString( + text, + 'text/html' + ); - // Do the search-replace until there is nothing to be replaced. - do { - // Keep pre-replace text for comparison. - text = _text; - - // Do the replacement. - _text = text - .replace( /|$)/g, '' ) - .replace( /<(script|style)[^>]*>[\s\S]*?(<\/\1>|$)/ig, '' ) - .replace( /<\/?[a-z][\s\S]*?(>|$)/ig, '' ); - } while ( _text !== text ); + /* + * The following self-assignment appears to be a no-op, but it isn't. + * It enforces the escaping. Reading the `innerText` property decodes + * character references, returning a raw string. When written, however, + * the text is re-escaped to ensure that the rendered text replicates + * what it's given. + * + * See . + */ + htmlDocument.body.innerText = htmlDocument.body.innerText; // Return the text with stripped tags. - return _text; + return htmlDocument.body.innerHTML; }, /**