Skip to content

Commit ce7f1bf

Browse files
matheoweaverryan
authored andcommitted
Fix error for component name with :
1 parent f99cc2a commit ce7f1bf

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

src/TwigComponent/src/Twig/TwigPreLexer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function preLexComponents(string $input): string
5858
$this->currentComponents[] = ['name' => $componentName, 'hasDefaultBlock' => false];
5959
}
6060

61-
$output .= "{% component {$componentName}".($attributes ? " with { {$attributes} }" : '').' %}';
61+
$output .= "{% component '{$componentName}'".($attributes ? " with { {$attributes} }" : '').' %}';
6262
if ($isSelfClosing) {
6363
$output .= '{% endcomponent %}';
6464
}
@@ -117,7 +117,7 @@ public function preLexComponents(string $input): string
117117
private function consumeComponentName(): string
118118
{
119119
$start = $this->position;
120-
while ($this->position < $this->length && preg_match('/[A-Za-z0-9_]/', $this->input[$this->position])) {
120+
while ($this->position < $this->length && preg_match('/[A-Za-z0-9_:@\-\/.]/', $this->input[$this->position])) {
121121
++$this->position;
122122
}
123123
$componentName = substr($this->input, $start, $this->position - $start);

src/TwigComponent/tests/Unit/TwigPreLexerTest.php

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,47 +29,67 @@ public function getLexTests(): iterable
2929
{
3030
yield 'simple_component' => [
3131
'<twig:foo />',
32-
'{% component foo %}{% endcomponent %}',
32+
'{% component \'foo\' %}{% endcomponent %}',
3333
];
3434

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

4040
yield 'component_with_dynamic_attributes' => [
4141
'<twig:foo dynamic="{{ dynamicVar }}" :otherDynamic="anotherVar" />',
42-
'{% component foo with { dynamic: dynamicVar, otherDynamic: anotherVar } %}{% endcomponent %}',
42+
'{% component \'foo\' with { dynamic: dynamicVar, otherDynamic: anotherVar } %}{% endcomponent %}',
4343
];
4444

4545
yield 'component_with_closing_tag' => [
4646
'<twig:foo></twig:foo>',
47-
'{% component foo %}{% endcomponent %}',
47+
'{% component \'foo\' %}{% endcomponent %}',
4848
];
4949

5050
yield 'component_with_block' => [
5151
'<twig:foo><twig:block name="foo_block">Foo</twig:block></twig:foo>',
52-
'{% component foo %}{% block foo_block %}Foo{% endblock %}{% endcomponent %}',
52+
'{% component \'foo\' %}{% block foo_block %}Foo{% endblock %}{% endcomponent %}',
5353
];
5454

5555
yield 'component_with_embedded_component_inside_block' => [
5656
'<twig:foo><twig:block name="foo_block"><twig:bar /></twig:block></twig:foo>',
57-
'{% component foo %}{% block foo_block %}{% component bar %}{% endcomponent %}{% endblock %}{% endcomponent %}',
57+
'{% component \'foo\' %}{% block foo_block %}{% component \'bar\' %}{% endcomponent %}{% endblock %}{% endcomponent %}',
5858
];
5959

6060
yield 'attribute_with_no_value' => [
6161
'<twig:foo bar />',
62-
'{% component foo with { bar: true } %}{% endcomponent %}',
62+
'{% component \'foo\' with { bar: true } %}{% endcomponent %}',
6363
];
6464

6565
yield 'component_with_default_block_content' => [
6666
'<twig:foo>Foo</twig:foo>',
67-
'{% component foo %}{% block content %}Foo{% endblock %}{% endcomponent %}',
67+
'{% component \'foo\' %}{% block content %}Foo{% endblock %}{% endcomponent %}',
6868
];
6969

7070
yield 'component_with_default_block_that_holds_a_component_and_multi_blocks' => [
7171
'<twig:foo>Foo <twig:bar /><twig:block name="other_block">Other block</twig:block></twig:foo>',
72-
'{% component foo %}{% block content %}Foo {% component bar %}{% endcomponent %}{% endblock %}{% block other_block %}Other block{% endblock %}{% endcomponent %}',
72+
'{% component \'foo\' %}{% block content %}Foo {% component \'bar\' %}{% endcomponent %}{% endblock %}{% block other_block %}Other block{% endblock %}{% endcomponent %}',
73+
];
74+
yield 'component_with_character_:_on_his_name' => [
75+
'<twig:foo:bar></twig:foo:bar>',
76+
'{% component \'foo:bar\' %}{% endcomponent %}',
77+
];
78+
yield 'component_with_character_@_on_his_name' => [
79+
'<twig:@foo></twig:@foo>',
80+
'{% component \'@foo\' %}{% endcomponent %}',
81+
];
82+
yield 'component_with_character_-_on_his_name' => [
83+
'<twig:foo-bar></twig:foo-bar>',
84+
'{% component \'foo-bar\' %}{% endcomponent %}',
85+
];
86+
yield 'component_with_character_/_on_his_name' => [
87+
'<twig:foo/bar></twig:foo/bar>',
88+
'{% component \'foo/bar\' %}{% endcomponent %}',
89+
];
90+
yield 'component_with_character_._on_his_name' => [
91+
'<twig:foo.bar></twig:foo.bar>',
92+
'{% component \'foo.bar\' %}{% endcomponent %}',
7393
];
7494
}
7595
}

0 commit comments

Comments
 (0)