Skip to content
Open
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
6 changes: 4 additions & 2 deletions src/Languages/Base/Injections/CustomClassInjection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
20 changes: 20 additions & 0 deletions tests/Languages/Base/Injections/CustomClassInjectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Loading