2015-08-17 17:00:26 -07:00
< ? php
/**
* @ file
* Contains \Drupal\Component\Utility\SafeMarkup .
*/
namespace Drupal\Component\Utility ;
2015-10-08 11:40:12 -07:00
use Drupal\Component\Render\HtmlEscapedText ;
use Drupal\Component\Render\FormattableMarkup ;
use Drupal\Component\Render\MarkupInterface ;
2015-08-17 17:00:26 -07:00
/**
2015-10-08 11:40:12 -07:00
* Contains deprecated functionality related to sanitization of markup .
2015-08-17 17:00:26 -07:00
*
2015-10-08 11:40:12 -07:00
* @ deprecated Will be removed before Drupal 9.0 . 0. Use the appropriate
* @ link sanitization sanitization functions @ endlink or the @ link theme_render theme and render systems @ endlink
* so that the output can can be themed , escaped , and altered properly .
2015-08-17 17:00:26 -07:00
*
* @ see TwigExtension :: escapeFilter ()
* @ see twig_render_template ()
* @ see sanitization
* @ see theme_render
*/
class SafeMarkup {
/**
2015-09-04 13:20:09 -07:00
* Checks if a string is safe to output .
2015-08-17 17:00:26 -07:00
*
2015-10-08 11:40:12 -07:00
* @ param string | \Drupal\Component\Render\MarkupInterface $string
2015-09-04 13:20:09 -07:00
* The content to be checked .
2015-08-17 17:00:26 -07:00
* @ param string $strategy
2015-10-08 11:40:12 -07:00
* ( optional ) This value is ignored .
2015-08-17 17:00:26 -07:00
*
* @ return bool
* TRUE if the string has been marked secure , FALSE otherwise .
*
2015-10-08 11:40:12 -07:00
* @ deprecated in Drupal 8.0 . x - dev , will be removed before Drupal 9.0 . 0.
* Instead , you should just check if a variable is an instance of
* \Drupal\Component\Render\MarkupInterface .
2015-08-17 17:00:26 -07:00
*/
2015-10-08 11:40:12 -07:00
public static function isSafe ( $string , $strategy = 'html' ) {
2015-10-21 21:44:50 -07:00
return $string instanceof MarkupInterface ;
2015-08-17 17:00:26 -07:00
}
/**
* Encodes special characters in a plain - text string for display as HTML .
*
* Also validates strings as UTF - 8. All processed strings are also
* automatically flagged as safe markup strings for rendering .
*
* @ param string $text
* The text to be checked or processed .
*
2015-10-08 11:40:12 -07:00
* @ return \Drupal\Component\Render\HtmlEscapedText
* An HtmlEscapedText object that escapes when rendered to string .
2015-08-17 17:00:26 -07:00
*
2015-10-08 11:40:12 -07:00
* @ deprecated Will be removed before Drupal 9.0 . 0. Rely on Twig ' s
2015-09-04 13:20:09 -07:00
* 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 .
*
2015-08-17 17:00:26 -07:00
* @ see drupal_validate_utf8 ()
*/
public static function checkPlain ( $text ) {
2015-10-08 11:40:12 -07:00
return new HtmlEscapedText ( $text );
2015-08-17 17:00:26 -07:00
}
/**
* Formats a string for HTML display by replacing variable placeholders .
*
* @ param string $string
2015-10-08 11:40:12 -07:00
* A string containing placeholders . The string itself will not be escaped ,
* any unsafe content must be in $args and inserted via placeholders .
2015-08-17 17:00:26 -07:00
* @ param array $args
2015-10-08 11:40:12 -07:00
* An array with placeholder replacements , keyed by placeholder . See
* \Drupal\Component\Render\FormattableMarkup :: placeholderFormat () for
* additional information about placeholders .
2015-08-17 17:00:26 -07:00
*
2015-10-08 11:40:12 -07:00
* @ return string | \Drupal\Component\Render\MarkupInterface
* The formatted string , which is an instance of MarkupInterface unless
* sanitization of an unsafe argument was suppressed ( see above ) .
2015-08-17 17:00:26 -07:00
*
2015-10-08 11:40:12 -07:00
* @ see \Drupal\Component\Render\FormattableMarkup :: placeholderFormat ()
* @ see \Drupal\Component\Render\FormattableMarkup
2015-08-17 17:00:26 -07:00
*
2015-10-08 11:40:12 -07:00
* @ deprecated in Drupal 8.0 . 0 , will be removed before Drupal 9.0 . 0.
* Use \Drupal\Component\Render\FormattableMarkup .
2015-08-17 17:00:26 -07:00
*/
2015-09-04 13:20:09 -07:00
public static function format ( $string , array $args ) {
2015-10-08 11:40:12 -07:00
return new FormattableMarkup ( $string , $args );
2015-08-17 17:00:26 -07:00
}
}