wp-sanitize.js 993 B

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