Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Introduce short-hand "false" syntax for Blade component props #48084

Merged
merged 1 commit into from
Aug 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/Illuminate/View/Compilers/ComponentTagCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ protected function compileOpeningTags(string $value)
(\:\\\$)(\w+)
)
|
(?:
(![\w]+)
)
|
(?:
[\w\-:.@%]+
(
Expand Down Expand Up @@ -192,6 +196,10 @@ protected function compileSelfClosingTags(string $value)
(\:\\\$)(\w+)
)
|
(?:
(![\w]+)
)
|
(?:
[\w\-:.@%]+
(
Expand Down Expand Up @@ -582,6 +590,7 @@ public function compileSlots(string $value)
protected function getAttributesFromAttributeString(string $attributeString)
{
$attributeString = $this->parseShortAttributeSyntax($attributeString);
$attributeString = $this->parseShortFalseSyntax($attributeString);
$attributeString = $this->parseAttributeBag($attributeString);
$attributeString = $this->parseComponentTagClassStatements($attributeString);
$attributeString = $this->parseComponentTagStyleStatements($attributeString);
Expand Down Expand Up @@ -650,6 +659,21 @@ protected function parseShortAttributeSyntax(string $value)
}, $value);
}

/**
* Parses a short false syntax like !required into a fully-qualified syntax like :required="false".
*
* @param string $value
* @return string
*/
protected function parseShortFalseSyntax(string $value)
{
$pattern = "/\s!(\w+)/x";

return preg_replace_callback($pattern, function (array $matches) {
return " :{$matches[1]}=\"false\"";
}, $value);
}

/**
* Parse the attribute bag in a given attribute string into its fully-qualified syntax.
*
Expand Down
40 changes: 40 additions & 0 deletions tests/View/Blade/BladeComponentTagCompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,31 @@ public function testAttributesTreatedAsPropsAreRemovedFromFinalAttributes()
$this->assertSame($attributes->get('other'), 'ok');
}

public function testFalseShortSyntax()
{
$this->mockViewFactory();
$result = $this->compiler(['bool' => TestBoolComponent::class])->compileTags('<x-bool !bool></x-bool>');

$this->assertSame("##BEGIN-COMPONENT-CLASS##@component('Illuminate\Tests\View\Blade\TestBoolComponent', 'bool', ['bool' => false])
<?php if (isset(\$attributes) && \$attributes instanceof Illuminate\View\ComponentAttributeBag && \$constructor = (new ReflectionClass(Illuminate\Tests\View\Blade\TestBoolComponent::class))->getConstructor()): ?>
<?php \$attributes = \$attributes->except(collect(\$constructor->getParameters())->map->getName()->all()); ?>
<?php endif; ?>
<?php \$component->withAttributes([]); ?> @endComponentClass##END-COMPONENT-CLASS##", trim($result));
}

public function testSelfClosingComponentWithFalseShortSyntax()
{
$this->mockViewFactory();
$result = $this->compiler(['bool' => TestBoolComponent::class])->compileTags('<x-bool !bool />');

$this->assertSame("##BEGIN-COMPONENT-CLASS##@component('Illuminate\Tests\View\Blade\TestBoolComponent', 'bool', ['bool' => false])
<?php if (isset(\$attributes) && \$attributes instanceof Illuminate\View\ComponentAttributeBag && \$constructor = (new ReflectionClass(Illuminate\Tests\View\Blade\TestBoolComponent::class))->getConstructor()): ?>
<?php \$attributes = \$attributes->except(collect(\$constructor->getParameters())->map->getName()->all()); ?>
<?php endif; ?>
<?php \$component->withAttributes([]); ?>
@endComponentClass##END-COMPONENT-CLASS##", trim($result));
}

protected function mockViewFactory($existsSucceeds = true)
{
$container = new Container;
Expand Down Expand Up @@ -797,3 +822,18 @@ public function render()
return 'input';
}
}

class TestBoolComponent extends Component
{
public $bool;

public function __construct($bool)
{
$this->bool = $bool;
}

public function render()
{
return 'bool';
}
}