be35d96b4232a0e45ff76d713c7313557035006d
[yaffs-website] / web / core / lib / Drupal / Component / Utility / SafeMarkup.php
1 <?php
2
3 namespace Drupal\Component\Utility;
4
5 use Drupal\Component\Render\HtmlEscapedText;
6 use Drupal\Component\Render\FormattableMarkup;
7 use Drupal\Component\Render\MarkupInterface;
8
9 /**
10  * Contains deprecated functionality related to sanitization of markup.
11  *
12  * @deprecated Will be removed before Drupal 9.0.0. Use the appropriate
13  *   @link sanitization sanitization functions @endlink or the @link theme_render theme and render systems @endlink
14  *   so that the output can can be themed, escaped, and altered properly.
15  *
16  * @see TwigExtension::escapeFilter()
17  * @see twig_render_template()
18  * @see sanitization
19  * @see theme_render
20  */
21 class SafeMarkup {
22
23   /**
24    * Checks if a string is safe to output.
25    *
26    * @param string|\Drupal\Component\Render\MarkupInterface $string
27    *   The content to be checked.
28    * @param string $strategy
29    *   (optional) This value is ignored.
30    *
31    * @return bool
32    *   TRUE if the string has been marked secure, FALSE otherwise.
33    *
34    * @deprecated in Drupal 8.0.x-dev, will be removed before Drupal 9.0.0.
35    *   Instead, you should just check if a variable is an instance of
36    *   \Drupal\Component\Render\MarkupInterface.
37    */
38   public static function isSafe($string, $strategy = 'html') {
39     return $string instanceof MarkupInterface;
40   }
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 drupal_validate_utf8()
62    */
63   public static function checkPlain($text) {
64     return new HtmlEscapedText($text);
65   }
66
67   /**
68    * Formats a string for HTML display by replacing variable placeholders.
69    *
70    * @param string $string
71    *   A string containing placeholders. The string itself will not be escaped,
72    *   any unsafe content must be in $args and inserted via placeholders.
73    * @param array $args
74    *   An array with placeholder replacements, keyed by placeholder. See
75    *   \Drupal\Component\Render\FormattableMarkup::placeholderFormat() for
76    *   additional information about placeholders.
77    *
78    * @return string|\Drupal\Component\Render\MarkupInterface
79    *   The formatted string, which is an instance of MarkupInterface unless
80    *   sanitization of an unsafe argument was suppressed (see above).
81    *
82    * @see \Drupal\Component\Render\FormattableMarkup::placeholderFormat()
83    * @see \Drupal\Component\Render\FormattableMarkup
84    *
85    * @deprecated in Drupal 8.0.0, will be removed before Drupal 9.0.0.
86    *   Use \Drupal\Component\Render\FormattableMarkup.
87    */
88   public static function format($string, array $args) {
89     return new FormattableMarkup($string, $args);
90   }
91
92 }