8000 [AssetMapper] Fix unable to use asset mapper with CSP by vtsykun · Pull Request #50456 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[AssetMapper] Fix unable to use asset mapper with CSP #50456

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Fix unable to use asset mapper with CSP
  • Loading branch information
vtsykun committed May 29, 2023
commit 0a0a2c9d04b3ea456bb312c07a48217ecd66b87f
4 changes: 2 additions & 2 deletions src/Symfony/Bridge/Twig/Extension/ImportMapRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public function __construct(private readonly ImportMapRenderer $importMapRendere
{
}

public function importmap(?string $entryPoint = 'app'): string
public function importmap(?string $entryPoint = 'app', array $attributes = []): string
{
return $this->importMapRenderer->render($entryPoint);
return $this->importMapRenderer->render($entryPoint, $attributes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,16 @@ public function __construct(
) {
}

public function render(string $entryPoint = null): string
public function render(string $entryPoint = null, array $attributes = []): string
{
$attributeString = '';

if (isset($this->scriptAttributes['src']) || isset($this->scriptAttributes['type'])) {
$attributes += $this->scriptAttributes;
if (isset($attributes['src']) || isset($attributes['type'])) {
throw new \InvalidArgumentException(sprintf('The "src" and "type" attributes are not allowed on the <script> tag rendered by "%s".', self::class));
}

foreach ($this->scriptAttributes as $name => $value) {
foreach ($attributes as $name => $value) {
$attributeString .= ' ';
if (true === $value) {
$attributeString .= $name;
Expand Down Expand Up @@ -70,7 +71,7 @@ public function render(string $entryPoint = null): string
}

if (null !== $entryPoint) {
$output .= "\n<script type=\"module\">import '".str_replace("'", "\\'", $entryPoint)."';</script>";
$output .= "\n<script type=\"module\"$attributeString>import '".str_replace("'", "\\'", $entryPoint)."';</script>";
}

return $output;
Expand Down
0