Skip to content

Commit 2f0b247

Browse files
erikaraujobrendt
andauthored
fix(view): : is replaced by - and @ is removed (#1125)
Co-authored-by: brendt <[email protected]>
1 parent da7006f commit 2f0b247

File tree

5 files changed

+41
-32
lines changed

5 files changed

+41
-32
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"ext-readline": "*",
1414
"ext-simplexml": "*",
1515
"filp/whoops": "^2.15",
16+
"giggsey/libphonenumber-for-php-lite": "^9.0",
1617
"guzzlehttp/guzzle": "^7.8",
1718
"laminas/laminas-diactoros": "^3.3",
1819
"monolog/monolog": "^3.7.0",
@@ -34,8 +35,7 @@
3435
"symfony/var-exporter": "^7.1",
3536
"tempest/highlight": "^2.11.2",
3637
"vlucas/phpdotenv": "^5.6",
37-
"voku/portable-ascii": "^2.0.3",
38-
"giggsey/libphonenumber-for-php-lite": "^9.0"
38+
"voku/portable-ascii": "^2.0.3"
3939
},
4040
"require-dev": {
4141
"aidan-casey/mock-client": "dev-master",

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

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -66,22 +66,7 @@ private function makeElement(Token $token, ?Element $parent): ?Element
6666
return new RawElement(tag: null, content: $token->compile());
6767
}
6868

69-
$attributes = [];
70-
71-
foreach ($token->htmlAttributes as $name => $value) {
72-
$name = str($name)
73-
->trim()
74-
->before('=')
75-
->camel()
76-
->toString();
77-
78-
$value = str($value)
79-
->afterFirst('"')
80-
->beforeLast('"')
81-
->toString();
82-
83-
$attributes[$name] = $value;
84-
}
69+
$attributes = $token->htmlAttributes;
8570

8671
foreach ($token->phpAttributes as $index => $content) {
8772
$attributes[] = new PhpAttribute((string) $index, $content);

src/Tempest/View/src/Elements/GenericElement.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,6 @@ public function compile(): string
3838
$attributes = [];
3939

4040
foreach ($this->getAttributes() as $name => $value) {
41-
$name = str($name);
42-
43-
if ($name->startsWith(':')) {
44-
$name = ':' . $name->kebab()->toString();
45-
} else {
46-
$name = $name->kebab()->toString();
47-
}
48-
4941
if ($value) {
5042
$attributes[] = $name . '="' . $value . '"';
5143
} else {

src/Tempest/View/src/Elements/PhpDataElement.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,14 @@ public function getWrappingElement(): Element
2626

2727
public function compile(): string
2828
{
29-
$name = ltrim($this->name, ':');
29+
$variableName = str($this->name)->ltrim(':')->camel()->toString();
3030
$isExpression = str_starts_with($this->name, ':');
31-
3231
$value = $this->value ?? '';
3332

3433
// We'll declare the variable in PHP right before the actual element
3534
$variableDeclaration = sprintf(
3635
'$%s ??= %s ?? null;',
37-
$name,
36+
$variableName,
3837
$isExpression
3938
? ($value ?: 'null')
4039
: var_export($value, true), // @mago-expect best-practices/no-debug-symbols
@@ -44,17 +43,22 @@ public function compile(): string
4443
// where the variable is only available to that specific element.
4544
$variableRemoval = sprintf(
4645
'unset($%s);',
47-
$name,
46+
$variableName,
4847
);
4948

5049
// Support for boolean attributes. When an expression attribute has a falsy value, it won't be rendered at all.
5150
// When it's "true", it will only render the attribute name and not the "true" value
5251
$coreElement = $this->unwrap(GenericElement::class);
5352

5453
if ($isExpression && $coreElement) {
54+
$attributeName = ltrim($this->name, ':');
55+
5556
$coreElement
56-
->addRawAttribute(new RawConditionalAttribute($name, $coreElement->getAttribute($name))->compile())
57-
->unsetAttribute($name);
57+
->addRawAttribute(new RawConditionalAttribute(
58+
name: $attributeName,
59+
value: $coreElement->getAttribute($attributeName),
60+
)->compile())
61+
->unsetAttribute($attributeName);
5862
}
5963

6064
return sprintf(

tests/Integration/View/TempestViewRendererTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,34 @@ public function test_view_processors(): void
547547
$this->assertStringEqualsStringIgnoringLineEndings('<div>test</div>', $html);
548548
}
549549

550+
public function test_with_at_symbol_in_html_tag(): void
551+
{
552+
$rendered = $this->render(
553+
view('<button @click="foo">test</button>'),
554+
);
555+
556+
$this->assertStringEqualsStringIgnoringLineEndings(
557+
<<<HTML
558+
<button @click="foo">test</button>
559+
HTML,
560+
$rendered,
561+
);
562+
}
563+
564+
public function test_with_colon_symbol_in_html_tag(): void
565+
{
566+
$rendered = $this->render(
567+
view('<button x-on:click="foo">test</button>'),
568+
);
569+
570+
$this->assertStringEqualsStringIgnoringLineEndings(
571+
<<<HTML
572+
<button x-on:click="foo">test</button>
573+
HTML,
574+
$rendered,
575+
);
576+
}
577+
550578
private function assertSnippetsMatch(string $expected, string $actual): void
551579
{
552580
$expected = str_replace([PHP_EOL, ' '], '', $expected);

0 commit comments

Comments
 (0)