diff --git a/src/HtmlBuilder.php b/src/HtmlBuilder.php index 7306fc42..1d40f68d 100755 --- a/src/HtmlBuilder.php +++ b/src/HtmlBuilder.php @@ -447,6 +447,11 @@ protected function attributeElement($key, $value) if (is_numeric($key)) { $key = $value; } + + // Treat boolean attributes as HTML properties + if (is_bool($value)) { + return $value ? $key : ''; + } if (! is_null($value)) { return $key . '="' . e($value) . '"'; diff --git a/tests/HtmlBuilderTest.php b/tests/HtmlBuilderTest.php index 44baeb83..1406ad70 100644 --- a/tests/HtmlBuilderTest.php +++ b/tests/HtmlBuilderTest.php @@ -58,7 +58,7 @@ public function testTag() $this->htmlBuilder->image('http://example.com/image1'), $this->htmlBuilder->image('http://example.com/image2'), ]; - + $result4 = $this->htmlBuilder->tag('div', $content, ['class' => 'row']); $this->assertEquals('

' . PHP_EOL . 'Lorem ipsum dolor sit amet.' . PHP_EOL . '

' . PHP_EOL, $result1); @@ -115,4 +115,15 @@ public function testMailto() $this->assertEquals('<span>First Name Last</span>', $result1); $this->assertEquals('First Name Last', $result2); } + + public function testBooleanAttributes() + { + $result1 = $this->htmlBuilder->attributes(['my-property' => true]); + + $result2 = $this->htmlBuilder->attributes(['my-property' => false]); + + $this->assertEquals('my-property', trim($result1)); + + $this->assertEquals('', trim($result2)); + } }