Skip to content
Closed
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
17 changes: 15 additions & 2 deletions src/Precompilers/ExtractTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Livewire\Volt\Exceptions\ReturnNewClassExecutionEndingException;
use Livewire\Volt\MountedDirectories;
use Livewire\Volt\Volt;
use PhpToken;

class ExtractTemplate
{
Expand Down Expand Up @@ -48,9 +49,21 @@ protected function shouldExtractTemplate(string $template): bool
*/
protected function html(string $template): string
{
$template = trim(preg_replace('/<\?php\s*(.*?)\s*\?>/s', '', $template));
$html = '';
$inPhp = false;

return str($template)->beforeLast('<?php')->trim()->value();
foreach (PhpToken::tokenize($template) as $token) {
if ($token->is(T_OPEN_TAG)) {
$inPhp = true;
} elseif ($inPhp && $token->is(T_CLOSE_TAG)) {
$inPhp = false;
$html .= substr($token->text, 2);
} elseif (! $inPhp) {
$html .= $token->text;
}
}

return trim($html);
}

/**
Expand Down
8 changes: 8 additions & 0 deletions tests/Feature/FunctionalComponentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -939,3 +939,11 @@ public function render()
->call('increment')
->assertSet('counter', 3);
});

it('renders PHP tags in state strings before and after an update', function () {
Volt::test('php-tags-in-strings')
->assertSee('Example of ?> causing problems')
->assertSee('PHP starts with <?php and ends with ?>')
->call('update')
->assertSee('Updated text containing ?>');
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

use function Livewire\Volt\state;

state([
'title' => 'Example of ?> causing problems',
'message' => 'PHP starts with <?php and ends with ?>',
]);

$update = fn () => $this->title = 'Updated text containing ?>';
?>

<div>
<h1>{{ $title }}</h1>
<p>{{ $message }}</p>
<button wire:click="update">Update text</button>
</div>
62 changes: 62 additions & 0 deletions tests/Unit/Precompilers/ExtractTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -418,3 +418,65 @@ function something() use () {

expect(trim($result))->toBe($expected);
})->with($conflictsDataset);

it('ignores PHP tags inside strings when extracting the template', function () {
$template = <<<'HTML'
<?php

use function Livewire\Volt\state;

state([
'title' => 'Example of ?> causing problems',
'message' => 'PHP starts with <?php and ends with ?>',
]);

?>

<div>
<h1>{{ $title }}</h1>
<p>{{ $message }}</p>
</div>
HTML;

$expected = <<<'HTML'
<div>
<h1>{{ $title }}</h1>
<p>{{ $message }}</p>
</div>
HTML;

expect($this->precompiler->__invoke($template))->toBe($expected);
});

it('ignores PHP tags inside comments and multiline strings', function () {
$template = <<<'HTML'
<?php

/* A comment containing ?> and <?php */
$message = <<<'TEXT'
PHP starts with <?php and ends with ?>
TEXT;

?>

<div>{{ $message }}</div>
HTML;

expect($this->precompiler->__invoke($template))->toBe('<div>{{ $message }}</div>');
});

it('preserves short echo tags in the template', function () {
$template = <<<'HTML'
<?php

use function Livewire\Volt\state;

state('name');

?>

<div><?= $name ?></div>
HTML;

expect($this->precompiler->__invoke($template))->toBe('<div><?= $name ?></div>');
});
Loading