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
4 changes: 4 additions & 0 deletions src/Parse/Lexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,10 @@ protected function lexComment(): void
$text = substr($this->source, $this->cursor, $matches[0][1] - $this->cursor);

$this->moveCursor($text.$matches[0][0]);

if ($matches[2][0][0] === LexerOptions::WhitespaceTrim->value) {
$this->trimWhitespaces();
}
}

protected function lexInlineComment(): void
Expand Down
4 changes: 2 additions & 2 deletions src/Parse/LexerOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,10 @@ public static function blockCommentDataRegex(): string

if ($regex === null) {
$regex = sprintf(
'{%s(%s)?\s*endcomment\s*(?:%s|%s)}sx',
'{(%s%s?)\s*endcomment\s*(%s?%s)}sx',
preg_quote(LexerOptions::TagBlockStart->value),
LexerOptions::WhitespaceTrim->value,
preg_quote(LexerOptions::WhitespaceTrim->value.LexerOptions::TagBlockEnd->value),
LexerOptions::WhitespaceTrim->value,
preg_quote(LexerOptions::TagBlockEnd->value),
);
}
Expand Down
21 changes: 21 additions & 0 deletions tests/Integration/TrimModeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,27 @@
</div>
HTML;
assertTemplateResult($expected, $source);

$source = <<<'LIQUID'
{%- comment -%}123{%- endcomment -%}Hello!
LIQUID;
assertTemplateResult('Hello!', $source);

$source = <<<'LIQUID'
{%- comment -%}123{%- endcomment -%} Hello!
LIQUID;
assertTemplateResult('Hello!', $source);

$source = <<<'LIQUID'
{%- comment -%}123{%- endcomment -%} Hello!
LIQUID;
assertTemplateResult('Hello!', $source);

$source = <<<'LIQUID'
{%- comment %}Whitespace control!{% endcomment -%}
Hello!
LIQUID;
assertTemplateResult('Hello!', $source);
});

test('complex trim output', function () {
Expand Down
20 changes: 20 additions & 0 deletions tests/Unit/LexerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,26 @@
$tokens->consume(TokenType::BlockEnd);
});

test('[comment] whitespace trim', function () {
expect(tokenize('{%- comment -%}123{%- endcomment -%} Hello!')->consume())
->type->toBe(TokenType::TextData)
->data->toBe('Hello!');

expect(tokenize("{%- comment -%}123{%- endcomment -%}\nHello!")->consume())
->type->toBe(TokenType::TextData)
->data->toBe('Hello!');
});

test('[comment] without whitespace trim', function () {
expect(tokenize('{% comment %}123{% endcomment %} Hello!')->consume())
->type->toBe(TokenType::TextData)
->data->toBe(' Hello!');

expect(tokenize("{% comment %}123{% endcomment %}\nHello!")->consume())
->type->toBe(TokenType::TextData)
->data->toBe("\nHello!");
});

test('text', function () {
expect(tokenize(' ')->consume())
->type->toBe(TokenType::TextData)
Expand Down