Skip to content

Commit 8867703

Browse files
committed
General: Leverage DOMParser to implement wp.sanitize.stripTags().
Developed in #10536 Follow-up to [60907]. Props hbhalodia, dmsnell, westonruter. See #48054. Fixes #64274. git-svn-id: https://develop.svn.wordpress.org/trunk@61347 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 74b60c2 commit 8867703

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

src/js/_enqueues/wp/sanitize.js

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,25 @@
2323
* @return {string} Stripped text.
2424
*/
2525
stripTags: function( text ) {
26-
let _text = text || '';
26+
const domParser = new DOMParser();
27+
const htmlDocument = domParser.parseFromString(
28+
text,
29+
'text/html'
30+
);
2731

28-
// Do the search-replace until there is nothing to be replaced.
29-
do {
30-
// Keep pre-replace text for comparison.
31-
text = _text;
32-
33-
// Do the replacement.
34-
_text = text
35-
.replace( /<!--[\s\S]*?(-->|$)/g, '' )
36-
.replace( /<(script|style)[^>]*>[\s\S]*?(<\/\1>|$)/ig, '' )
37-
.replace( /<\/?[a-z][\s\S]*?(>|$)/ig, '' );
38-
} while ( _text !== text );
32+
/*
33+
* The following self-assignment appears to be a no-op, but it isn't.
34+
* It enforces the escaping. Reading the `innerText` property decodes
35+
* character references, returning a raw string. When written, however,
36+
* the text is re-escaped to ensure that the rendered text replicates
37+
* what it's given.
38+
*
39+
* See <https://github.com/WordPress/wordpress-develop/pull/10536#discussion_r2550615378>.
40+
*/
41+
htmlDocument.body.innerText = htmlDocument.body.innerText;
3942

4043
// Return the text with stripped tags.
41-
return _text;
44+
return htmlDocument.body.innerHTML;
4245
},
4346

4447
/**

0 commit comments

Comments
 (0)