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