Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[AssetMapper] Load es-module-shims only if importmap is not supported #58121

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
Aug 30, 2024
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
41 changes: 25 additions & 16 deletions src/Symfony/Component/AssetMapper/ImportMap/ImportMapRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function render(string|array $entryPoint, array $attributes = []): string
$this->addWebLinkPreloads($request, $cssLinks);
}

$scriptAttributes = $this->createAttributesString($attributes);
$scriptAttributes = $attributes || $this->scriptAttributes ? ' '.$this->createAttributesString($attributes) : '';
$importMapJson = json_encode(['imports' => $importMap], \JSON_THROW_ON_ERROR | \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES | \JSON_HEX_TAG);
$output .= <<<HTML

Expand All @@ -114,21 +114,26 @@ public function render(string|array $entryPoint, array $attributes = []): string
}

if ($polyfillPath) {
$url = $this->escapeAttributeValue($polyfillPath);
$polyfillAttributes = $scriptAttributes;
$polyfillAttributes = $attributes + $this->scriptAttributes;

// Add security attributes for the default polyfill hosted on jspm.io
if (self::DEFAULT_ES_MODULE_SHIMS_POLYFILL_URL === $polyfillPath) {
$polyfillAttributes = $this->createAttributesString([
$polyfillAttributes = [
'crossorigin' => 'anonymous',
'integrity' => self::DEFAULT_ES_MODULE_SHIMS_POLYFILL_INTEGRITY,
] + $attributes);
] + $polyfillAttributes;
}

$output .= <<<HTML

<!-- ES Module Shims: Import maps polyfill for modules browsers without import maps support -->
<script async src="$url"$polyfillAttributes></script>
<script async$scriptAttributes>
if (!HTMLScriptElement.supports || !HTMLScriptElement.supports('importmap')) (function () {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I confirmed this works using Browerstack :shipit:

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Soon in Nicolas' bio "GPT+BrowserStack powered JS dev" 😁

const script = document.createElement('script');
script.src = '{$this->escapeAttributeValue($polyfillPath, \ENT_NOQUOTES)}';
script.setAttribute('async', 'async');
{$this->createAttributesString($polyfillAttributes, "script.setAttribute('%s', '%s');", "\n ", \ENT_NOQUOTES)}
document.head.appendChild(script);
})();
</script>
HTML;
}

Expand All @@ -151,12 +156,14 @@ public function render(string|array $entryPoint, array $attributes = []): string
return $output;
}

private function escapeAttributeValue(string $value): string
private function escapeAttributeValue(string $value, int $flags = \ENT_COMPAT | \ENT_SUBSTITUTE): string
{
return htmlspecialchars($value, \ENT_COMPAT | \ENT_SUBSTITUTE, $this->charset);
$value = htmlspecialchars($value, $flags, $this->charset);

return \ENT_NOQUOTES & $flags ? addslashes($value) : $value;
}

private function createAttributesString(array $attributes): string
private function createAttributesString(array $attributes, string $pattern = '%s="%s"', string $glue = ' ', int $flags = \ENT_COMPAT | \ENT_SUBSTITUTE): string
{
$attributeString = '';

Expand All @@ -166,15 +173,17 @@ private function createAttributesString(array $attributes): string
}

foreach ($attributes as $name => $value) {
$attributeString .= ' ';
if ('' !== $attributeString) {
$attributeString .= $glue;
}
if (true === $value) {
$attributeString .= $name;

continue;
$value = $name;
}
$attributeString .= \sprintf('%s="%s"', $name, $this->escapeAttributeValue($value));
$attributeString .= \sprintf($pattern, $this->escapeAttributeValue($name, $flags), $this->escapeAttributeValue($value, $flags));
}

$attributeString = preg_replace('/\b([^ =]++)="\1"/', '\1', $attributeString);

return $attributeString;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function testBasicRender()

$this->assertStringContainsString('<script type="importmap">', $html);
// polyfill is rendered as a normal script tag
$this->assertStringContainsString('<script async src="https://ga.jspm.io/npm:es-module-shims"></script>', $html);
$this->assertStringContainsString("script.src = 'https://ga.jspm.io/npm:es-module-shims';", $html);
// and is hidden from the import map
$this->assertStringNotContainsString('"es-module-shim"', $html);
$this->assertStringContainsString('import \'app\';', $html);
Expand Down Expand Up @@ -120,8 +120,8 @@ public function testDefaultPolyfillUsedIfNotInImportmap()
polyfillImportName: 'es-module-shims',
);
$html = $renderer->render(['app']);
$this->assertStringContainsString('<script async src="https://ga.jspm.io/npm:es-module-shims@', $html);
$this->assertStringContainsString('es-module-shims.js" crossorigin="anonymous" integrity="sha384-', $html);
$this->assertStringContainsString("script.src = 'https://ga.jspm.io/npm:es-module-shims@", $html);
$this->assertStringContainsString("script.setAttribute('crossorigin', 'anonymous');\n script.setAttribute('integrity', 'sha384-", $html);
}

public function testCustomScriptAttributes()
Expand All @@ -132,7 +132,13 @@ public function testCustomScriptAttributes()
]);
$html = $renderer->render([]);
$this->assertStringContainsString('<script type="importmap" something data-turbo-track="reload">', $html);
$this->assertStringContainsString('<script async src="https://polyfillUrl.example" something data-turbo-track="reload"></script>', $html);
$this->assertStringContainsString('<script async something data-turbo-track="reload">', $html);
$this->assertStringContainsString(<<<EOTXT
script.src = 'https://polyfillUrl.example';
script.setAttribute('async', 'async');
script.setAttribute('something', 'something');
script.setAttribute('data-turbo-track', 'reload');
EOTXT, $html);
}

public function testWithEntrypoint()
Expand Down