‪TYPO3CMS  11.5
ScriptViewHelper.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 ‪ScriptViewHelper extends AbstractTagBasedViewHelper
38 {
44  protected ‪$escapeOutput = false;
45 
52  protected ‪$escapeChildren = false;
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('async', 'bool', 'Define that the script will be fetched in parallel to parsing and evaluation.', false);
89  $this->registerTagAttribute('crossorigin', 'string', 'Define how to handle crossorigin requests.', false);
90  $this->registerTagAttribute('defer', 'bool', 'Define that the script is meant to be executed after the document has been parsed.', false);
91  $this->registerTagAttribute('integrity', 'string', 'Define base64-encoded cryptographic hash of the resource that allows browsers to verify what they fetch.', false);
92  $this->registerTagAttribute('nomodule', 'bool', 'Define that the script should not be executed in browsers that support ES2015 modules.', false);
93  $this->registerTagAttribute('nonce', 'string', 'Define a cryptographic nonce (number used once) used to whitelist inline styles in a style-src Content-Security-Policy.', false);
94  $this->registerTagAttribute('referrerpolicy', 'string', 'Define which referrer is sent when fetching the resource.', false);
95  $this->registerTagAttribute('src', 'string', 'Define the URI of the external resource.', false);
96  $this->registerTagAttribute('type', 'string', 'Define the MIME type (usually \'text/javascript\').', false);
97  $this->registerArgument(
98  'identifier',
99  'string',
100  'Use this identifier within templates to only inject your JS once, even though it is added multiple times.',
101  true
102  );
103  $this->registerArgument(
104  'priority',
105  'boolean',
106  'Define whether the JavaScript should be put in the <head> tag above-the-fold or somewhere in the body part.',
107  false,
108  false
109  );
110  }
111 
112  public function ‪render(): string
113  {
114  $identifier = (string)$this->arguments['identifier'];
115  $attributes = $this->tag->getAttributes();
116 
117  // boolean attributes shall output attr="attr" if set
118  foreach (['async', 'defer', 'nomodule'] as $_attr) {
119  if ($attributes[$_attr] ?? false) {
120  $attributes[$_attr] = $_attr;
121  }
122  }
123 
124  $src = $this->tag->getAttribute('src');
125  unset($attributes['src']);
126  $options = [
127  'priority' => $this->arguments['priority'],
128  ];
129  if ($src !== null) {
130  $this->assetCollector->addJavaScript($identifier, $src, $attributes, $options);
131  } else {
132  $content = (string)$this->renderChildren();
133  if ($content !== '') {
134  $this->assetCollector->addInlineJavaScript($identifier, $content, $attributes, $options);
135  }
136  }
137  return '';
138  }
139 }
‪TYPO3\CMS\Core\Page\AssetCollector
Definition: AssetCollector.php:42
‪TYPO3\CMS\Fluid\ViewHelpers\Asset\ScriptViewHelper\initializeArguments
‪initializeArguments()
Definition: ScriptViewHelper.php:81
‪TYPO3\CMS\Fluid\ViewHelpers\Asset\ScriptViewHelper\$assetCollector
‪AssetCollector $assetCollector
Definition: ScriptViewHelper.php:54
‪TYPO3\CMS\Fluid\ViewHelpers\Asset\ScriptViewHelper\$escapeOutput
‪bool $escapeOutput
Definition: ScriptViewHelper.php:43
‪TYPO3\CMS\Fluid\ViewHelpers\Asset
Definition: CssViewHelper.php:18
‪TYPO3\CMS\Fluid\ViewHelpers\Asset\ScriptViewHelper\initialize
‪initialize()
Definition: ScriptViewHelper.php:64
‪TYPO3\CMS\Fluid\ViewHelpers\Asset\ScriptViewHelper\$escapeChildren
‪bool $escapeChildren
Definition: ScriptViewHelper.php:50
‪TYPO3\CMS\Fluid\ViewHelpers\Asset\ScriptViewHelper\render
‪render()
Definition: ScriptViewHelper.php:109
‪TYPO3\CMS\Fluid\ViewHelpers\Asset\ScriptViewHelper\injectAssetCollector
‪injectAssetCollector(AssetCollector $assetCollector)
Definition: ScriptViewHelper.php:59
‪TYPO3\CMS\Fluid\ViewHelpers\Asset\ScriptViewHelper
Definition: ScriptViewHelper.php:38