Skip to content

Commit 7564b4a

Browse files
authored
Merge pull request #17 from 8fold/body-props
add: Ability to add properties to body element
2 parents 381b782 + d602b0a commit 7564b4a

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/Document.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ class Document implements Stringable
1919
*/
2020
private array $body = [];
2121

22+
/**
23+
* @var array<string>
24+
*/
25+
private array $bodyProps = [];
26+
2227
public static function create(
2328
string|Stringable $title,
2429
string $lang = 'en',
@@ -46,6 +51,12 @@ public function body(string|Stringable ...$content): Document
4651
return $this;
4752
}
4853

54+
public function bodyProps(string ...$props): Document
55+
{
56+
$this->bodyProps = $props;
57+
return $this;
58+
}
59+
4960
public function __toString(): string
5061
{
5162
$pageTitle = $this->title();
@@ -63,7 +74,7 @@ public function __toString(): string
6374
Element::meta()->omitEndTag()->props($this->charset()),
6475
...$this->headContent()
6576
),
66-
Element::body(...$this->bodyContent())
77+
Element::body(...$this->bodyContent())->props(...$this->bodyProps)
6778
)->props($this->lang());
6879
return $doctype . $html;
6980
}

tests/DocumentBaselineTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,26 @@
1313

1414
class DocumentBaselineTest extends TestCase
1515
{
16+
/**
17+
* @test
18+
*/
19+
public function can_add_properties_to_body_element(): void // phpcs:ignore
20+
{
21+
$expected = <<<html
22+
<!doctype html>
23+
<html lang="en"><head><title>Second | First</title><meta charset="utf-8"></head><body id="hello" class="world" style="color: #000;"></body></html>
24+
html;
25+
26+
$result = (string) Document::create(
27+
PageTitle::create(['Second', 'First'])
28+
)->bodyProps('style color: #000;', 'class world', 'id hello');
29+
30+
$this->assertSame(
31+
$expected,
32+
$result
33+
);
34+
}
35+
1636
/**
1737
* @test
1838
*/

0 commit comments

Comments
 (0)