‪TYPO3CMS  11.5
I18nSanitizerBuilder.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 project.
7  *
8  * It is free software; you can redistribute it and/or modify it under the terms
9  * of the MIT License (MIT). For the full copyright and license information,
10  * please read the LICENSE file that was distributed with this source code.
11  *
12  * The TYPO3 project - inspiring people to share!
13  */
14 
15 namespace ‪TYPO3\CMS\Core\Html;
16 
18 use TYPO3\HtmlSanitizer\Behavior;
19 use TYPO3\HtmlSanitizer\Behavior\Attr\UriAttrValueBuilder;
20 use TYPO3\HtmlSanitizer\Builder\BuilderInterface;
21 use TYPO3\HtmlSanitizer\Sanitizer;
22 use TYPO3\HtmlSanitizer\Visitor\CommonVisitor;
23 
31 class ‪I18nSanitizerBuilder implements BuilderInterface
32 {
33  public function ‪build(): Sanitizer
34  {
35  $globalAttrs = $this->‪createGlobalAttrs();
36  $httpUriBuilder = GeneralUtility::makeInstance(UriAttrValueBuilder::class)
37  ->allowSchemes('http', 'https');
38 
39  $behavior = GeneralUtility::makeInstance(Behavior::class)
40  ->withTags(
41  (new Behavior\Tag('a', Behavior\Tag::ALLOW_CHILDREN))
42  ->addAttrs(...$globalAttrs)
43  ->addAttrs(...$this->‪createAttrs('rel', 'target'))
44  ->addAttrs(
45  (new Behavior\Attr('href'))
46  ->withValues(...$httpUriBuilder->getValues()),
47  ),
48  (new Behavior\Tag('b', Behavior\Tag::ALLOW_CHILDREN))
49  ->addAttrs(...$globalAttrs),
50  (new Behavior\Tag('br'))
51  ->addAttrs(...$globalAttrs),
52  (new Behavior\Tag('div', Behavior\Tag::ALLOW_CHILDREN))
53  ->addAttrs(...$globalAttrs),
54  (new Behavior\Tag('em', Behavior\Tag::ALLOW_CHILDREN))
55  ->addAttrs(...$globalAttrs),
56  (new Behavior\Tag('i', Behavior\Tag::ALLOW_CHILDREN))
57  ->addAttrs(...$globalAttrs),
58  (new Behavior\Tag('li', Behavior\Tag::ALLOW_CHILDREN))
59  ->addAttrs(...$globalAttrs),
60  (new Behavior\Tag('span', Behavior\Tag::ALLOW_CHILDREN))
61  ->addAttrs(...$globalAttrs),
62  (new Behavior\Tag('strong', Behavior\Tag::ALLOW_CHILDREN))
63  ->addAttrs(...$globalAttrs),
64  (new Behavior\Tag('ul', Behavior\Tag::ALLOW_CHILDREN))
65  ->addAttrs(...$globalAttrs)
66  );
67 
68  $visitor = GeneralUtility::makeInstance(CommonVisitor::class, $behavior);
69  return GeneralUtility::makeInstance(Sanitizer::class, $behavior, $visitor);
70  }
71 
75  protected function ‪createGlobalAttrs(): array
76  {
77  // https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes
78  $attrs = $this->‪createAttrs(
79  'class',
80  'role',
81  'tabindex',
82  'title',
83  );
84  $attrs[] = new Behavior\Attr('aria-', Behavior\Attr::NAME_PREFIX);
85  $attrs[] = new Behavior\Attr('data-', Behavior\Attr::NAME_PREFIX);
86  return $attrs;
87  }
88 
93  protected function ‪createAttrs(string ...$names): array
94  {
95  return array_map(
96  function (string $name) {
97  return new Behavior\Attr($name);
98  },
99  $names
100  );
101  }
102 }
‪TYPO3\CMS\Core\Html
Definition: DefaultSanitizerBuilder.php:15
‪TYPO3\CMS\Core\Html\I18nSanitizerBuilder\createAttrs
‪Behavior Attr[] createAttrs(string ... $names)
Definition: I18nSanitizerBuilder.php:93
‪TYPO3\CMS\Core\Html\I18nSanitizerBuilder\createGlobalAttrs
‪Behavior Attr[] createGlobalAttrs()
Definition: I18nSanitizerBuilder.php:75
‪TYPO3\CMS\Core\Html\I18nSanitizerBuilder\build
‪build()
Definition: I18nSanitizerBuilder.php:33
‪TYPO3\CMS\Core\Html\I18nSanitizerBuilder
Definition: I18nSanitizerBuilder.php:32
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50