Skip to content
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
32 changes: 32 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 @@ -597,6 +605,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 @@ -665,6 +674,29 @@ 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)
{
$parts = preg_split('/(".*?(?<!\\\\)")/', $value, -1, PREG_SPLIT_DELIM_CAPTURE);

return (new Collection($parts))
->map(function (string $value) {
if (preg_match('/^".*"$/s', $value)) {
return $value;
}

return preg_replace_callback('/!(\w+)/', function ($matches) {
return " :{$matches[1]}=\"false\"";
}, $value);
})
->implode('');
}

/**
* Parse the attribute bag in a given attribute string into its fully-qualified syntax.
*
Expand Down
90 changes: 90 additions & 0 deletions tests/View/Blade/BladeComponentTagCompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,81 @@ public function testSelfClosingComponentWithColonDataMultipleAttributesAndStatic
'@endComponentClass##END-COMPONENT-CLASS##', trim($result));
}

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): ?>
<?php \$attributes = \$attributes->except(\Illuminate\Tests\View\Blade\TestBoolComponent::ignoredParameterNames()); ?>
<?php endif; ?>
<?php \$component->withAttributes([]); ?> @endComponentClass##END-COMPONENT-CLASS##", trim($result));
}

public function testFalseShortSyntaxAsValue()
{
$this->mockViewFactory();
$result = $this->compiler(['bool' => TestBoolComponent::class])->compileTags('<x-bool :bool="!false"></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): ?>
<?php \$attributes = \$attributes->except(\Illuminate\Tests\View\Blade\TestBoolComponent::ignoredParameterNames()); ?>
<?php endif; ?>
<?php \$component->withAttributes([]); ?> @endComponentClass##END-COMPONENT-CLASS##", trim($result));
}

public function testFalseShortSyntaxWithinValue()
{
$this->mockViewFactory();
$result = $this->compiler(['bool' => TestBoolComponent::class])->compileTags('<x-bool :bool="$value && !old(\'value\')"></x-bool>');

$this->assertSame("##BEGIN-COMPONENT-CLASS##@component('Illuminate\Tests\View\Blade\TestBoolComponent', 'bool', ['bool' => \$value && !old('value')])
<?php if (isset(\$attributes) && \$attributes instanceof Illuminate\View\ComponentAttributeBag): ?>
<?php \$attributes = \$attributes->except(\Illuminate\Tests\View\Blade\TestBoolComponent::ignoredParameterNames()); ?>
<?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): ?>
<?php \$attributes = \$attributes->except(\Illuminate\Tests\View\Blade\TestBoolComponent::ignoredParameterNames()); ?>
<?php endif; ?>
<?php \$component->withAttributes([]); ?>\n".
'@endComponentClass##END-COMPONENT-CLASS##', trim($result));
}

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

$this->assertSame("##BEGIN-COMPONENT-CLASS##@component('Illuminate\Tests\View\Blade\TestBoolComponent', 'bool', ['bool' => !false])
<?php if (isset(\$attributes) && \$attributes instanceof Illuminate\View\ComponentAttributeBag): ?>
<?php \$attributes = \$attributes->except(\Illuminate\Tests\View\Blade\TestBoolComponent::ignoredParameterNames()); ?>
<?php endif; ?>
<?php \$component->withAttributes([]); ?>\n".
'@endComponentClass##END-COMPONENT-CLASS##', trim($result));
}

public function testSelfClosingComponentWithFalseShortSyntaxWithinValue()
{
$this->mockViewFactory();
$result = $this->compiler(['bool' => TestBoolComponent::class])->compileTags('<x-bool :bool="$value && !old(\'value\')" />');

$this->assertSame("##BEGIN-COMPONENT-CLASS##@component('Illuminate\Tests\View\Blade\TestBoolComponent', 'bool', ['bool' => \$value && !old('value')])
<?php if (isset(\$attributes) && \$attributes instanceof Illuminate\View\ComponentAttributeBag): ?>
<?php \$attributes = \$attributes->except(\Illuminate\Tests\View\Blade\TestBoolComponent::ignoredParameterNames()); ?>
<?php endif; ?>
<?php \$component->withAttributes([]); ?>\n".
'@endComponentClass##END-COMPONENT-CLASS##', trim($result));
}

public function testEscapedColonAttribute()
{
$this->mockViewFactory();
Expand Down Expand Up @@ -1010,6 +1085,21 @@ public function render()
}
}

class TestBoolComponent extends Component
{
public $bool;

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

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

class TestContainerComponent extends Component
{
public function render()
Expand Down