Skip to content

Commit

Permalink
Merge branch 'main' into refactor-http-route-attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
brendt authored Dec 6, 2024
2 parents b93674c + 839cf60 commit 32aba50
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/Tempest/Console/src/GenericConsole.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public function warning(string $line, ?string $symbol = null): self

public function success(string $line, ?string $symbol = null): self
{
$symbol ??= '✔︎';
$symbol ??= '';

$this->writeln("<style=\"bg-dark-green fg-white\"> {$symbol} </style> <style=\"fg-green\">{$line}</style>");

Expand Down
3 changes: 1 addition & 2 deletions src/Tempest/Console/src/Middleware/OverviewMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ private function renderOverview(bool $showHidden = false): void
->toString();

$this->console
->writeln()
->writeln()
->writeln($title);

Expand All @@ -91,7 +90,7 @@ private function renderOverview(bool $showHidden = false): void
$this->console
->unless(
condition: $this->discoveryCache->isValid(),
callback: fn (Console $console) => $console->writeln(PHP_EOL . '<error>Discovery cache invalid. Run discovery:generate to enable discovery caching.</error>'),
callback: fn (Console $console) => $console->writeln()->error('Discovery cache invalid. Run discovery:generate to enable discovery caching.'),
);
}
}
22 changes: 22 additions & 0 deletions src/Tempest/View/src/Elements/CommentElement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Tempest\View\Elements;

use Tempest\View\Element;

final class CommentElement implements Element
{
use IsElement;

public function __construct(
private readonly string $content,
) {
}

public function compile(): string
{
return sprintf('<!--%s-->', $this->content);
}
}
9 changes: 8 additions & 1 deletion src/Tempest/View/src/Elements/ElementFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Tempest\View\Elements;

use Dom\Comment;
use Dom\Element as DomElement;
use Dom\Node;
use Dom\Text;
Expand Down Expand Up @@ -51,7 +52,13 @@ private function makeElement(Node $node, ?Element $parent): ?Element
);
}

$tagName = $node->tagName ? strtolower($node->tagName) : null;
if ($node instanceof Comment) {
return new CommentElement(
content: $node->textContent,
);
}

$tagName = strtolower($node->tagName);

$attributes = [];

Expand Down
27 changes: 22 additions & 5 deletions src/Tempest/View/src/Renderers/TempestViewCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Tempest\View\Element;
use Tempest\View\Elements\ElementFactory;
use function Tempest\path;
use function Tempest\Support\str;
use const Dom\HTML_NO_DEFAULT_NS;

final readonly class TempestViewCompiler
Expand Down Expand Up @@ -81,11 +82,27 @@ private function retrieveTemplate(string $path): string

private function parseDom(string $template): NodeList
{
$template = str_replace(
search: array_keys(self::TOKEN_MAPPING),
replace: array_values(self::TOKEN_MAPPING),
subject: $template,
);
$template = str($template)

// Escape PHP tags
->replace(
search: array_keys(self::TOKEN_MAPPING),
replace: array_values(self::TOKEN_MAPPING),
)

// Convert self-closing tags
->replaceRegex(
regex: '/<x-(?<element>.*?)\/>/',
replace: function (array $match) {
$closingTag = str($match['element'])->before(' ')->toString();

return sprintf(
'<x-%s></x-%s>',
$match['element'],
$closingTag,
);
},
);

$dom = HTMLDocument::createFromString("<div id='tempest_render'>{$template}</div>", LIBXML_NOERROR | HTML_NO_DEFAULT_NS);

Expand Down
23 changes: 23 additions & 0 deletions tests/Fixtures/Console/StylingCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Tests\Tempest\Fixtures\Console;

use Tempest\Console\ConsoleCommand;
use Tempest\Console\HasConsole;

final readonly class StylingCommand
{
use HasConsole;

#[ConsoleCommand(name: 'test:style')]
public function __invoke(): void
{
$this
->info('info')
->success('success')
->warning('warning')
->error('error');
}
}
41 changes: 41 additions & 0 deletions tests/Integration/View/TempestViewRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -418,4 +418,45 @@ 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_slot_with_comment(): void
{
$this->assertStringEqualsStringIgnoringLineEndings(
<<<'HTML'
<div class="base"><!-- example of comment -->
Test
</div>
HTML,
$this->render(
<<<'HTML'
<x-base-layout>
<!-- example of comment -->
Test
</x-base-layout>
HTML,
),
);
}

public function test_self_closing_component_tags_are_compiled(): void
{
$this->registerViewComponent('x-foo', '<div>foo</div>');

$this->assertStringEqualsStringIgnoringLineEndings(
'<div>foo</div><div>foo</div>',
str_replace(PHP_EOL, '', $this->render('<x-foo /><x-foo />')),
);

$this->assertStringEqualsStringIgnoringLineEndings(
'<div>foo</div><div>foo</div>',
str_replace(PHP_EOL, '', $this->render('<x-foo/><x-foo/>')),
);

$this->assertStringEqualsStringIgnoringLineEndings(
'<div>foo</div><div>foo</div>',
str_replace(PHP_EOL, '', $this->render('<x-foo foo="bar" :baz="$hello"/><x-foo foo="bar" :baz="$hello"/>')),
);
}
}

0 comments on commit 32aba50

Please sign in to comment.