wp/wp-includes/js/wp-sanitize.js
changeset 7 cf61fcea0001
child 9 177826044cd9
equal deleted inserted replaced
6:490d5cc509ed 7:cf61fcea0001
       
     1 ( function () {
       
     2 
       
     3 	window.wp = window.wp || {};
       
     4 
       
     5 	/**
       
     6 	 * wp.sanitize
       
     7 	 *
       
     8 	 * Helper functions to sanitize strings.
       
     9 	 */
       
    10 	wp.sanitize = {
       
    11 
       
    12 		/**
       
    13 		 * Strip HTML tags.
       
    14 		 *
       
    15 		 * @param {string} text Text to have the HTML tags striped out of.
       
    16 		 *
       
    17 		 * @return  Stripped text.
       
    18 		 */
       
    19 		stripTags: function( text ) {
       
    20 			text = text || '';
       
    21 
       
    22 			return text
       
    23 				.replace( /<!--[\s\S]*?(-->|$)/g, '' )
       
    24 				.replace( /<(script|style)[^>]*>[\s\S]*?(<\/\1>|$)/ig, '' )
       
    25 				.replace( /<\/?[a-z][\s\S]*?(>|$)/ig, '' );
       
    26 		},
       
    27 
       
    28 		/**
       
    29 		 * Strip HTML tags and convert HTML entities.
       
    30 		 *
       
    31 		 * @param {string} text Text to strip tags and convert HTML entities.
       
    32 		 *
       
    33 		 * @return Sanitized text. False on failure.
       
    34 		 */
       
    35 		stripTagsAndEncodeText: function( text ) {
       
    36 			var _text = wp.sanitize.stripTags( text ),
       
    37 				textarea = document.createElement( 'textarea' );
       
    38 
       
    39 			try {
       
    40 				textarea.innerHTML = _text;
       
    41 				_text = wp.sanitize.stripTags( textarea.value );
       
    42 			} catch ( er ) {}
       
    43 
       
    44 			return _text;
       
    45 		}
       
    46 	};
       
    47 }() );