Skip to content

Commit f2c03df

Browse files
authored
fix(view): remove multiline comments before AST parsing (#1395)
1 parent 3ac0e3a commit f2c03df

File tree

3 files changed

+35
-10
lines changed

3 files changed

+35
-10
lines changed

packages/view/src/Elements/TextElement.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,6 @@ public function __construct(
1919
public function compile(): string
2020
{
2121
return str($this->text)
22-
// Render {{-- --}}
23-
->replaceRegex(
24-
regex: '/{{--(.|\n)*?--}}/',
25-
replace: '',
26-
)
2722
// Render {{
2823
->replaceRegex(
2924
regex: '/{{(?<match>.*?)}}/',

packages/view/src/Parser/TempestViewCompiler.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,24 +41,34 @@ public function compile(string|View $view): string
4141
// 1. Retrieve template
4242
$template = $this->retrieveTemplate($view);
4343

44-
// 2. Parse AST
44+
// 2. Remove comments before parsing
45+
$template = $this->removeComments($template);
46+
47+
// 3. Parse AST
4548
$ast = $this->parseAst($template);
4649

47-
// 3. Map to elements
50+
// 4. Map to elements
4851
$elements = $this->mapToElements($ast);
4952

50-
// 4. Apply attributes
53+
// 5. Apply attributes
5154
$elements = $this->applyAttributes($elements);
5255

53-
// 5. Compile to PHP
56+
// 6. Compile to PHP
5457
$compiled = $this->compileElements($elements);
5558

56-
// 6. Cleanup compiled PHP
59+
// 7. Cleanup compiled PHP
5760
$cleaned = $this->cleanupCompiled($compiled);
5861

5962
return $cleaned;
6063
}
6164

65+
private function removeComments(string $template): string
66+
{
67+
return str($template)
68+
->replaceRegex('/{{--.*?--}}/s', '')
69+
->toString();
70+
}
71+
6272
private function retrieveTemplate(string|View $view): string
6373
{
6474
$path = ($view instanceof View) ? $view->path : $view;

tests/Integration/View/TempestViewRendererTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,4 +727,24 @@ public function test_view_comments(): void
727727

728728
$this->assertSnippetsMatch('<p>this is rendered text</p>', $html);
729729
}
730+
731+
public function test_multiline_view_comments(): void
732+
{
733+
$html = $this->render(<<<'HTML'
734+
{{-- this is a comment
735+
<div>
736+
<!-- Start -->
737+
<x-label>{{ Tempest\Intl\translate('test_2') }}</x-label>
738+
<x-input
739+
name="test"
740+
type="text"
741+
class="py-2.5 sm:py-3 px-4 block w-full border-gray-500 border-1 rounded-lg sm:text-sm focus:border-blue-500 focus:ring-blue-500 disabled:opacity-50 disabled:pointer-events-none dark:bg-neutral-900 dark:border-neutral-700 dark:text-neutral-400 dark:placeholder-neutral-500 dark:focus:ring-neutral-600" placeholder="This is placeholder" />
742+
<!-- end -->
743+
</div>
744+
--}}
745+
<p>This should be rendered</p>
746+
HTML);
747+
748+
$this->assertSnippetsMatch('<p>This should be rendered</p>', $html);
749+
}
730750
}

0 commit comments

Comments
 (0)