Skip to content

Commit 1f35dbf

Browse files
authored
Merge pull request #53 from ryo-morimoto/fix/comment-whitespace-trim
Fix whitespace trim not working on {%- endcomment -%} tag.
2 parents d1b2af4 + ab26542 commit 1f35dbf

4 files changed

Lines changed: 47 additions & 2 deletions

File tree

src/Parse/Lexer.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,10 @@ protected function lexComment(): void
288288
$text = substr($this->source, $this->cursor, $matches[0][1] - $this->cursor);
289289

290290
$this->moveCursor($text.$matches[0][0]);
291+
292+
if ($matches[2][0][0] === LexerOptions::WhitespaceTrim->value) {
293+
$this->trimWhitespaces();
294+
}
291295
}
292296

293297
protected function lexInlineComment(): void

src/Parse/LexerOptions.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,10 @@ public static function blockCommentDataRegex(): string
116116

117117
if ($regex === null) {
118118
$regex = sprintf(
119-
'{%s(%s)?\s*endcomment\s*(?:%s|%s)}sx',
119+
'{(%s%s?)\s*endcomment\s*(%s?%s)}sx',
120120
preg_quote(LexerOptions::TagBlockStart->value),
121121
LexerOptions::WhitespaceTrim->value,
122-
preg_quote(LexerOptions::WhitespaceTrim->value.LexerOptions::TagBlockEnd->value),
122+
LexerOptions::WhitespaceTrim->value,
123123
preg_quote(LexerOptions::TagBlockEnd->value),
124124
);
125125
}

tests/Integration/TrimModeTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,27 @@
488488
</div>
489489
HTML;
490490
assertTemplateResult($expected, $source);
491+
492+
$source = <<<'LIQUID'
493+
{%- comment -%}123{%- endcomment -%}Hello!
494+
LIQUID;
495+
assertTemplateResult('Hello!', $source);
496+
497+
$source = <<<'LIQUID'
498+
{%- comment -%}123{%- endcomment -%} Hello!
499+
LIQUID;
500+
assertTemplateResult('Hello!', $source);
501+
502+
$source = <<<'LIQUID'
503+
{%- comment -%}123{%- endcomment -%} Hello!
504+
LIQUID;
505+
assertTemplateResult('Hello!', $source);
506+
507+
$source = <<<'LIQUID'
508+
{%- comment %}Whitespace control!{% endcomment -%}
509+
Hello!
510+
LIQUID;
511+
assertTemplateResult('Hello!', $source);
491512
});
492513

493514
test('complex trim output', function () {

tests/Unit/LexerTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,26 @@
133133
$tokens->consume(TokenType::BlockEnd);
134134
});
135135

136+
test('[comment] whitespace trim', function () {
137+
expect(tokenize('{%- comment -%}123{%- endcomment -%} Hello!')->consume())
138+
->type->toBe(TokenType::TextData)
139+
->data->toBe('Hello!');
140+
141+
expect(tokenize("{%- comment -%}123{%- endcomment -%}\nHello!")->consume())
142+
->type->toBe(TokenType::TextData)
143+
->data->toBe('Hello!');
144+
});
145+
146+
test('[comment] without whitespace trim', function () {
147+
expect(tokenize('{% comment %}123{% endcomment %} Hello!')->consume())
148+
->type->toBe(TokenType::TextData)
149+
->data->toBe(' Hello!');
150+
151+
expect(tokenize("{% comment %}123{% endcomment %}\nHello!")->consume())
152+
->type->toBe(TokenType::TextData)
153+
->data->toBe("\nHello!");
154+
});
155+
136156
test('text', function () {
137157
expect(tokenize(' ')->consume())
138158
->type->toBe(TokenType::TextData)

0 commit comments

Comments
 (0)