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