From ecd72e58bbd98b8bafbb7fe65b940d56735f364d Mon Sep 17 00:00:00 2001 From: Filip Ganyicz Date: Sun, 1 Mar 2026 15:47:14 +0100 Subject: [PATCH 1/3] Add failing test --- tests/Compiler/WrapperTest.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tests/Compiler/WrapperTest.php b/tests/Compiler/WrapperTest.php index f24b8145..5c2c5db9 100644 --- a/tests/Compiler/WrapperTest.php +++ b/tests/Compiler/WrapperTest.php @@ -105,7 +105,13 @@ ]); test('preserves php directives', function () { - $input = '@php something @endphp'; + $input = '@php /* uncompiled */ @endphp'; + + expect(app(Wrapper::class)->wrap($input, ''))->toContain($input); +}); + +test('preserves verbatim directives', function () { + $input = '@verbatim /* uncompiled */ @endverbatim'; expect(app(Wrapper::class)->wrap($input, ''))->toContain($input); }); \ No newline at end of file From 681e77fc20fb7cf7c6a877da04242c6432a17318 Mon Sep 17 00:00:00 2001 From: Filip Ganyicz Date: Tue, 3 Mar 2026 22:12:41 +0100 Subject: [PATCH 2/3] Fix verbatim inside components --- src/BladeService.php | 41 +++++++++++++++++++++--------------- src/Support/LaravelRegex.php | 14 ++++++++++++ 2 files changed, 38 insertions(+), 17 deletions(-) diff --git a/src/BladeService.php b/src/BladeService.php index 746f8060..25cbd2e9 100644 --- a/src/BladeService.php +++ b/src/BladeService.php @@ -7,8 +7,9 @@ use Illuminate\Support\Facades\File; use Illuminate\Support\Str; use Illuminate\View\Compilers\ComponentTagCompiler; -use ReflectionClass; +use Livewire\Blaze\Support\LaravelRegex; use Livewire\Blaze\Support\Utils; +use ReflectionClass; class BladeService { @@ -218,24 +219,27 @@ public static function earliestPreCompilationHook(callable $callback): void } /** - * Invoke the Blade compiler's storeUncompiledBlocks via reflection. + * Store a raw block placeholder via the Blade compiler. */ - public static function preStoreUncompiledBlocks(string $input): string + public static function storeRawBlock(string $pattern, string $content): string { $compiler = app('blade.compiler'); $reflection = new \ReflectionClass($compiler); - - $storeRawBlock = $reflection->getMethod('storeRawBlock'); - $output = $input; + return preg_replace_callback($pattern, function ($matches) use ($compiler, $reflection) { + return $reflection->getMethod('storeRawBlock')->invoke($compiler, $matches[0]); + }, $content); + } - $output = preg_replace_callback('/(?invoke($compiler, "@verbatim{$matches[2]}@endverbatim"); - }, $output); + /** + * Invoke the Blade compiler's storeUncompiledBlocks via reflection. + */ + public static function preStoreUncompiledBlocks(string $input): string + { + $output = $input; - $output = preg_replace_callback('/(?invoke($compiler, "@php{$matches[1]}@endphp"); - }, $output); + $output = static::storeVerbatimBlocks($output); + $output = static::storePhpBlocks($output); return $output; } @@ -245,12 +249,15 @@ public static function preStoreUncompiledBlocks(string $input): string */ public static function storeVerbatimBlocks(string $input): string { - $compiler = app('blade.compiler'); - - $reflection = new \ReflectionClass($compiler); - $method = $reflection->getMethod('storeVerbatimBlocks'); + return static::storeRawBlock(LaravelRegex::VERBATIM_BLOCK, $input); + } - return $method->invoke($compiler, $input); + /** + * Store only @verbatim blocks as raw block placeholders. + */ + public static function storePhpBlocks(string $input): string + { + return static::storeRawBlock(LaravelRegex::PHP_BLOCK, $input); } /** diff --git a/src/Support/LaravelRegex.php b/src/Support/LaravelRegex.php index 47329d52..20f43a5c 100644 --- a/src/Support/LaravelRegex.php +++ b/src/Support/LaravelRegex.php @@ -50,4 +50,18 @@ class LaravelRegex ) )? /x'; + + /** + * Pattern for matching @verbatim...@endverbatim blocks. + * + * @see BladeCompiler::storeVerbatimBlocks() — /(? Date: Tue, 3 Mar 2026 22:24:26 +0100 Subject: [PATCH 3/3] Refactor --- src/BladeService.php | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/BladeService.php b/src/BladeService.php index 25cbd2e9..6d8e8b42 100644 --- a/src/BladeService.php +++ b/src/BladeService.php @@ -218,19 +218,6 @@ public static function earliestPreCompilationHook(callable $callback): void }); } - /** - * Store a raw block placeholder via the Blade compiler. - */ - public static function storeRawBlock(string $pattern, string $content): string - { - $compiler = app('blade.compiler'); - $reflection = new \ReflectionClass($compiler); - - return preg_replace_callback($pattern, function ($matches) use ($compiler, $reflection) { - return $reflection->getMethod('storeRawBlock')->invoke($compiler, $matches[0]); - }, $content); - } - /** * Invoke the Blade compiler's storeUncompiledBlocks via reflection. */ @@ -260,6 +247,19 @@ public static function storePhpBlocks(string $input): string return static::storeRawBlock(LaravelRegex::PHP_BLOCK, $input); } + /** + * Store a raw block placeholder via the Blade compiler. + */ + protected static function storeRawBlock(string $pattern, string $content): string + { + $compiler = app('blade.compiler'); + $reflection = new \ReflectionClass($compiler); + + return preg_replace_callback($pattern, function ($matches) use ($compiler, $reflection) { + return $reflection->getMethod('storeRawBlock')->invoke($compiler, $matches[0]); + }, $content); + } + /** * Restore raw block placeholders to their original content. */