Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / filter / tests / filter_test / src / Plugin / Filter / FilterTestRestrictTagsAndAttributes.php
1 <?php
2
3 namespace Drupal\filter_test\Plugin\Filter;
4
5 use Drupal\filter\FilterProcessResult;
6 use Drupal\filter\Plugin\FilterBase;
7 use Drupal\Component\Utility\Xss;
8
9 /**
10  * Provides a test filter to restrict HTML tags and attributes.
11  *
12  * @Filter(
13  *   id = "filter_test_restrict_tags_and_attributes",
14  *   title = @Translation("Tag and attribute restricting filter"),
15  *   description = @Translation("Used for testing \Drupal\filter\Entity\FilterFormatInterface::getHtmlRestrictions()."),
16  *   type = Drupal\filter\Plugin\FilterInterface::TYPE_HTML_RESTRICTOR
17  * )
18  */
19 class FilterTestRestrictTagsAndAttributes extends FilterBase {
20
21   /**
22    * {@inheritdoc}
23    */
24   public function process($text, $langcode) {
25     $allowed_tags = array_filter($this->settings['restrictions']['allowed'], function ($value) {
26       return is_array($value) || (bool) $value !== FALSE;
27     });
28     return new FilterProcessResult(Xss::filter($text, array_keys($allowed_tags)));
29   }
30
31   /**
32    * {@inheritdoc}
33    */
34   public function getHTMLRestrictions() {
35     $restrictions = $this->settings['restrictions'];
36
37     // The configuration system stores FALSE as '0' and TRUE as '1'. Fix that.
38     if (isset($restrictions['allowed'])) {
39       foreach ($restrictions['allowed'] as $tag => $attrs_or_bool) {
40         if (!is_array($attrs_or_bool)) {
41           $restrictions['allowed'][$tag] = (bool) $attrs_or_bool;
42         }
43         else {
44           foreach ($attrs_or_bool as $attr => $attrvals_or_bool) {
45             if (!is_array($attrvals_or_bool)) {
46               $restrictions['allowed'][$tag][$attr] = (bool) $attrvals_or_bool;
47             }
48             else {
49               foreach ($attrvals_or_bool as $attrval => $bool) {
50                 $restrictions['allowed'][$tag][$attr][$attrval] = (bool) $bool;
51               }
52             }
53           }
54         }
55       }
56     }
57     if (isset($restrictions['forbidden_tags'])) {
58       foreach ($restrictions['forbidden_tags'] as $tag => $bool) {
59         $restrictions['forbidden_tags'][$tag] = (bool) $bool;
60       }
61     }
62
63     return $restrictions;
64   }
65
66 }