Skip to content

Commit

Permalink
fix: Page title passed directly to Document
Browse files Browse the repository at this point in the history
  • Loading branch information
joshbruce committed Apr 21, 2023
1 parent 84d1172 commit 2bf5a17
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
16 changes: 12 additions & 4 deletions src/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ class Document implements Stringable
private array $body = [];

public static function create(
string $title,
string|Stringable $title,
string $lang = 'en',
string $charset = 'utf-8'
): Document {
return new static($title, $lang, $charset);
}

final private function __construct(
private string $title,
private string|Stringable $title,
private string $lang,
private string $charset
) {
Expand All @@ -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, '<title>') === 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()
),
Expand All @@ -60,7 +68,7 @@ public function __toString(): string
return $doctype . $html;
}

private function title(): string
private function title(): string|Stringable
{
return $this->title;
}
Expand Down
22 changes: 22 additions & 0 deletions tests/DocumentBaselineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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</title><meta charset="utf-8"></head><body></body></html>
html;

$result = (string) Document::create(
PageTitle::create(['Second', 'First'])
);

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

/**
* @test
*/
Expand Down
5 changes: 0 additions & 5 deletions tests/Forms/SelectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2bf5a17

Please sign in to comment.