Skip to content

Fix error for component name with : #806

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 19, 2023
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: 2 additions & 2 deletions src/TwigComponent/src/Twig/TwigPreLexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function preLexComponents(string $input): string
$this->currentComponents[] = ['name' => $componentName, 'hasDefaultBlock' => false];
}

$output .= "{% component {$componentName}".($attributes ? " with { {$attributes} }" : '').' %}';
$output .= "{% component '{$componentName}'".($attributes ? " with { {$attributes} }" : '').' %}';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does {% component @foo/bar-baz.html.twig %} (no ') not work?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes It works with both

if ($isSelfClosing) {
$output .= '{% endcomponent %}';
}
Expand Down Expand Up @@ -117,7 +117,7 @@ public function preLexComponents(string $input): string
private function consumeComponentName(): string
{
$start = $this->position;
while ($this->position < $this->length && preg_match('/[A-Za-z0-9_]/', $this->input[$this->position])) {
while ($this->position < $this->length && preg_match('/[A-Za-z0-9_:@\-\/.]/', $this->input[$this->position])) {
++$this->position;
}
$componentName = substr($this->input, $start, $this->position - $start);
Expand Down
38 changes: 29 additions & 9 deletions src/TwigComponent/tests/Unit/TwigPreLexerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,47 +29,67 @@ public function getLexTests(): iterable
{
yield 'simple_component' => [
'<twig:foo />',
'{% component foo %}{% endcomponent %}',
'{% component \'foo\' %}{% endcomponent %}',
];

yield 'component_with_attributes' => [
'<twig:foo bar="baz" with_quotes="It\'s with quotes" />',
"{% component foo with { bar: 'baz', with_quotes: 'It\'s with quotes' } %}{% endcomponent %}",
"{% component 'foo' with { bar: 'baz', with_quotes: 'It\'s with quotes' } %}{% endcomponent %}",
];

yield 'component_with_dynamic_attributes' => [
'<twig:foo dynamic="{{ dynamicVar }}" :otherDynamic="anotherVar" />',
'{% component foo with { dynamic: dynamicVar, otherDynamic: anotherVar } %}{% endcomponent %}',
'{% component \'foo\' with { dynamic: dynamicVar, otherDynamic: anotherVar } %}{% endcomponent %}',
];

yield 'component_with_closing_tag' => [
'<twig:foo></twig:foo>',
'{% component foo %}{% endcomponent %}',
'{% component \'foo\' %}{% endcomponent %}',
];

yield 'component_with_block' => [
'<twig:foo><twig:block name="foo_block">Foo</twig:block></twig:foo>',
'{% component foo %}{% block foo_block %}Foo{% endblock %}{% endcomponent %}',
'{% component \'foo\' %}{% block foo_block %}Foo{% endblock %}{% endcomponent %}',
];

yield 'component_with_embedded_component_inside_block' => [
'<twig:foo><twig:block name="foo_block"><twig:bar /></twig:block></twig:foo>',
'{% component foo %}{% block foo_block %}{% component bar %}{% endcomponent %}{% endblock %}{% endcomponent %}',
'{% component \'foo\' %}{% block foo_block %}{% component \'bar\' %}{% endcomponent %}{% endblock %}{% endcomponent %}',
];

yield 'attribute_with_no_value' => [
'<twig:foo bar />',
'{% component foo with { bar: true } %}{% endcomponent %}',
'{% component \'foo\' with { bar: true } %}{% endcomponent %}',
];

yield 'component_with_default_block_content' => [
'<twig:foo>Foo</twig:foo>',
'{% component foo %}{% block content %}Foo{% endblock %}{% endcomponent %}',
'{% component \'foo\' %}{% block content %}Foo{% endblock %}{% endcomponent %}',
];

yield 'component_with_default_block_that_holds_a_component_and_multi_blocks' => [
'<twig:foo>Foo <twig:bar /><twig:block name="other_block">Other block</twig:block></twig:foo>',
'{% component foo %}{% block content %}Foo {% component bar %}{% endcomponent %}{% endblock %}{% block other_block %}Other block{% endblock %}{% endcomponent %}',
'{% component \'foo\' %}{% block content %}Foo {% component \'bar\' %}{% endcomponent %}{% endblock %}{% block other_block %}Other block{% endblock %}{% endcomponent %}',
];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add tests specifically for any of the new character we allow - e.g. <twig:foo:bar or <twig:foo-bar, etc

yield 'component_with_character_:_on_his_name' => [
'<twig:foo:bar></twig:foo:bar>',
'{% component \'foo:bar\' %}{% endcomponent %}',
];
yield 'component_with_character_@_on_his_name' => [
'<twig:@foo></twig:@foo>',
'{% component \'@foo\' %}{% endcomponent %}',
];
yield 'component_with_character_-_on_his_name' => [
'<twig:foo-bar></twig:foo-bar>',
'{% component \'foo-bar\' %}{% endcomponent %}',
];
yield 'component_with_character_/_on_his_name' => [
'<twig:foo/bar></twig:foo/bar>',
'{% component \'foo/bar\' %}{% endcomponent %}',
];
yield 'component_with_character_._on_his_name' => [
'<twig:foo.bar></twig:foo.bar>',
'{% component \'foo.bar\' %}{% endcomponent %}',
];
}
}