Skip to content

Commit 381b782

Browse files
authored
Merge pull request #16 from 8fold/fix-title
fix: Page title passed directly to Document
2 parents 84d1172 + 2bf5a17 commit 381b782

File tree

3 files changed

+34
-9
lines changed

3 files changed

+34
-9
lines changed

src/Document.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ class Document implements Stringable
2020
private array $body = [];
2121

2222
public static function create(
23-
string $title,
23+
string|Stringable $title,
2424
string $lang = 'en',
2525
string $charset = 'utf-8'
2626
): Document {
2727
return new static($title, $lang, $charset);
2828
}
2929

3030
final private function __construct(
31-
private string $title,
31+
private string|Stringable $title,
3232
private string $lang,
3333
private string $charset
3434
) {
@@ -48,10 +48,18 @@ public function body(string|Stringable ...$content): Document
4848

4949
public function __toString(): string
5050
{
51+
$pageTitle = $this->title();
52+
if (
53+
is_string($pageTitle) and
54+
str_starts_with($pageTitle, '<title>') === false
55+
) {
56+
$pageTitle = Element::title($this->title());
57+
}
58+
5159
$doctype = '<!doctype html>' . "\n";
5260
$html = (string) Element::html(
5361
Element::head(
54-
Element::title($this->title()),
62+
$pageTitle,
5563
Element::meta()->omitEndTag()->props($this->charset()),
5664
...$this->headContent()
5765
),
@@ -60,7 +68,7 @@ public function __toString(): string
6068
return $doctype . $html;
6169
}
6270

63-
private function title(): string
71+
private function title(): string|Stringable
6472
{
6573
return $this->title;
6674
}

tests/DocumentBaselineTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,30 @@
99

1010
use Eightfold\HTMLBuilder\Element;
1111

12+
use Eightfold\HTMLBuilder\Components\PageTitle;
13+
1214
class DocumentBaselineTest extends TestCase
1315
{
16+
/**
17+
* @test
18+
*/
19+
public function can_use_page_title(): 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></body></html>
24+
html;
25+
26+
$result = (string) Document::create(
27+
PageTitle::create(['Second', 'First'])
28+
);
29+
30+
$this->assertSame(
31+
$expected,
32+
$result
33+
);
34+
}
35+
1436
/**
1537
* @test
1638
*/

tests/Forms/SelectTest.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@
55

66
use PHPUnit\Framework\TestCase;
77

8-
// use Eightfold\HTMLBuilder\Tests\Extensions\ElementExtension;
9-
10-
// use Eightfold\XMLBuilder\Comment;
11-
12-
// use Eightfold\HTMLBuilder\Document;
138
use Eightfold\HTMLBuilder\Forms\Select;
149

1510
class SelectTest extends TestCase

0 commit comments

Comments
 (0)