Skip to content

Commit 73e24aa

Browse files
committed
[TwigComponent] Support ...spread operator with html syntax
1 parent eb7023a commit 73e24aa

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

src/TwigComponent/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGELOG
22

3+
## 2.11.0
4+
5+
- Support ...spread operator with html syntax
6+
37
## 2.9.0
48

59
- The `ComponentAttributes::defaults()` method now accepts any iterable argument.

src/TwigComponent/src/Twig/TwigPreLexer.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ class TwigPreLexer
2222
private int $length;
2323
private int $position = 0;
2424
private int $line;
25-
/** @var array<string: name, bool: hasDefaultBlock> */
25+
/**
26+
* @var list<array{name: string, hasDefaultBlock: bool}>
27+
*/
2628
private array $currentComponents = [];
2729

2830
public function __construct(int $startingLine = 1)
@@ -201,6 +203,15 @@ private function consumeAttributes(string $componentName): string
201203
break;
202204
}
203205

206+
if ($this->check('{{...') || $this->check('{{ ...')) {
207+
$this->consume('{{...');
208+
$this->consume('{{ ...');
209+
$attributes[] = '...'.trim($this->consumeUntil('}}'));
210+
$this->consume('}}');
211+
212+
continue;
213+
}
214+
204215
$isAttributeDynamic = false;
205216

206217
// :someProp="dynamicVar"

src/TwigComponent/tests/Unit/TwigPreLexerTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,5 +216,31 @@ public function getLexTests(): iterable
216216
'{% verbatim %}<twig:Alert/>{% endverbatim %}',
217217
'{% verbatim %}<twig:Alert/>{% endverbatim %}',
218218
];
219+
220+
yield 'component_attr_spreading_self_closing' => [
221+
'<twig:foobar bar="baz"{{...attr}}/>',
222+
'{{ component(\'foobar\', { bar: \'baz\', ...attr }) }}',
223+
];
224+
yield 'component_attr_spreading_self_closing2' => [
225+
'<twig:foobar bar="baz"{{ ...customAttrs }} />',
226+
'{{ component(\'foobar\', { bar: \'baz\', ...customAttrs }) }}',
227+
];
228+
yield 'component_attr_spreading_self_closing3' => [
229+
'<twig:foobar bar="baz" {{...attr }} />',
230+
'{{ component(\'foobar\', { bar: \'baz\', ...attr }) }}',
231+
];
232+
233+
yield 'component_attr_spreading_with_content1' => [
234+
'<twig:foobar bar="baz"{{...attr}}>content</twig:foobar>',
235+
'{% component \'foobar\' with { bar: \'baz\', ...attr } %}{% block content %}content{% endblock %}{% endcomponent %}',
236+
];
237+
yield 'component_attr_spreading_with_content2' => [
238+
'<twig:foobar bar="baz"{{ ...customAttrs }}>content</twig:foobar>',
239+
'{% component \'foobar\' with { bar: \'baz\', ...customAttrs } %}{% block content %}content{% endblock %}{% endcomponent %}',
240+
];
241+
yield 'component_attr_spreading_with_content3' => [
242+
'<twig:foobar bar="baz" {{ ...attr }}>content</twig:foobar>',
243+
'{% component \'foobar\' with { bar: \'baz\', ...attr } %}{% block content %}content{% endblock %}{% endcomponent %}',
244+
];
219245
}
220246
}

0 commit comments

Comments
 (0)