‪TYPO3CMS  ‪main
CssViewHelper.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 
19 
21 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
22 use TYPO3Fluid\Fluid\Core\ViewHelper\TagBuilder;
23 
47 final class ‪CssViewHelper extends AbstractTagBasedViewHelper
48 {
54  protected ‪$escapeOutput = false;
55 
62  protected ‪$escapeChildren = true;
63 
65 
67  {
68  $this->assetCollector = ‪$assetCollector;
69  }
70 
71  public function ‪initialize(): void
72  {
73  // Add a tag builder, that does not html encode values, because rendering with encoding happens in AssetRenderer
74  $this->setTagBuilder(
75  new class () extends TagBuilder {
76  public function addAttribute($attributeName, $attributeValue, $escapeSpecialCharacters = false): void
77  {
78  parent::addAttribute($attributeName, $attributeValue, false);
79  }
80  }
81  );
82  parent::initialize();
83  }
84 
85  public function ‪initializeArguments(): void
86  {
87  parent::initializeArguments();
88  $this->registerUniversalTagAttributes();
89  $this->registerTagAttribute('as', 'string', 'Define the type of content being loaded (For rel="preload" or rel="prefetch" only).', false);
90  $this->registerTagAttribute('crossorigin', 'string', 'Define how to handle crossorigin requests.', false);
91  $this->registerTagAttribute('disabled', 'bool', 'Define whether or not the described stylesheet should be loaded and applied to the document.', false);
92  $this->registerTagAttribute('href', 'string', 'Define the URL of the resource (absolute or relative).', false);
93  $this->registerTagAttribute('hreflang', 'string', 'Define the language of the resource (Only to be used if \'href\' is set).', false);
94  $this->registerTagAttribute('importance', 'string', 'Define the relative fetch priority of the resource.', false);
95  $this->registerTagAttribute('integrity', 'string', 'Define base64-encoded cryptographic hash of the resource that allows browsers to verify what they fetch.', false);
96  $this->registerTagAttribute('media', 'string', 'Define which media type the resources applies to.', false);
97  $this->registerTagAttribute('referrerpolicy', 'string', 'Define which referrer is sent when fetching the resource.', false);
98  $this->registerTagAttribute('rel', 'string', 'Define the relationship of the target object to the link object.', false);
99  $this->registerTagAttribute('sizes', 'string', 'Define the icon size of the resource.', false);
100  $this->registerTagAttribute('type', 'string', 'Define the MIME type (usually \'text/css\').', false);
101  $this->registerTagAttribute('nonce', 'string', 'Define a cryptographic nonce (number used once) used to whitelist inline styles in a style-src Content-Security-Policy.', false);
102  $this->registerArgument('useNonce', 'bool', 'Whether to use the global nonce value', false, false);
103  $this->registerArgument(
104  'identifier',
105  'string',
106  'Use this identifier within templates to only inject your CSS once, even though it is added multiple times.',
107  true
108  );
109  $this->registerArgument(
110  'priority',
111  'boolean',
112  'Define whether the CSS should be included before other CSS. CSS will always be output in the <head> tag.',
113  false,
114  false
115  );
116  }
117 
118  public function ‪render(): string
119  {
120  ‪$identifier = (string)$this->arguments['identifier'];
121  $attributes = $this->tag->getAttributes();
122 
123  // boolean attributes shall output attr="attr" if set
124  if ($attributes['disabled'] ?? false) {
125  $attributes['disabled'] = 'disabled';
126  }
127 
128  $file = $attributes['href'] ?? null;
129  unset($attributes['href']);
130  $options = [
131  'priority' => $this->arguments['priority'],
132  'useNonce' => $this->arguments['useNonce'],
133  ];
134  if ($file !== null) {
135  $this->assetCollector->addStyleSheet(‪$identifier, $file, $attributes, $options);
136  } else {
137  $content = (string)$this->renderChildren();
138  if ($content !== '') {
139  $this->assetCollector->addInlineStyleSheet(‪$identifier, $content, $attributes, $options);
140  }
141  }
142  return '';
143  }
144 }
‪TYPO3\CMS\Fluid\ViewHelpers\Asset\CssViewHelper\initialize
‪initialize()
Definition: CssViewHelper.php:69
‪TYPO3\CMS\Core\Page\AssetCollector
Definition: AssetCollector.php:42
‪TYPO3\CMS\Fluid\ViewHelpers\Asset\CssViewHelper\render
‪render()
Definition: CssViewHelper.php:116
‪TYPO3\CMS\Fluid\ViewHelpers\Asset\CssViewHelper\$assetCollector
‪AssetCollector $assetCollector
Definition: CssViewHelper.php:62
‪TYPO3\CMS\Fluid\ViewHelpers\Asset\CssViewHelper\initializeArguments
‪initializeArguments()
Definition: CssViewHelper.php:83
‪TYPO3\CMS\Fluid\ViewHelpers\Asset
Definition: CssViewHelper.php:18
‪TYPO3\CMS\Fluid\ViewHelpers\Asset\CssViewHelper\$escapeChildren
‪bool $escapeChildren
Definition: CssViewHelper.php:60
‪TYPO3\CMS\Fluid\ViewHelpers\Asset\CssViewHelper\injectAssetCollector
‪injectAssetCollector(AssetCollector $assetCollector)
Definition: CssViewHelper.php:64
‪TYPO3\CMS\Fluid\ViewHelpers\Asset\CssViewHelper
Definition: CssViewHelper.php:48
‪TYPO3\CMS\Fluid\ViewHelpers\Asset\CssViewHelper\$escapeOutput
‪bool $escapeOutput
Definition: CssViewHelper.php:53
‪TYPO3\CMS\Webhooks\Message\$identifier
‪identifier readonly string $identifier
Definition: FileAddedMessage.php:37