From 1add9b421ac013612595cf18581d7fd15e7c159f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cristian=20Pallar=C3=A9s?= Date: Thu, 28 Apr 2016 14:06:22 +0200 Subject: [PATCH 1/2] Treat boolean attributes as HTML properties --- src/HtmlBuilder.php | 5 +++++ 1 file changed, 5 insertions(+) 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) . '"'; From dc15cf8463afb980049bc9aba5b0a7856435a746 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cristian=20Pallar=C3=A9s?= Date: Tue, 16 Aug 2016 09:24:57 +0200 Subject: [PATCH 2/2] Add test for boolean attributes --- tests/HtmlBuilderTest.php | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) 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)); + } }