SafeMarkup.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. @trigger_error('SafeMarkup::isSafe() is scheduled for removal in Drupal 9.0.0. Instead, you should just check if a variable is an instance of \Drupal\Component\Render\MarkupInterface. See https://www.drupal.org/node/2549395.', E_USER_DEPRECATED);
  40. return $string instanceof MarkupInterface;
  41. }
  42. /**
  43. * Encodes special characters in a plain-text string for display as HTML.
  44. *
  45. * Also validates strings as UTF-8. All processed strings are also
  46. * automatically flagged as safe markup strings for rendering.
  47. *
  48. * @param string $text
  49. * The text to be checked or processed.
  50. *
  51. * @return \Drupal\Component\Render\HtmlEscapedText
  52. * An HtmlEscapedText object that escapes when rendered to string.
  53. *
  54. * @deprecated Will be removed before Drupal 9.0.0. Rely on Twig's
  55. * auto-escaping feature, or use the @link theme_render #plain_text @endlink
  56. * key when constructing a render array that contains plain text in order to
  57. * use the renderer's auto-escaping feature. If neither of these are
  58. * possible, \Drupal\Component\Utility\Html::escape() can be used in places
  59. * where explicit escaping is needed.
  60. *
  61. * @see https://www.drupal.org/node/2549395
  62. * @see drupal_validate_utf8()
  63. */
  64. public static function checkPlain($text) {
  65. @trigger_error('SafeMarkup::checkPlain() is scheduled for removal in Drupal 9.0.0. Rely on Twig\'s auto-escaping feature, or use the @link theme_render #plain_text @endlink key when constructing a render array that contains plain text in order to use the renderer\'s auto-escaping feature. If neither of these are possible, \Drupal\Component\Utility\Html::escape() can be used in places where explicit escaping is needed. See https://www.drupal.org/node/2549395.', E_USER_DEPRECATED);
  66. return new HtmlEscapedText($text);
  67. }
  68. /**
  69. * Formats a string for HTML display by replacing variable placeholders.
  70. *
  71. * @param string $string
  72. * A string containing placeholders. The string itself will not be escaped,
  73. * any unsafe content must be in $args and inserted via placeholders.
  74. * @param array $args
  75. * An array with placeholder replacements, keyed by placeholder. See
  76. * \Drupal\Component\Render\FormattableMarkup::placeholderFormat() for
  77. * additional information about placeholders.
  78. *
  79. * @return string|\Drupal\Component\Render\MarkupInterface
  80. * The formatted string, which is an instance of MarkupInterface unless
  81. * sanitization of an unsafe argument was suppressed (see above).
  82. *
  83. * @see \Drupal\Component\Render\FormattableMarkup::placeholderFormat()
  84. * @see \Drupal\Component\Render\FormattableMarkup
  85. *
  86. * @deprecated in Drupal 8.0.0, will be removed before Drupal 9.0.0.
  87. * Use \Drupal\Component\Render\FormattableMarkup.
  88. *
  89. * @see https://www.drupal.org/node/2549395
  90. */
  91. public static function format($string, array $args) {
  92. @trigger_error('SafeMarkup::format() is scheduled for removal in Drupal 9.0.0. Use \Drupal\Component\Render\FormattableMarkup. See https://www.drupal.org/node/2549395.', E_USER_DEPRECATED);
  93. return new FormattableMarkup($string, $args);
  94. }
  95. }