Upgraded drupal core with security updates
[yaffs-website] / web / core / lib / Drupal / Core / Field / FieldFilteredMarkup.php
1 <?php
2
3 namespace Drupal\Core\Field;
4
5 use Drupal\Component\Utility\Html;
6 use Drupal\Component\Render\MarkupInterface;
7 use Drupal\Component\Render\MarkupTrait;
8 use Drupal\Component\Utility\Xss;
9
10 /**
11  * Defines an object that passes safe strings through the Field system.
12  *
13  * This object filters the string using a very restrictive tag list when it is
14  * created.
15  *
16  * @internal
17  *   This object is marked as internal because it should only be used by the
18  *   Field module and field-related plugins.
19  *
20  * @see \Drupal\Core\Render\Markup
21  */
22 final class FieldFilteredMarkup implements MarkupInterface, \Countable {
23   use MarkupTrait;
24
25   /**
26    * Overrides \Drupal\Component\Render\MarkupTrait::create().
27    *
28    * @return string|\Drupal\Component\Render\MarkupInterface
29    *   A safe string filtered with the allowed tag list and normalized.
30    *
31    * @see \Drupal\Core\Field\FieldFilteredMarkup::allowedTags()
32    * @see \Drupal\Component\Utility\Xss::filter()
33    * @see \Drupal\Component\Utility\Html::normalize()
34    */
35   public static function create($string) {
36     $string = (string) $string;
37     if ($string === '') {
38       return '';
39     }
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()));
49     return $safe_string;
50   }
51
52   /**
53    * Returns the allowed tag list.
54    *
55    * @return string[]
56    *   A list of allowed tags.
57    */
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'];
60   }
61
62   /**
63    * Returns a human-readable list of allowed tags for display in help texts.
64    *
65    * @return string
66    *   A human-readable list of allowed tags for display in help texts.
67    */
68   public static function displayAllowedTags() {
69     return '<' . implode('> <', static::allowedTags()) . '>';
70   }
71
72 }