- md5
- 15028204776bdc7d9334ebffdd73d93e
- sha1
- 6b0bb6f3afe9d81496acee4be6daa45e6f3f43c2
- sha256
- 7089d4108f0913e99994b3dbfcb96c00752191925d68a3ee8a3efc9b01e32c5d
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
/**
* @output wp-includes/js/wp-sanitize.js
*/
/**
* Provides helper functions to sanitize strings.
*/
( function () {
window.wp = window.wp || {};
/**
* wp.sanitize
*
* Helper functions to sanitize strings.
*/
wp.sanitize = {
/**
* Strip HTML tags.
*
* @param {string} text Text to strip the HTML tags from.
*
* @return {string} Stripped text.
*/
stripTags: function( text ) {
if ( 'string' !== typeof text ) {
return '';
}
const domParser = new DOMParser();
const htmlDocument = domParser.parseFromString(
text,
'text/html'
);
/*
* 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 <https://github.com/WordPress/wordpress-develop/pull/10536#discussion_r2550615378>.
*/
htmlDocument.body.innerText = htmlDocument.body.innerText;
// Return the text with stripped tags.
return htmlDocument.body.innerHTML;
},
/**
* Strip HTML tags and convert HTML entities.
*
* @param {string} text Text to strip tags and convert HTML entities.
*
* @return {string} Sanitized text.
*/
stripTagsAndEncodeText: function( text ) {
let _text = wp.sanitize.stripTags( text ),
textarea = document.createElement( 'textarea' );
try {
textarea.textContent = _text;
_text = wp.sanitize.stripTags( textarea.value );
} catch ( er ) {}
return _text;
}
};
}() );
The same content is in 7 releases, WordPress 7.2-beta1 to 7.2-RC2. See them all, or every version of this file.