From e8dcf5dbecdf066abb0439d8c21313f95f2fcf78 Mon Sep 17 00:00:00 2001 From: Pascal CESCON - Amoifr Date: Mon, 5 Oct 2026 09:55:04 +0200 Subject: [PATCH] Only strip the delimiters of matched custom class blocks --- .../Base/Injections/CustomClassInjection.php | 6 ++++-- .../Injections/CustomClassInjectionTest.php | 20 +++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/Languages/Base/Injections/CustomClassInjection.php b/src/Languages/Base/Injections/CustomClassInjection.php index 2edb9a72..f08f13e4 100644 --- a/src/Languages/Base/Injections/CustomClassInjection.php +++ b/src/Languages/Base/Injections/CustomClassInjection.php @@ -36,10 +36,12 @@ public function parse(string $content, Highlighter $highlighter): ParsedInjectio ); $additionalOffset += strlen($endToken); - - $content = str_replace([$startToken, $endToken], '', $content); } + // Only strip the delimiters of the matched blocks: a global replacement would also remove + // any `{:class:` or `:}` occurring in the code itself, and shift the offsets of the tokens. + $content = preg_replace_callback($pattern, static fn (array $match) => $match['match'], $content); + return new ParsedInjection( content: $content, tokens: $tokens, diff --git a/tests/Languages/Base/Injections/CustomClassInjectionTest.php b/tests/Languages/Base/Injections/CustomClassInjectionTest.php index 9dee7e66..b4ce8d86 100644 --- a/tests/Languages/Base/Injections/CustomClassInjectionTest.php +++ b/tests/Languages/Base/Injections/CustomClassInjectionTest.php @@ -34,4 +34,24 @@ public function test_custom_class_injection() $this->assertSame(24, $parsed->tokens[2]->end); $this->assertSame('hl-type', $parsed->tokens[2]->type->getValue()); } + + public function test_custom_class_injection_keeps_delimiters_outside_of_blocks() + { + $content = <<<'TXT' +{:hl-property:x:} = "{:}".format({:hl-type:y:}) +TXT; + + $expected = <<<'TXT' +x = "{:}".format(y) +TXT; + + $parsed = (new CustomClassInjection())->parse($content, new Highlighter()); + + $this->assertSame($expected, $parsed->content); + $this->assertCount(2, $parsed->tokens); + $this->assertSame(0, $parsed->tokens[0]->start); + $this->assertSame(1, $parsed->tokens[0]->end); + $this->assertSame(17, $parsed->tokens[1]->start); + $this->assertSame(18, $parsed->tokens[1]->end); + } }