From 2bf5a179270717a9c24509370e33925e5ad5f302 Mon Sep 17 00:00:00 2001 From: Josh Bruce Date: Fri, 21 Apr 2023 19:33:01 -0400 Subject: [PATCH] fix: Page title passed directly to Document --- src/Document.php | 16 ++++++++++++---- tests/DocumentBaselineTest.php | 22 ++++++++++++++++++++++ tests/Forms/SelectTest.php | 5 ----- 3 files changed, 34 insertions(+), 9 deletions(-) diff --git a/src/Document.php b/src/Document.php index c62201d..794cfb5 100644 --- a/src/Document.php +++ b/src/Document.php @@ -20,7 +20,7 @@ class Document implements Stringable private array $body = []; public static function create( - string $title, + string|Stringable $title, string $lang = 'en', string $charset = 'utf-8' ): Document { @@ -28,7 +28,7 @@ public static function create( } final private function __construct( - private string $title, + private string|Stringable $title, private string $lang, private string $charset ) { @@ -48,10 +48,18 @@ public function body(string|Stringable ...$content): Document public function __toString(): string { + $pageTitle = $this->title(); + if ( + is_string($pageTitle) and + str_starts_with($pageTitle, '') === false + ) { + $pageTitle = Element::title($this->title()); + } + $doctype = '<!doctype html>' . "\n"; $html = (string) Element::html( Element::head( - Element::title($this->title()), + $pageTitle, Element::meta()->omitEndTag()->props($this->charset()), ...$this->headContent() ), @@ -60,7 +68,7 @@ public function __toString(): string return $doctype . $html; } - private function title(): string + private function title(): string|Stringable { return $this->title; } diff --git a/tests/DocumentBaselineTest.php b/tests/DocumentBaselineTest.php index ccb0d7e..eb36d5a 100644 --- a/tests/DocumentBaselineTest.php +++ b/tests/DocumentBaselineTest.php @@ -9,8 +9,30 @@ use Eightfold\HTMLBuilder\Element; +use Eightfold\HTMLBuilder\Components\PageTitle; + class DocumentBaselineTest extends TestCase { + /** + * @test + */ + public function can_use_page_title(): void // phpcs:ignore + { + $expected = <<<html + <!doctype html> + <html lang="en"><head><title>Second | First + html; + + $result = (string) Document::create( + PageTitle::create(['Second', 'First']) + ); + + $this->assertSame( + $expected, + $result + ); + } + /** * @test */ diff --git a/tests/Forms/SelectTest.php b/tests/Forms/SelectTest.php index ea22a14..a826b69 100644 --- a/tests/Forms/SelectTest.php +++ b/tests/Forms/SelectTest.php @@ -5,11 +5,6 @@ use PHPUnit\Framework\TestCase; -// use Eightfold\HTMLBuilder\Tests\Extensions\ElementExtension; - -// use Eightfold\XMLBuilder\Comment; - -// use Eightfold\HTMLBuilder\Document; use Eightfold\HTMLBuilder\Forms\Select; class SelectTest extends TestCase