Skip to content

Commit

Permalink
Remove null and false attributes. Cast true attributes to "true". Closes
Browse files Browse the repository at this point in the history
 #2613, Fixes #2605
  • Loading branch information
jasonvarga committed Oct 13, 2020
1 parent 16865fc commit 76213f4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
11 changes: 8 additions & 3 deletions src/Tags/Concerns/RendersAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,15 @@ protected function renderAttributes($attributes)
{
return collect($attributes)
->map(function ($value, $attribute) {
return $value === true
? $attribute
: sprintf('%s="%s"', $attribute, $value);
if ($value === null || $value === false) {
return;
}

$value = $value === true ? 'true' : $value;

return sprintf('%s="%s"', $attribute, $value);
})
->filter()
->implode(' ');
}

Expand Down
11 changes: 9 additions & 2 deletions tests/Tags/Concerns/RendersAttributesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@ public function it_renders_attributes_from_array()
$output = $this->tag->renderAttributes([
'class' => 'm-0 mb-1',
':name' => 'first_name',
'disabled' => 'true',
'autocomplete' => true,
'dont_render_falses' => false,
'dont_render_nulls' => null,
]);

$this->assertEquals('class="m-0 mb-1" :name="first_name"', $output);
$this->assertEquals('class="m-0 mb-1" :name="first_name" disabled="true" autocomplete="true"', $output);
}

/** @test */
Expand All @@ -40,11 +44,14 @@ public function it_renders_attributes_from_params()
'class' => 'm-0 mb-1',
':name' => 'first_name',
'attr:src' => 'avatar.jpg',
'dont_render_falses' => false,
'dont_render_nulls' => null,
'disabled' => 'true',
'autocomplete' => true,
])
->renderAttributesFromParams();

$this->assertEquals('class="m-0 mb-1" name="Han" src="avatar.jpg" disabled', $output);
$this->assertEquals('class="m-0 mb-1" name="Han" src="avatar.jpg" disabled="true" autocomplete="true"', $output);
}

/** @test */
Expand Down

0 comments on commit 76213f4

Please sign in to comment.