1/**
2 * @output wp-includes/js/wp-sanitize.js
3 */
4
5/* eslint-env es6 */
6
7( function () {
8
9 window.wp = window.wp || {};
10
11 /**
12 * wp.sanitize
13 *
14 * Helper functions to sanitize strings.
15 */
16 wp.sanitize = {
17
18 /**
19 * Strip HTML tags.
20 *
21 * @param {string} text - Text to strip the HTML tags from.
22 *
23 * @return {string} Stripped text.
24 */
25 stripTags: function( text ) {
26 let _text = text || '';
27
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 );
39
40 // Return the text with stripped tags.
41 return _text;
42 },
43
44 /**
45 * Strip HTML tags and convert HTML entities.
46 *
47 * @param {string} text - Text to strip tags and convert HTML entities.
48 *
49 * @return {string} Sanitized text.
50 */
51 stripTagsAndEncodeText: function( text ) {
52 let _text = wp.sanitize.stripTags( text ),
53 textarea = document.createElement( 'textarea' );
54
55 try {
56 textarea.textContent = _text;
57 _text = wp.sanitize.stripTags( textarea.value );
58 } catch ( er ) {}
59
60 return _text;
61 }
62 };
63}() );
64