Skip to content

Commit fa0b45c

Browse files
[9.x] Short attribute syntax for Self Closing Blade Components (#44413)
* Short attribute syntax for Self Closing Blade Components * Fix test
1 parent 031b7d1 commit fa0b45c

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

src/Illuminate/View/Compilers/ComponentTagCompiler.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,10 @@ protected function compileSelfClosingTags(string $value)
180180
\{\{\s*\\\$attributes(?:[^}]+?)?\s*\}\}
181181
)
182182
|
183+
(?:
184+
(\:\\\$)(\w+)
185+
)
186+
|
183187
(?:
184188
[\w\-:.@]+
185189
(

tests/View/Blade/BladeComponentTagCompilerTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,18 @@ public function testColonDataShortSyntax()
148148
<?php \$component->withAttributes([]); ?> @endComponentClass##END-COMPONENT-CLASS##", trim($result));
149149
}
150150

151+
public function testSelfClosingComponentWithColonDataShortSyntax()
152+
{
153+
$result = $this->compiler(['profile' => TestProfileComponent::class])->compileTags('<x-profile :$userId/>');
154+
155+
$this->assertSame("##BEGIN-COMPONENT-CLASS##@component('Illuminate\Tests\View\Blade\TestProfileComponent', 'profile', ['userId' => \$userId])
156+
<?php if (isset(\$attributes) && \$constructor = (new ReflectionClass(Illuminate\Tests\View\Blade\TestProfileComponent::class))->getConstructor()): ?>
157+
<?php \$attributes = \$attributes->except(collect(\$constructor->getParameters())->map->getName()->all()); ?>
158+
<?php endif; ?>
159+
<?php \$component->withAttributes([]); ?>\n".
160+
'@endComponentClass##END-COMPONENT-CLASS##', trim($result));
161+
}
162+
151163
public function testEscapedColonAttribute()
152164
{
153165
$result = $this->compiler(['profile' => TestProfileComponent::class])->compileTags('<x-profile :user-id="1" ::title="user.name"></x-profile>');

0 commit comments

Comments
 (0)