SafeMarkup.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace Drupal\Component\Utility;
  3. use Drupal\Component\Render\HtmlEscapedText;
  4. use Drupal\Component\Render\FormattableMarkup;
  5. use Drupal\Component\Render\MarkupInterface;
  6. /**
  7. * Contains deprecated functionality related to sanitization of markup.
  8. *
  9. * @deprecated Will be removed before Drupal 9.0.0. Use the appropriate
  10. * @link sanitization sanitization functions @endlink or the @link theme_render theme and render systems @endlink
  11. * so that the output can can be themed, escaped, and altered properly.
  12. *
  13. * @see https://www.drupal.org/node/2549395
  14. *
  15. * @see TwigExtension::escapeFilter()
  16. * @see twig_render_template()
  17. * @see sanitization
  18. * @see theme_render
  19. */
  20. class SafeMarkup {
  21. /**
  22. * Checks if a string is safe to output.
  23. *
  24. * @param string|\Drupal\Component\Render\MarkupInterface $string
  25. * The content to be checked.
  26. * @param string $strategy
  27. * (optional) This value is ignored.
  28. *
  29. * @return bool
  30. * TRUE if the string has been marked secure, FALSE otherwise.
  31. *
  32. * @deprecated in Drupal 8.0.x-dev, will be removed before Drupal 9.0.0.
  33. * Instead, you should just check if a variable is an instance of
  34. * \Drupal\Component\Render\MarkupInterface.
  35. *
  36. * @see https://www.drupal.org/node/2549395
  37. */
  38. public static function isSafe($string, $strategy = 'html') {
  39. return $string instanceof MarkupInterface;
  40. }
  41. /**
  42. * Encodes special characters in a plain-text string for display as HTML.
  43. *
  44. * Also validates strings as UTF-8. All processed strings are also
  45. * automatically flagged as safe markup strings for rendering.
  46. *
  47. * @param string $text
  48. * The text to be checked or processed.
  49. *
  50. * @return \Drupal\Component\Render\HtmlEscapedText
  51. * An HtmlEscapedText object that escapes when rendered to string.
  52. *
  53. * @deprecated Will be removed before Drupal 9.0.0. Rely on Twig's
  54. * auto-escaping feature, or use the @link theme_render #plain_text @endlink
  55. * key when constructing a render array that contains plain text in order to
  56. * use the renderer's auto-escaping feature. If neither of these are
  57. * possible, \Drupal\Component\Utility\Html::escape() can be used in places
  58. * where explicit escaping is needed.
  59. *
  60. * @see https://www.drupal.org/node/2549395
  61. * @see drupal_validate_utf8()
  62. */
  63. public static function checkPlain($text) {
  64. return new HtmlEscapedText($text);
  65. }
  66. /**
  67. * Formats a string for HTML display by replacing variable placeholders.
  68. *
  69. * @param string $string
  70. * A string containing placeholders. The string itself will not be escaped,
  71. * any unsafe content must be in $args and inserted via placeholders.
  72. * @param array $args
  73. * An array with placeholder replacements, keyed by placeholder. See
  74. * \Drupal\Component\Render\FormattableMarkup::placeholderFormat() for
  75. * additional information about placeholders.
  76. *
  77. * @return string|\Drupal\Component\Render\MarkupInterface
  78. * The formatted string, which is an instance of MarkupInterface unless
  79. * sanitization of an unsafe argument was suppressed (see above).
  80. *
  81. * @see \Drupal\Component\Render\FormattableMarkup::placeholderFormat()
  82. * @see \Drupal\Component\Render\FormattableMarkup
  83. *
  84. * @deprecated in Drupal 8.0.0, will be removed before Drupal 9.0.0.
  85. * Use \Drupal\Component\Render\FormattableMarkup.
  86. *
  87. * @see https://www.drupal.org/node/2549395
  88. */
  89. public static function format($string, array $args) {
  90. return new FormattableMarkup($string, $args);
  91. }
  92. }