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
8 changes: 8 additions & 0 deletions src/TwigComponent/src/Twig/TwigPreLexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ private function consumeAttributes(string $componentName): string
$isAttributeDynamic = false;

// :someProp="dynamicVar"
$this->consumeWhitespace();
if ($this->check(':')) {
$this->consume(':');
$isAttributeDynamic = true;
Expand All @@ -244,6 +245,7 @@ private function consumeAttributes(string $componentName): string

// <twig:component someProp> -> someProp: true
if (!$this->check('=')) {
$this->consumeWhitespace();
// don't allow "<twig:component :someProp>"
if ($isAttributeDynamic) {
throw new SyntaxError(\sprintf('Expected "=" after ":%s" when parsing the "<twig:%s" syntax.', $key, $componentName), $this->line);
Expand Down Expand Up @@ -332,6 +334,12 @@ private function consumeWhitespace(): void
$whitespace = substr($this->input, $this->position, strspn($this->input, " \t\n\r\0\x0B", $this->position));
$this->line += substr_count($whitespace, "\n");
$this->position += \strlen($whitespace);

if ($this->check('#')) {
$this->consume('#');
$this->consumeUntil("\n");
$this->consumeWhitespace();
}
}

/**
Expand Down
60 changes: 60 additions & 0 deletions src/TwigComponent/tests/Unit/TwigPreLexerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -376,5 +376,65 @@ public static function getLexTests(): iterable
'<twig:foobar bar="baz" {{ ...attr }}>content</twig:foobar>',
'{% component \'foobar\' with { bar: \'baz\', ...attr } %}{% block content %}content{% endblock %}{% endcomponent %}',
];
yield 'component_with_comment_line' => [
"<twig:foo \n # bar \n />",
'{{ component(\'foo\') }}',
];
yield 'component_with_comment_line_between_args' => [
<<<TWIG
<twig:foo
# bar
bar="baz"
/>
TWIG,
'{{ component(\'foo\', { bar: \'baz\' }) }}',
];
yield 'component_with_comment_lines_between_args' => [
<<<TWIG
<twig:foo
# comment
foo="foo"
# comment
bar="bar"
/>
TWIG,
'{{ component(\'foo\', { foo: \'foo\', bar: \'bar\' }) }}',
];
yield 'component_with_comment_line_containing_ending_tag' => [
<<<TWIG
<twig:foo
# comment /></twig:foo>
bar="bar"
/>
TWIG,
'{{ component(\'foo\', { bar: \'bar\' }) }}',
];
yield 'component_with_comment_line_in_argument_value' => [
<<<TWIG
<twig:foo
bar="# bar"
/>
TWIG,
'{{ component(\'foo\', { bar: \'# bar\' }) }}',
];
yield 'component_with_comment_line_in_argument_array_value_is_kept' => [
<<<TWIG
<twig:foo
bar="{{ {
a: 'b',
# comment
c: 'd'
} }}"
/>
TWIG,
// Twig will remove the comment, we don't need to remove it
<<<TWIG
{{ component('foo', { bar: ({
a: 'b',
# comment
c: 'd'
}) }) }}
TWIG,
];
}
}
Loading