Skip to content

Commit

Permalink
[TwigComponent] Support ...spread operator with html syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
norkunas committed Jul 27, 2023
1 parent eb7023a commit 73e24aa
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/TwigComponent/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## 2.11.0

- Support ...spread operator with html syntax

## 2.9.0

- The `ComponentAttributes::defaults()` method now accepts any iterable argument.
Expand Down
13 changes: 12 additions & 1 deletion src/TwigComponent/src/Twig/TwigPreLexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ class TwigPreLexer
private int $length;
private int $position = 0;
private int $line;
/** @var array<string: name, bool: hasDefaultBlock> */
/**
* @var list<array{name: string, hasDefaultBlock: bool}>
*/
private array $currentComponents = [];

public function __construct(int $startingLine = 1)
Expand Down Expand Up @@ -201,6 +203,15 @@ private function consumeAttributes(string $componentName): string
break;
}

if ($this->check('{{...') || $this->check('{{ ...')) {
$this->consume('{{...');
$this->consume('{{ ...');
$attributes[] = '...'.trim($this->consumeUntil('}}'));
$this->consume('}}');

continue;
}

$isAttributeDynamic = false;

// :someProp="dynamicVar"
Expand Down
26 changes: 26 additions & 0 deletions src/TwigComponent/tests/Unit/TwigPreLexerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,5 +216,31 @@ public function getLexTests(): iterable
'{% verbatim %}<twig:Alert/>{% endverbatim %}',
'{% verbatim %}<twig:Alert/>{% endverbatim %}',
];

yield 'component_attr_spreading_self_closing' => [
'<twig:foobar bar="baz"{{...attr}}/>',
'{{ component(\'foobar\', { bar: \'baz\', ...attr }) }}',
];
yield 'component_attr_spreading_self_closing2' => [
'<twig:foobar bar="baz"{{ ...customAttrs }} />',
'{{ component(\'foobar\', { bar: \'baz\', ...customAttrs }) }}',
];
yield 'component_attr_spreading_self_closing3' => [
'<twig:foobar bar="baz" {{...attr }} />',
'{{ component(\'foobar\', { bar: \'baz\', ...attr }) }}',
];

yield 'component_attr_spreading_with_content1' => [
'<twig:foobar bar="baz"{{...attr}}>content</twig:foobar>',
'{% component \'foobar\' with { bar: \'baz\', ...attr } %}{% block content %}content{% endblock %}{% endcomponent %}',
];
yield 'component_attr_spreading_with_content2' => [
'<twig:foobar bar="baz"{{ ...customAttrs }}>content</twig:foobar>',
'{% component \'foobar\' with { bar: \'baz\', ...customAttrs } %}{% block content %}content{% endblock %}{% endcomponent %}',
];
yield 'component_attr_spreading_with_content3' => [
'<twig:foobar bar="baz" {{ ...attr }}>content</twig:foobar>',
'{% component \'foobar\' with { bar: \'baz\', ...attr } %}{% block content %}content{% endblock %}{% endcomponent %}',
];
}
}

0 comments on commit 73e24aa

Please sign in to comment.