Skip to content

Commit 8956f00

Browse files
author
Matt Lantz
authored
Merge pull request #188 from skyrpex/patch-1
Treat boolean attributes as HTML properties
2 parents abec449 + dc15cf8 commit 8956f00

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/HtmlBuilder.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,11 @@ protected function attributeElement($key, $value)
447447
if (is_numeric($key)) {
448448
$key = $value;
449449
}
450+
451+
// Treat boolean attributes as HTML properties
452+
if (is_bool($value)) {
453+
return $value ? $key : '';
454+
}
450455

451456
if (! is_null($value)) {
452457
return $key . '="' . e($value) . '"';

tests/HtmlBuilderTest.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function testTag()
5858
$this->htmlBuilder->image('http://example.com/image1'),
5959
$this->htmlBuilder->image('http://example.com/image2'),
6060
];
61-
61+
6262
$result4 = $this->htmlBuilder->tag('div', $content, ['class' => 'row']);
6363

6464
$this->assertEquals('<p>' . PHP_EOL . 'Lorem ipsum dolor sit amet.' . PHP_EOL . '</p>' . PHP_EOL, $result1);
@@ -115,4 +115,15 @@ public function testMailto()
115115
$this->assertEquals('<a href="mailto:[email protected]" class="example-link">&lt;span&gt;First Name Last&lt;/span&gt;</a>', $result1);
116116
$this->assertEquals('<a href="mailto:[email protected]" class="example-link"><span>First Name Last</span></a>', $result2);
117117
}
118+
119+
public function testBooleanAttributes()
120+
{
121+
$result1 = $this->htmlBuilder->attributes(['my-property' => true]);
122+
123+
$result2 = $this->htmlBuilder->attributes(['my-property' => false]);
124+
125+
$this->assertEquals('my-property', trim($result1));
126+
127+
$this->assertEquals('', trim($result2));
128+
}
118129
}

0 commit comments

Comments
 (0)