Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@

class Document implements Stringable
{
/**
* @var array<string>
*/
private array $htmlProps = [];

/**
* @var array<string|Stringable>
*/
Expand Down Expand Up @@ -39,6 +44,12 @@ final private function __construct(
) {
}

public function htmlProps(string ...$props): Document
{
$this->htmlProps = $props;
return $this;
}

public function head(string|Stringable ...$content): Document
{
$this->head = $content;
Expand Down Expand Up @@ -75,7 +86,7 @@ public function toString(): string
...$this->headContent()
),
Element::body(...$this->bodyContent())->props(...$this->bodyProps)
)->props($this->lang());
)->props($this->lang(), ...$this->htmlProps);
return $doctype . $html;
}

Expand Down
37 changes: 37 additions & 0 deletions tests/DocumentBaselineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,43 @@

class DocumentBaselineTest extends TestCase
{
#[Test]
public function adding_lang_to_html_props_overrides_create(): void // phpcs:ignore
{
$expected = <<<html
<!doctype html>
<html class="dark-theme" lang="fr"><head><title>Second | First</title><meta charset="utf-8"></head><body></body></html>
html;

$result = (string) Document::create(
PageTitle::create(['Second', 'First']),
'en-us'
)->htmlProps('class dark-theme', 'lang fr');

$this->assertSame(
$expected,
$result
);
}

#[Test]
public function can_add_properties_to_html_element(): void // phpcs:ignore
{
$expected = <<<html
<!doctype html>
<html class="dark-theme" lang="en"><head><title>Second | First</title><meta charset="utf-8"></head><body></body></html>
html;

$result = (string) Document::create(
PageTitle::create(['Second', 'First'])
)->htmlProps('class dark-theme');

$this->assertSame(
$expected,
$result
);
}

#[Test]
public function can_add_properties_to_body_element(): void // phpcs:ignore
{
Expand Down
Loading