Skip to content

Commit 8b95679

Browse files
authored
fix(view): support doc comment elements (#816)
1 parent f34c2ba commit 8b95679

File tree

3 files changed

+41
-18
lines changed

3 files changed

+41
-18
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tempest\View\Elements;
6+
7+
use Tempest\View\Element;
8+
9+
final class CommentElement implements Element
10+
{
11+
use IsElement;
12+
13+
public function __construct(
14+
private readonly string $content,
15+
) {
16+
}
17+
18+
public function compile(): string
19+
{
20+
return sprintf('<!--%s-->', $this->content);
21+
}
22+
}

src/Tempest/View/src/Elements/ElementFactory.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Tempest\View\Elements;
66

7+
use Dom\Comment;
78
use Dom\Element as DomElement;
89
use Dom\Node;
910
use Dom\Text;
@@ -12,7 +13,6 @@
1213
use Tempest\View\Renderers\TempestViewCompiler;
1314
use Tempest\View\ViewComponent;
1415
use Tempest\View\ViewConfig;
15-
use const XML_ELEMENT_NODE;
1616
use function Tempest\Support\str;
1717

1818
final class ElementFactory
@@ -52,12 +52,14 @@ private function makeElement(Node $node, ?Element $parent): ?Element
5252
);
5353
}
5454

55-
$tagName = null;
56-
57-
if ($node->nodeType == XML_ELEMENT_NODE) {
58-
$tagName = strtolower($node->tagName);
55+
if ($node instanceof Comment) {
56+
return new CommentElement(
57+
content: $node->textContent,
58+
);
5959
}
6060

61+
$tagName = strtolower($node->tagName);
62+
6163
$attributes = [];
6264

6365
/** @var \Dom\Attr $attribute */

tests/Integration/View/TempestViewRendererTest.php

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -421,22 +421,21 @@ public function test_view_component_with_multiple_attributes(): void
421421

422422
public function test_slot_with_comment(): void
423423
{
424-
$this->assertSame(
424+
$this->assertStringEqualsStringIgnoringLineEndings(
425425
<<<'HTML'
426-
<div class="base">
427-
428-
429-
Test
430-
431-
</div>
432-
HTML,
433-
$this->render(
434-
<<<'HTML'
435-
<x-base-layout>
436-
<!-- example of comment -->
426+
<div class="base"><!-- example of comment -->
427+
437428
Test
438-
</x-base-layout>
429+
430+
</div>
439431
HTML,
432+
$this->render(
433+
<<<'HTML'
434+
<x-base-layout>
435+
<!-- example of comment -->
436+
Test
437+
</x-base-layout>
438+
HTML,
440439
),
441440
);
442441
}

0 commit comments

Comments
 (0)