Skip to content

Commit

Permalink
Merge pull request #188 from skyrpex/patch-1
Browse files Browse the repository at this point in the history
Treat boolean attributes as HTML properties
  • Loading branch information
Matt Lantz committed Dec 13, 2016
2 parents abec449 + dc15cf8 commit 8956f00
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/HtmlBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) . '"';
Expand Down
13 changes: 12 additions & 1 deletion tests/HtmlBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('<p>' . PHP_EOL . 'Lorem ipsum dolor sit amet.' . PHP_EOL . '</p>' . PHP_EOL, $result1);
Expand Down Expand Up @@ -115,4 +115,15 @@ public function testMailto()
$this->assertEquals('<a href="mailto:person@example.com" class="example-link">&lt;span&gt;First Name Last&lt;/span&gt;</a>', $result1);
$this->assertEquals('<a href="mailto:person@example.com" class="example-link"><span>First Name Last</span></a>', $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));
}
}

0 comments on commit 8956f00

Please sign in to comment.