Skip to content

Commit c0bc625

Browse files
committed
Fix deletion markers conflicting with Twig whitespace control
1 parent 36c6d01 commit c0bc625

5 files changed

Lines changed: 33 additions & 3 deletions

File tree

src/Languages/Base/Injections/DeletionInjection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function parse(string $content, Highlighter $highlighter): ParsedInjectio
2222
$content = str_replace('❷span class=❹ignore❹❸{-❷/span❸', '{-', $content);
2323
$content = str_replace('❷span class=❹ignore❹❸-}❷/span❸', '-}', $content);
2424

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

2727
$parsedOffset = 0;
2828

src/Languages/Base/Patterns/DeletionEndTokenPattern.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@
1212

1313
#[PatternTest('{- pull_request_target: -}', '-}')]
1414
#[PatternTest('{-- pull_request_target: --}', null)]
15+
#[PatternTest('{{- variable -}}', null)]
1516
final readonly class DeletionEndTokenPattern implements Pattern
1617
{
1718
use IsPattern;
1819

1920
public function getPattern(): string
2021
{
21-
return '/(?<!-)(?<match>\-})/';
22+
return '/(?<!-)(?<match>\-})(?!})/';
2223
}
2324

2425
public function getTokenType(): TokenType

src/Languages/Base/Patterns/DeletionStartTokenPattern.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@
1212

1313
#[PatternTest('{- pull_request_target: -}', '{-')]
1414
#[PatternTest('{-- pull_request_target: --}', null)]
15+
#[PatternTest('{{- variable -}}', null)]
1516
final readonly class DeletionStartTokenPattern implements Pattern
1617
{
1718
use IsPattern;
1819

1920
public function getPattern(): string
2021
{
21-
return '/(?<match>{\-)(?!-)/';
22+
return '/(?<!\{)(?<match>{\-)(?!-)/';
2223
}
2324

2425
public function getTokenType(): TokenType

tests/Languages/Base/Injections/DeletionInjectionTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,31 @@ public function test_deletion_injection()
2525

2626
$this->assertSame($expected, Escape::html($parsedInjection->content));
2727
}
28+
29+
public function test_deletion_not_triggered_by_twig_whitespace_control()
30+
{
31+
$content = '{{- block(outerBlocks.content) -}}';
32+
33+
$parsedInjection = (new DeletionInjection())->parse($content, new Highlighter());
34+
35+
$this->assertSame($content, $parsedInjection->content);
36+
}
37+
38+
public function test_deletion_not_triggered_when_preceded_by_brace()
39+
{
40+
$content = '{{- variable -}}';
41+
42+
$parsedInjection = (new DeletionInjection())->parse($content, new Highlighter());
43+
44+
$this->assertSame($content, $parsedInjection->content);
45+
}
46+
47+
public function test_deletion_not_triggered_by_tag_whitespace_control()
48+
{
49+
$content = '{%- if true -%}';
50+
51+
$parsedInjection = (new DeletionInjection())->parse($content, new Highlighter());
52+
53+
$this->assertSame($content, $parsedInjection->content);
54+
}
2855
}

tests/Languages/Twig/TwigLanguageTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public static function provide_highlight_cases(): iterable
3939
['<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;'],
4040
['{% cache "cache key" %}', '<span class="hl-injection">{% <span class="hl-keyword">cache</span> &quot;<span class="hl-value">cache key</span>&quot; %}</span>'],
4141
['{{ "<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>'],
42+
['{{- block(outerBlocks.content) -}}', '<span class="hl-injection">{{- <span class="hl-property">block</span>(outerBlocks.<span class="hl-property">content</span>) -}}</span>'],
4243
];
4344
}
4445
}

0 commit comments

Comments
 (0)