Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat(view): add raw html element #738

Merged
merged 1 commit into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
11 changes: 7 additions & 4 deletions src/Tempest/View/src/Elements/ElementFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion src/Tempest/View/src/Elements/RawElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
7 changes: 7 additions & 0 deletions tests/Integration/View/TempestViewRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -416,4 +416,11 @@ public function test_view_component_with_multiple_attributes(): void
$html = $this->render(view('<x-view-component-with-multiple-attributes :a="\'a\'" b="b"></x-view-component-with-multiple-attributes>'));
$this->assertStringEqualsStringIgnoringLineEndings($expected, $html);
}

public function test_raw_component(): void
{
$html = $this->render('<x-raw><div :prop="$foo">{{ $bar }}</div></x-raw>');

$this->assertStringEqualsStringIgnoringLineEndings('<div :prop="$foo">{{ $bar }}</div>', $html);
}
}
4 changes: 2 additions & 2 deletions tests/Integration/View/ViewComponentDiscoveryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
}
}
Expand Down
Loading