diff --git a/src/BladeService.php b/src/BladeService.php index 746f8060..6d8e8b42 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 { @@ -222,20 +223,10 @@ public static function earliestPreCompilationHook(callable $callback): void */ public static function preStoreUncompiledBlocks(string $input): string { - $compiler = app('blade.compiler'); - $reflection = new \ReflectionClass($compiler); - - $storeRawBlock = $reflection->getMethod('storeRawBlock'); - $output = $input; - $output = preg_replace_callback('/(?invoke($compiler, "@verbatim{$matches[2]}@endverbatim"); - }, $output); - - $output = preg_replace_callback('/(?invoke($compiler, "@php{$matches[1]}@endphp"); - }, $output); + $output = static::storeVerbatimBlocks($output); + $output = static::storePhpBlocks($output); return $output; } @@ -245,12 +236,28 @@ public static function preStoreUncompiledBlocks(string $input): string */ public static function storeVerbatimBlocks(string $input): string { - $compiler = app('blade.compiler'); + return static::storeRawBlock(LaravelRegex::VERBATIM_BLOCK, $input); + } + + /** + * Store only @verbatim blocks as raw block placeholders. + */ + 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); - $method = $reflection->getMethod('storeVerbatimBlocks'); - return $method->invoke($compiler, $input); + return preg_replace_callback($pattern, function ($matches) use ($compiler, $reflection) { + return $reflection->getMethod('storeRawBlock')->invoke($compiler, $matches[0]); + }, $content); } /** 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() — /(?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