Skip to content

Commit

Permalink
bug #113 Fixing null and false attributes (weaverryan)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
weaverryan committed Feb 17, 2021
2 parents ed807e7 + 5dc7e3c commit 14a222f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Asset/TagRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,18 @@ private function getEntrypointLookup(string $buildName): EntrypointLookupInterfa

private function convertArrayToAttributes(array $attributesMap): string
{
// remove attributes set specifically to false
$attributesMap = array_filter($attributesMap, function($value) {
return $value !== false;
});

return implode(' ', array_map(
function ($key, $value) {
// allows for things like defer: true to only render "defer"
if ($value === true) {
if ($value === true || $value === null) {
return $key;
}

return sprintf('%s="%s"', $key, htmlentities($value));
},
array_keys($attributesMap),
Expand Down
29 changes: 29 additions & 0 deletions tests/Asset/TagRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,35 @@ public function testRenderScriptTagsWithExtraAttributes()
);
}

public function testRenderScriptTagsWithFalseyAttributes()
{
$entrypointLookup = $this->createMock(EntrypointLookupInterface::class);
$entrypointLookup->expects($this->once())
->method('getJavaScriptFiles')
->willReturn(['/build/file1.js']);
$entrypointCollection = $this->createMock(EntrypointLookupCollection::class);
$entrypointCollection->expects($this->once())
->method('getEntrypointLookup')
->willReturn($entrypointLookup);

$packages = $this->createMock(Packages::class);
$packages->expects($this->once())
->method('getUrl')
->willReturnCallback(function ($path) {
return 'http://localhost:8080' . $path;
});
$renderer = new TagRenderer($entrypointCollection, $packages, [
'defer' => false, // false disables the attribute
'async' => null, // null allows the attribute
]);

$output = $renderer->renderWebpackScriptTags('my_entry');
$this->assertStringContainsString(
'<script src="http://localhost:8080/build/file1.js" async></script>',
$output
);
}

public function testRenderScriptTagsDispatchesAnEvent()
{
$entrypointLookup = $this->createMock(EntrypointLookupInterface::class);
Expand Down

0 comments on commit 14a222f

Please sign in to comment.