3 namespace Drupal\Core\Field;
5 use Drupal\Component\Utility\Html;
6 use Drupal\Component\Render\MarkupInterface;
7 use Drupal\Component\Render\MarkupTrait;
8 use Drupal\Component\Utility\Xss;
11 * Defines an object that passes safe strings through the Field system.
13 * This object filters the string using a very restrictive tag list when it is
17 * This object is marked as internal because it should only be used by the
18 * Field module and field-related plugins.
20 * @see \Drupal\Core\Render\Markup
22 final class FieldFilteredMarkup implements MarkupInterface, \Countable {
26 * Overrides \Drupal\Component\Render\MarkupTrait::create().
28 * @return string|\Drupal\Component\Render\MarkupInterface
29 * A safe string filtered with the allowed tag list and normalized.
31 * @see \Drupal\Core\Field\FieldFilteredMarkup::allowedTags()
32 * @see \Drupal\Component\Utility\Xss::filter()
33 * @see \Drupal\Component\Utility\Html::normalize()
35 public static function create($string) {
36 $string = (string) $string;
40 $safe_string = new static();
41 // All known XSS vectors are filtered out by
42 // \Drupal\Component\Utility\Xss::filter(), all tags in the markup are
43 // allowed intentionally by the trait, and no danger is added in by
44 // \Drupal\Component\Utility\HTML::normalize(). Since the normalized value
45 // is essentially the same markup, designate this string as safe as well.
46 // This method is an internal part of field sanitization, so the resultant,
47 // sanitized string should be printable as is.
48 $safe_string->string = Html::normalize(Xss::filter($string, static::allowedTags()));
53 * Returns the allowed tag list.
56 * A list of allowed tags.
58 public static function allowedTags() {
59 return ['a', 'b', 'big', 'code', 'del', 'em', 'i', 'ins', 'pre', 'q', 'small', 'span', 'strong', 'sub', 'sup', 'tt', 'ol', 'ul', 'li', 'p', 'br', 'img'];
63 * Returns a human-readable list of allowed tags for display in help texts.
66 * A human-readable list of allowed tags for display in help texts.
68 public static function displayAllowedTags() {
69 return '<' . implode('> <', static::allowedTags()) . '>';