From df6a4189ae62d1ed3ea7fcf03f4d2cc1005c57db Mon Sep 17 00:00:00 2001 From: Brent Roose Date: Fri, 15 Nov 2024 18:32:03 +0100 Subject: [PATCH] feat(view): add raw html element (#738) --- .../View/src/Components/{Form.php => XForm.php} | 2 +- .../View/src/Components/{Input.php => XInput.php} | 2 +- .../View/src/Components/{Submit.php => XSubmit.php} | 2 +- src/Tempest/View/src/Elements/ElementFactory.php | 11 +++++++---- src/Tempest/View/src/Elements/RawElement.php | 2 +- tests/Integration/View/TempestViewRendererTest.php | 7 +++++++ tests/Integration/View/ViewComponentDiscoveryTest.php | 4 ++-- 7 files changed, 20 insertions(+), 10 deletions(-) rename src/Tempest/View/src/Components/{Form.php => XForm.php} (92%) rename src/Tempest/View/src/Components/{Input.php => XInput.php} (97%) rename src/Tempest/View/src/Components/{Submit.php => XSubmit.php} (89%) diff --git a/src/Tempest/View/src/Components/Form.php b/src/Tempest/View/src/Components/XForm.php similarity index 92% rename from src/Tempest/View/src/Components/Form.php rename to src/Tempest/View/src/Components/XForm.php index 24ab6bb7c..73a6cb93f 100644 --- a/src/Tempest/View/src/Components/Form.php +++ b/src/Tempest/View/src/Components/XForm.php @@ -7,7 +7,7 @@ use Tempest\View\Elements\ViewComponentElement; use Tempest\View\ViewComponent; -final readonly class Form implements ViewComponent +final readonly class XForm implements ViewComponent { public static function getName(): string { diff --git a/src/Tempest/View/src/Components/Input.php b/src/Tempest/View/src/Components/XInput.php similarity index 97% rename from src/Tempest/View/src/Components/Input.php rename to src/Tempest/View/src/Components/XInput.php index 75b9ef9f6..5340a16ac 100644 --- a/src/Tempest/View/src/Components/Input.php +++ b/src/Tempest/View/src/Components/XInput.php @@ -9,7 +9,7 @@ use Tempest\View\Elements\ViewComponentElement; use Tempest\View\ViewComponent; -final readonly class Input implements ViewComponent +final readonly class XInput implements ViewComponent { public function __construct( private Session $session, diff --git a/src/Tempest/View/src/Components/Submit.php b/src/Tempest/View/src/Components/XSubmit.php similarity index 89% rename from src/Tempest/View/src/Components/Submit.php rename to src/Tempest/View/src/Components/XSubmit.php index a927757c6..2a4f53df4 100644 --- a/src/Tempest/View/src/Components/Submit.php +++ b/src/Tempest/View/src/Components/XSubmit.php @@ -7,7 +7,7 @@ use Tempest\View\Elements\ViewComponentElement; use Tempest\View\ViewComponent; -final readonly class Submit implements ViewComponent +final readonly class XSubmit implements ViewComponent { public static function getName(): string { diff --git a/src/Tempest/View/src/Elements/ElementFactory.php b/src/Tempest/View/src/Elements/ElementFactory.php index 4d30e4ee0..5aaccebd5 100644 --- a/src/Tempest/View/src/Elements/ElementFactory.php +++ b/src/Tempest/View/src/Elements/ElementFactory.php @@ -63,8 +63,11 @@ private function makeElement(DOMNode $node, ?Element $parent): ?Element if (! $node instanceof DOMElement || $node->tagName === 'pre' - || $node->tagName === 'code') { + || $node->tagName === 'code' + || $node->tagName === 'x-raw' + ) { $content = ''; + foreach ($node->childNodes as $child) { $content .= $node->ownerDocument->saveHTML($child); } @@ -82,9 +85,9 @@ private function makeElement(DOMNode $node, ?Element $parent): ?Element } $element = new ViewComponentElement( - $this->compiler, - $viewComponentClass, - $attributes, + compiler: $this->compiler, + viewComponent: $viewComponentClass, + attributes: $attributes, ); } elseif ($node->tagName === 'x-slot') { $element = new SlotElement( diff --git a/src/Tempest/View/src/Elements/RawElement.php b/src/Tempest/View/src/Elements/RawElement.php index c2b2ef923..b01f0caea 100644 --- a/src/Tempest/View/src/Elements/RawElement.php +++ b/src/Tempest/View/src/Elements/RawElement.php @@ -21,7 +21,7 @@ public function __construct( public function compile(): string { - if ($this->tag === null) { + if ($this->tag === null || $this->tag === 'x-raw') { return $this->content; } diff --git a/tests/Integration/View/TempestViewRendererTest.php b/tests/Integration/View/TempestViewRendererTest.php index 722912f42..300b29b2e 100644 --- a/tests/Integration/View/TempestViewRendererTest.php +++ b/tests/Integration/View/TempestViewRendererTest.php @@ -416,4 +416,11 @@ public function test_view_component_with_multiple_attributes(): void $html = $this->render(view('')); $this->assertStringEqualsStringIgnoringLineEndings($expected, $html); } + + public function test_raw_component(): void + { + $html = $this->render('
{{ $bar }}
'); + + $this->assertStringEqualsStringIgnoringLineEndings('
{{ $bar }}
', $html); + } } diff --git a/tests/Integration/View/ViewComponentDiscoveryTest.php b/tests/Integration/View/ViewComponentDiscoveryTest.php index 5bdc2fd35..3494a1777 100644 --- a/tests/Integration/View/ViewComponentDiscoveryTest.php +++ b/tests/Integration/View/ViewComponentDiscoveryTest.php @@ -4,7 +4,7 @@ namespace Tests\Tempest\Integration\View; -use Tempest\View\Components\Input; +use Tempest\View\Components\XInput; use Tempest\View\Exceptions\DuplicateViewComponent; use Tempest\View\ViewComponentDiscovery; use Tests\Tempest\Integration\FrameworkIntegrationTestCase; @@ -22,7 +22,7 @@ public function test_duplicates(): void $discovery->discoverPath(__DIR__ . '/duplicateComponent.view.php'); } catch (DuplicateViewComponent $duplicateViewComponent) { $this->assertStringContainsString(__DIR__ . '/duplicateComponent.view.php', $duplicateViewComponent->getMessage()); - $this->assertStringContainsString(Input::class, $duplicateViewComponent->getMessage()); + $this->assertStringContainsString(XInput::class, $duplicateViewComponent->getMessage()); $this->assertStringContainsString('x-input', $duplicateViewComponent->getMessage()); } }