Skip to content

Commit b8595b2

Browse files
authored
[9.x] Add inline slot name syntax (<x-slot:name>) (#40747)
* Add slot inline naming syntax * Fix tests * Fix tests
1 parent dfc5270 commit b8595b2

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

src/Illuminate/View/Compilers/ComponentTagCompiler.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -406,8 +406,8 @@ public function compileSlots(string $value)
406406
<
407407
\s*
408408
x[\-\:]slot
409-
\s+
410-
(:?)name=(?<name>(\"[^\"]+\"|\\\'[^\\\']+\\\'|[^\s>]+))
409+
(?:\:(?<inlineName>\w+))?
410+
(?:\s+(:?)name=(?<name>(\"[^\"]+\"|\\\'[^\\\']+\\\'|[^\s>]+)))?
411411
(?<attributes>
412412
(?:
413413
\s+
@@ -438,9 +438,9 @@ public function compileSlots(string $value)
438438
/x";
439439

440440
$value = preg_replace_callback($pattern, function ($matches) {
441-
$name = $this->stripQuotes($matches['name']);
441+
$name = $this->stripQuotes($matches['inlineName'] ?: $matches['name']);
442442

443-
if ($matches[1] !== ':') {
443+
if ($matches[2] !== ':') {
444444
$name = "'{$name}'";
445445
}
446446

tests/View/Blade/BladeComponentTagCompilerTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ public function testSlotsCanBeCompiled()
2828
$this->assertSame("@slot('foo', null, []) \n".' @endslot', trim($result));
2929
}
3030

31+
public function testInlineSlotsCanBeCompiled()
32+
{
33+
$result = $this->compiler()->compileSlots('<x-slot:foo>
34+
</x-slot>');
35+
36+
$this->assertSame("@slot('foo', null, []) \n".' @endslot', trim($result));
37+
}
38+
3139
public function testDynamicSlotsCanBeCompiled()
3240
{
3341
$result = $this->compiler()->compileSlots('<x-slot :name="$foo">
@@ -44,6 +52,14 @@ public function testSlotsWithAttributesCanBeCompiled()
4452
$this->assertSame("@slot('foo', null, ['class' => 'font-bold']) \n".' @endslot', trim($result));
4553
}
4654

55+
public function testInlineSlotsWithAttributesCanBeCompiled()
56+
{
57+
$result = $this->compiler()->compileSlots('<x-slot:foo class="font-bold">
58+
</x-slot>');
59+
60+
$this->assertSame("@slot('foo', null, ['class' => 'font-bold']) \n".' @endslot', trim($result));
61+
}
62+
4763
public function testSlotsWithDynamicAttributesCanBeCompiled()
4864
{
4965
$result = $this->compiler()->compileSlots('<x-slot name="foo" :class="$classes">

0 commit comments

Comments
 (0)