From 47d22ee10ec951d56b86486d48ad61fa0ac1a60a Mon Sep 17 00:00:00 2001 From: Arnaud RITTI Date: Wed, 12 Jan 2022 17:24:26 +0100 Subject: [PATCH 1/3] Fix missing integrity hash on preload --- src/Asset/TagRenderer.php | 4 +-- .../PreLoadAssetsEventListener.php | 26 +++++++++++++------ 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/Asset/TagRenderer.php b/src/Asset/TagRenderer.php index d84617eb..6341fcec 100644 --- a/src/Asset/TagRenderer.php +++ b/src/Asset/TagRenderer.php @@ -91,7 +91,7 @@ public function renderWebpackScriptTags(string $entryName, string $packageName = $this->convertArrayToAttributes($attributes) ); - $this->renderedFiles['scripts'][] = $attributes['src']; + $this->renderedFiles['scripts'][] = $attributes; } return implode('', $scriptTags); @@ -129,7 +129,7 @@ public function renderWebpackLinkTags(string $entryName, string $packageName = n $this->convertArrayToAttributes($attributes) ); - $this->renderedFiles['styles'][] = $attributes['href']; + $this->renderedFiles['styles'][] = $attributes; } return implode('', $scriptTags); diff --git a/src/EventListener/PreLoadAssetsEventListener.php b/src/EventListener/PreLoadAssetsEventListener.php index 442440f2..3733edbf 100644 --- a/src/EventListener/PreLoadAssetsEventListener.php +++ b/src/EventListener/PreLoadAssetsEventListener.php @@ -53,21 +53,31 @@ class_exists(GenericLinkProvider::class) ? new GenericLinkProvider() : new FigGe $defaultAttributes = $this->tagRenderer->getDefaultAttributes(); $crossOrigin = $defaultAttributes['crossorigin'] ?? false; - foreach ($this->tagRenderer->getRenderedScripts() as $href) { - $link = ($this->createLink('preload', $href))->withAttribute('as', 'script'); + foreach ($this->tagRenderer->getRenderedScripts() as $attributes) { + $attributes = array_merge($defaultAttributes, $attributes); - if (false !== $crossOrigin) { - $link = $link->withAttribute('crossorigin', $crossOrigin); + $link = ($this->createLink('preload', $attributes["src"]))->withAttribute('as', 'script'); + + if (!empty($attributes['crossorigin']) && false !== $attributes['crossorigin']) { + $link = $link->withAttribute('crossorigin', $attributes['crossorigin']); + } + if (!empty($attributes['integrity'])) { + $link = $link->withAttribute('integrity', $attributes['integrity']); } $linkProvider = $linkProvider->withLink($link); } - foreach ($this->tagRenderer->getRenderedStyles() as $href) { - $link = ($this->createLink('preload', $href))->withAttribute('as', 'style'); + foreach ($this->tagRenderer->getRenderedStyles() as $attributes) { + $attributes = array_merge($defaultAttributes, $attributes); - if (false !== $crossOrigin) { - $link = $link->withAttribute('crossorigin', $crossOrigin); + $link = ($this->createLink('preload', $attributes["href"]))->withAttribute('as', 'style'); + + if (!empty($attributes['crossorigin']) && false !== $attributes['crossorigin']) { + $link = $link->withAttribute('crossorigin', $attributes['crossorigin']); + } + if (!empty($attributes['integrity'])) { + $link = $link->withAttribute('integrity', $attributes['integrity']); } $linkProvider = $linkProvider->withLink($link); From d642586332ef34f44edf73f7edc0659fa6e60b01 Mon Sep 17 00:00:00 2001 From: Arnaud RITTI Date: Wed, 12 Jan 2022 17:51:50 +0100 Subject: [PATCH 2/3] Update tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Update test case to be cohérent with the case --- .../PreLoadAssetsEventListener.php | 4 ++-- tests/Asset/TagRendererTest.php | 16 ++++++++++++++-- .../PreLoadAssetsEventListenerTest.php | 19 ++++++++++++++++--- 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src/EventListener/PreLoadAssetsEventListener.php b/src/EventListener/PreLoadAssetsEventListener.php index 3733edbf..dd87cd62 100644 --- a/src/EventListener/PreLoadAssetsEventListener.php +++ b/src/EventListener/PreLoadAssetsEventListener.php @@ -56,7 +56,7 @@ class_exists(GenericLinkProvider::class) ? new GenericLinkProvider() : new FigGe foreach ($this->tagRenderer->getRenderedScripts() as $attributes) { $attributes = array_merge($defaultAttributes, $attributes); - $link = ($this->createLink('preload', $attributes["src"]))->withAttribute('as', 'script'); + $link = ($this->createLink('preload', $attributes['src']))->withAttribute('as', 'script'); if (!empty($attributes['crossorigin']) && false !== $attributes['crossorigin']) { $link = $link->withAttribute('crossorigin', $attributes['crossorigin']); @@ -71,7 +71,7 @@ class_exists(GenericLinkProvider::class) ? new GenericLinkProvider() : new FigGe foreach ($this->tagRenderer->getRenderedStyles() as $attributes) { $attributes = array_merge($defaultAttributes, $attributes); - $link = ($this->createLink('preload', $attributes["href"]))->withAttribute('as', 'style'); + $link = ($this->createLink('preload', $attributes['href']))->withAttribute('as', 'style'); if (!empty($attributes['crossorigin']) && false !== $attributes['crossorigin']) { $link = $link->withAttribute('crossorigin', $attributes['crossorigin']); diff --git a/tests/Asset/TagRendererTest.php b/tests/Asset/TagRendererTest.php index 6b57ac51..5d949aa6 100644 --- a/tests/Asset/TagRendererTest.php +++ b/tests/Asset/TagRendererTest.php @@ -296,8 +296,20 @@ public function testGetRenderedFilesAndReset() $renderer->renderWebpackScriptTags('my_entry'); $renderer->renderWebpackLinkTags('my_entry'); - $this->assertSame(['http://localhost:8080/build/file1.js', 'http://localhost:8080/build/file2.js'], $renderer->getRenderedScripts()); - $this->assertSame(['http://localhost:8080/build/file1.css'], $renderer->getRenderedStyles()); + $this->assertSame([ + [ + 'src' => 'http://localhost:8080/build/file1.js', + ], + [ + 'src' => 'http://localhost:8080/build/file2.js', + ], + ], $renderer->getRenderedScripts()); + $this->assertSame([ + [ + 'rel' => 'stylesheet', + 'href' => 'http://localhost:8080/build/file1.css', + ], + ], $renderer->getRenderedStyles()); $renderer->reset(); $this->assertEmpty($renderer->getRenderedScripts()); diff --git a/tests/EventListener/PreLoadAssetsEventListenerTest.php b/tests/EventListener/PreLoadAssetsEventListenerTest.php index 696bb607..c7cc7a97 100644 --- a/tests/EventListener/PreLoadAssetsEventListenerTest.php +++ b/tests/EventListener/PreLoadAssetsEventListenerTest.php @@ -28,8 +28,17 @@ public function testItPreloadsAssets() { $tagRenderer = $this->createMock(TagRenderer::class); $tagRenderer->expects($this->once())->method('getDefaultAttributes')->willReturn(['crossorigin' => 'anonymous']); - $tagRenderer->expects($this->once())->method('getRenderedScripts')->willReturn(['/file1.js']); - $tagRenderer->expects($this->once())->method('getRenderedStyles')->willReturn(['/css/file1.css']); + $tagRenderer->expects($this->once())->method('getRenderedScripts')->willReturn([ + [ + 'src' => '/file1.js', + ], + ]); + $tagRenderer->expects($this->once())->method('getRenderedStyles')->willReturn([ + [ + 'rel' => 'stylesheet', + 'href' => '/css/file1.css', + ], + ]); $request = new Request(); $response = new Response(); @@ -58,7 +67,11 @@ public function testItReusesExistingLinkProvider() { $tagRenderer = $this->createMock(TagRenderer::class); $tagRenderer->expects($this->once())->method('getDefaultAttributes')->willReturn(['crossorigin' => 'anonymous']); - $tagRenderer->expects($this->once())->method('getRenderedScripts')->willReturn(['/file1.js']); + $tagRenderer->expects($this->once())->method('getRenderedScripts')->willReturn([ + [ + 'src' => '/file1.js', + ], + ]); $tagRenderer->expects($this->once())->method('getRenderedStyles')->willReturn([]); $request = new Request(); From 84117124d15efcecde81113ded9c067979035606 Mon Sep 17 00:00:00 2001 From: Arnaud RITTI Date: Tue, 29 Mar 2022 12:47:06 +0200 Subject: [PATCH 3/3] Fix BC break --- src/Asset/TagRenderer.php | 23 +++++++++++++++---- .../PreLoadAssetsEventListener.php | 4 ++-- tests/Asset/TagRendererTest.php | 10 ++++++-- 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/src/Asset/TagRenderer.php b/src/Asset/TagRenderer.php index 6341fcec..d11aa995 100644 --- a/src/Asset/TagRenderer.php +++ b/src/Asset/TagRenderer.php @@ -28,6 +28,7 @@ class TagRenderer implements ResetInterface private $eventDispatcher; private $renderedFiles = []; + private $renderedFilesWithAttributes = []; public function __construct( $entrypointLookupCollection, @@ -60,7 +61,7 @@ public function __construct( $this->reset(); } - public function renderWebpackScriptTags(string $entryName, string $packageName = null, string $entrypointName = null, array $extraAttributes = []): string + public function renderWebpackScriptTags(string $entryName, string $packageName = null, string $entrypointName = null, array $extraAttributes = [], bool $includeAttributes = false): string { $entrypointName = $entrypointName ?: '_default'; $scriptTags = []; @@ -91,13 +92,14 @@ public function renderWebpackScriptTags(string $entryName, string $packageName = $this->convertArrayToAttributes($attributes) ); - $this->renderedFiles['scripts'][] = $attributes; + $this->renderedFiles['scripts'][] = $attributes["src"]; + $this->renderedFilesWithAttributes['scripts'][] = $attributes; } return implode('', $scriptTags); } - public function renderWebpackLinkTags(string $entryName, string $packageName = null, string $entrypointName = null, array $extraAttributes = []): string + public function renderWebpackLinkTags(string $entryName, string $packageName = null, string $entrypointName = null, array $extraAttributes = [], bool $includeAttributes = false): string { $entrypointName = $entrypointName ?: '_default'; $scriptTags = []; @@ -129,7 +131,8 @@ public function renderWebpackLinkTags(string $entryName, string $packageName = n $this->convertArrayToAttributes($attributes) ); - $this->renderedFiles['styles'][] = $attributes; + $this->renderedFiles['styles'][] = $attributes["href"]; + $this->renderedFilesWithAttributes['styles'][] = $attributes; } return implode('', $scriptTags); @@ -145,6 +148,16 @@ public function getRenderedStyles(): array return $this->renderedFiles['styles']; } + public function getRenderedScriptsWithAttributes(): array + { + return $this->renderedFilesWithAttributes['scripts']; + } + + public function getRenderedStylesWithAttributes(): array + { + return $this->renderedFilesWithAttributes['styles']; + } + public function getDefaultAttributes(): array { return $this->defaultAttributes; @@ -152,7 +165,7 @@ public function getDefaultAttributes(): array public function reset() { - $this->renderedFiles = [ + $this->renderedFiles = $this->renderedFilesWithAttributes = [ 'scripts' => [], 'styles' => [], ]; diff --git a/src/EventListener/PreLoadAssetsEventListener.php b/src/EventListener/PreLoadAssetsEventListener.php index dd87cd62..55473367 100644 --- a/src/EventListener/PreLoadAssetsEventListener.php +++ b/src/EventListener/PreLoadAssetsEventListener.php @@ -53,7 +53,7 @@ class_exists(GenericLinkProvider::class) ? new GenericLinkProvider() : new FigGe $defaultAttributes = $this->tagRenderer->getDefaultAttributes(); $crossOrigin = $defaultAttributes['crossorigin'] ?? false; - foreach ($this->tagRenderer->getRenderedScripts() as $attributes) { + foreach ($this->tagRenderer->getRenderedScriptsWithAttributes() as $attributes) { $attributes = array_merge($defaultAttributes, $attributes); $link = ($this->createLink('preload', $attributes['src']))->withAttribute('as', 'script'); @@ -68,7 +68,7 @@ class_exists(GenericLinkProvider::class) ? new GenericLinkProvider() : new FigGe $linkProvider = $linkProvider->withLink($link); } - foreach ($this->tagRenderer->getRenderedStyles() as $attributes) { + foreach ($this->tagRenderer->getRenderedStylesWithAttributes() as $attributes) { $attributes = array_merge($defaultAttributes, $attributes); $link = ($this->createLink('preload', $attributes['href']))->withAttribute('as', 'style'); diff --git a/tests/Asset/TagRendererTest.php b/tests/Asset/TagRendererTest.php index 5d949aa6..65955c9f 100644 --- a/tests/Asset/TagRendererTest.php +++ b/tests/Asset/TagRendererTest.php @@ -296,6 +296,9 @@ public function testGetRenderedFilesAndReset() $renderer->renderWebpackScriptTags('my_entry'); $renderer->renderWebpackLinkTags('my_entry'); + $this->assertSame(['http://localhost:8080/build/file1.js', 'http://localhost:8080/build/file2.js'], $renderer->getRenderedScripts()); + $this->assertSame(['http://localhost:8080/build/file1.css'], $renderer->getRenderedStyles()); + $this->assertSame([ [ 'src' => 'http://localhost:8080/build/file1.js', @@ -303,16 +306,19 @@ public function testGetRenderedFilesAndReset() [ 'src' => 'http://localhost:8080/build/file2.js', ], - ], $renderer->getRenderedScripts()); + ], $renderer->getRenderedScriptsWithAttributes()); $this->assertSame([ [ 'rel' => 'stylesheet', 'href' => 'http://localhost:8080/build/file1.css', ], - ], $renderer->getRenderedStyles()); + ], $renderer->getRenderedStylesWithAttributes()); + $renderer->reset(); $this->assertEmpty($renderer->getRenderedScripts()); $this->assertEmpty($renderer->getRenderedStyles()); + $this->assertEmpty($renderer->getRenderedScriptsWithAttributes()); + $this->assertEmpty($renderer->getRenderedStylesWithAttributes()); } }