Skip to content
Merged
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
2 changes: 1 addition & 1 deletion src/Languages/Base/Injections/DeletionInjection.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function parse(string $content, Highlighter $highlighter): ParsedInjectio
$content = str_replace('❷span class=❹ignore❹❸{-❷/span❸', '{-', $content);
$content = str_replace('❷span class=❹ignore❹❸-}❷/span❸', '-}', $content);

preg_match_all('/(\{-)(?!-)((.|\n)*?)(-})/', $content, $matches, PREG_OFFSET_CAPTURE);
preg_match_all('/(?<!\{)(\{-)(?!-)((.|\n)*?)(-})(?!})/', $content, $matches, PREG_OFFSET_CAPTURE);

$parsedOffset = 0;

Expand Down
3 changes: 2 additions & 1 deletion src/Languages/Base/Patterns/DeletionEndTokenPattern.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@

#[PatternTest('{- pull_request_target: -}', '-}')]
#[PatternTest('{-- pull_request_target: --}', null)]
#[PatternTest('{{- variable -}}', null)]
final readonly class DeletionEndTokenPattern implements Pattern
{
use IsPattern;

public function getPattern(): string
{
return '/(?<!-)(?<match>\-})/';
return '/(?<!-)(?<match>\-})(?!})/';
}

public function getTokenType(): TokenType
Expand Down
3 changes: 2 additions & 1 deletion src/Languages/Base/Patterns/DeletionStartTokenPattern.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@

#[PatternTest('{- pull_request_target: -}', '{-')]
#[PatternTest('{-- pull_request_target: --}', null)]
#[PatternTest('{{- variable -}}', null)]
final readonly class DeletionStartTokenPattern implements Pattern
{
use IsPattern;

public function getPattern(): string
{
return '/(?<match>{\-)(?!-)/';
return '/(?<!\{)(?<match>{\-)(?!-)/';
}

public function getTokenType(): TokenType
Expand Down
27 changes: 27 additions & 0 deletions tests/Languages/Base/Injections/DeletionInjectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,31 @@ public function test_deletion_injection()

$this->assertSame($expected, Escape::html($parsedInjection->content));
}

public function test_deletion_not_triggered_by_twig_whitespace_control()
{
$content = '{{- block(outerBlocks.content) -}}';

$parsedInjection = (new DeletionInjection())->parse($content, new Highlighter());

$this->assertSame($content, $parsedInjection->content);
}

public function test_deletion_not_triggered_when_preceded_by_brace()
{
$content = '{{- variable -}}';

$parsedInjection = (new DeletionInjection())->parse($content, new Highlighter());

$this->assertSame($content, $parsedInjection->content);
}

public function test_deletion_not_triggered_by_tag_whitespace_control()
{
$content = '{%- if true -%}';

$parsedInjection = (new DeletionInjection())->parse($content, new Highlighter());

$this->assertSame($content, $parsedInjection->content);
}
}
1 change: 1 addition & 0 deletions tests/Languages/Twig/TwigLanguageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public static function provide_highlight_cases(): iterable
['<script>const mainSearchUrl = "";</script>', '&lt;<span class="hl-keyword">script</span>&gt;<span class="hl-keyword">const</span> mainSearchUrl = <span class="hl-value">&quot;&quot;</span>;&lt;/<span class="hl-keyword">script</span>&gt;'],
['{% cache "cache key" %}', '<span class="hl-injection">{% <span class="hl-keyword">cache</span> &quot;<span class="hl-value">cache key</span>&quot; %}</span>'],
['{{ "<b>foobar</b>"|data_uri(mime="text/html", parameters={charset: "ascii"}) }}', '<span class="hl-injection">{{ &quot;<span class="hl-value">&lt;b&gt;foobar&lt;/b&gt;</span>&quot;|<span class="hl-property">data_uri</span>(<span class="hl-property">mime</span>=&quot;<span class="hl-value">text/html</span>&quot;, <span class="hl-property">parameters</span>={<span class="hl-property">charset</span>: &quot;<span class="hl-value">ascii</span>&quot;}) }}</span>'],
['{{- block(outerBlocks.content) -}}', '<span class="hl-injection">{{- <span class="hl-property">block</span>(outerBlocks.<span class="hl-property">content</span>) -}}</span>'],
];
}
}