AllowedTagsXssTrait.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. namespace Drupal\Core\Field;
  3. /**
  4. * Useful methods when dealing with displaying allowed tags.
  5. *
  6. * @deprecated in Drupal 8.0.x, will be removed before Drupal 9.0.0. Use
  7. * \Drupal\Core\Field\FieldFilteredMarkup instead.
  8. *
  9. * @see \Drupal\Core\Field\FieldFilteredMarkup
  10. */
  11. trait AllowedTagsXssTrait {
  12. /**
  13. * Filters an HTML string to prevent XSS vulnerabilities.
  14. *
  15. * Like \Drupal\Component\Utility\Xss::filterAdmin(), but with a shorter list
  16. * of allowed tags.
  17. *
  18. * Used for items entered by administrators, like field descriptions, allowed
  19. * values, where some (mainly inline) mark-up may be desired (so
  20. * \Drupal\Component\Utility\Html::escape() is not acceptable).
  21. *
  22. * @param string $string
  23. * The string with raw HTML in it.
  24. *
  25. * @return \Drupal\Core\Field\FieldFilteredMarkup
  26. * An XSS safe version of $string, or an empty string if $string is not
  27. * valid UTF-8.
  28. */
  29. public function fieldFilterXss($string) {
  30. return FieldFilteredMarkup::create($string);
  31. }
  32. /**
  33. * Returns a list of tags allowed by AllowedTagsXssTrait::fieldFilterXss().
  34. */
  35. public function allowedTags() {
  36. return FieldFilteredMarkup::allowedTags();
  37. }
  38. /**
  39. * Returns a human-readable list of allowed tags for display in help texts.
  40. */
  41. public function displayAllowedTags() {
  42. return FieldFilteredMarkup::displayAllowedTags();
  43. }
  44. }