Skip to content

Commit 14a222f

Browse files
committed
bug #113 Fixing null and false attributes (weaverryan)
This PR was merged into the main branch. Discussion ---------- Fixing null and false attributes Hi! Suppose this config: ```yaml webpack_encore: script_attributes: defer: false async: null ``` This PR changes what this outputs: ```html <!-- before --> <script src="..." defer="" async=""></script> <!-- after --> <script src="..." async></script> ``` This was an oversight when I built the feature. With this PR, `false` correctly means "do not show this attribute". `null` is a bit trickier: I chose to make this mean "show the attribute, but with value (e.g. `<script defer>`). Any other values are rendered normally. Cheers! Commits ------- 5dc7e3c Fixing null and false attributes
2 parents ed807e7 + 5dc7e3c commit 14a222f

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/Asset/TagRenderer.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,12 +177,18 @@ private function getEntrypointLookup(string $buildName): EntrypointLookupInterfa
177177

178178
private function convertArrayToAttributes(array $attributesMap): string
179179
{
180+
// remove attributes set specifically to false
181+
$attributesMap = array_filter($attributesMap, function($value) {
182+
return $value !== false;
183+
});
184+
180185
return implode(' ', array_map(
181186
function ($key, $value) {
182187
// allows for things like defer: true to only render "defer"
183-
if ($value === true) {
188+
if ($value === true || $value === null) {
184189
return $key;
185190
}
191+
186192
return sprintf('%s="%s"', $key, htmlentities($value));
187193
},
188194
array_keys($attributesMap),

tests/Asset/TagRendererTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,35 @@ public function testRenderScriptTagsWithExtraAttributes()
8585
);
8686
}
8787

88+
public function testRenderScriptTagsWithFalseyAttributes()
89+
{
90+
$entrypointLookup = $this->createMock(EntrypointLookupInterface::class);
91+
$entrypointLookup->expects($this->once())
92+
->method('getJavaScriptFiles')
93+
->willReturn(['/build/file1.js']);
94+
$entrypointCollection = $this->createMock(EntrypointLookupCollection::class);
95+
$entrypointCollection->expects($this->once())
96+
->method('getEntrypointLookup')
97+
->willReturn($entrypointLookup);
98+
99+
$packages = $this->createMock(Packages::class);
100+
$packages->expects($this->once())
101+
->method('getUrl')
102+
->willReturnCallback(function ($path) {
103+
return 'http://localhost:8080' . $path;
104+
});
105+
$renderer = new TagRenderer($entrypointCollection, $packages, [
106+
'defer' => false, // false disables the attribute
107+
'async' => null, // null allows the attribute
108+
]);
109+
110+
$output = $renderer->renderWebpackScriptTags('my_entry');
111+
$this->assertStringContainsString(
112+
'<script src="http://localhost:8080/build/file1.js" async></script>',
113+
$output
114+
);
115+
}
116+
88117
public function testRenderScriptTagsDispatchesAnEvent()
89118
{
90119
$entrypointLookup = $this->createMock(EntrypointLookupInterface::class);

0 commit comments

Comments
 (0)