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

[7.x] Fix setting component name and attributes #32599

Merged
merged 4 commits into from
Apr 29, 2020
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
3 changes: 1 addition & 2 deletions src/Illuminate/View/Compilers/ComponentTagCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,7 @@ protected function componentString(string $component, array $attributes)
$parameters = $data->all();
}

return " @component('{$class}', [".$this->attributesToString($parameters, $escapeBound = false).'])
<?php $component->withName(\''.$component.'\'); ?>
return " @component('{$class}', '{$component}', [".$this->attributesToString($parameters, $escapeBound = false).'])
<?php $component->withAttributes(['.$this->attributesToString($attributes->all()).']); ?>';
}

Expand Down
12 changes: 7 additions & 5 deletions src/Illuminate/View/Compilers/Concerns/CompilesComponents.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ trait CompilesComponents
*/
protected function compileComponent($expression)
{
[$component, $data] = strpos($expression, ',') !== false
? array_map('trim', explode(',', trim($expression, '()'), 2))
: [trim($expression, '()'), ''];
[$component, $alias, $data] = strpos($expression, ',') !== false
? array_map('trim', explode(',', trim($expression, '()'), 3)) + ['', '', '']
: [trim($expression, '()'), '', ''];

$component = trim($component, '\'"');

$hash = static::newComponentHash($component);

if (Str::contains($component, ['::class', '\\'])) {
return static::compileClassComponentOpening($component, $data, $hash);
return static::compileClassComponentOpening($component, $data, $hash, $alias);
}

return "<?php \$__env->startComponent{$expression}; ?>";
Expand All @@ -53,15 +53,17 @@ public static function newComponentHash(string $component)
* Compile a class component opening.
*
* @param string $component
* @param string $alias
* @param string $data
* @param string $hash
* @return string
*/
public static function compileClassComponentOpening(string $component, string $data, string $hash)
public static function compileClassComponentOpening(string $component, string $data, string $hash, string $alias)
{
return implode("\n", [
'<?php if (isset($component)) { $__componentOriginal'.$hash.' = $component; } ?>',
'<?php $component = $__env->getContainer()->make('.Str::finish($component, '::class').', '.($data ?: '[]').'); ?>',
'<?php $component->withName('.$alias.'); ?>',
'<?php if ($component->shouldRender()): ?>',
'<?php $__env->startComponent($component->resolveView(), $component->data()); ?>',
]);
Expand Down
48 changes: 16 additions & 32 deletions tests/View/Blade/BladeComponentTagCompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,9 @@ public function testBasicComponentParsing()

$result = $this->compiler(['alert' => TestAlertComponent::class])->compileTags('<div><x-alert type="foo" limit="5" @click="foo" wire:click="changePlan(\'{{ $plan }}\')" required /><x-alert /></div>');

$this->assertSame("<div> @component('Illuminate\Tests\View\Blade\TestAlertComponent', [])
<?php \$component->withName('alert'); ?>
$this->assertSame("<div> @component('Illuminate\Tests\View\Blade\TestAlertComponent', 'alert', [])
<?php \$component->withAttributes(['type' => 'foo','limit' => '5','@click' => 'foo','wire:click' => 'changePlan(\''.e(\$plan).'\')','required' => true]); ?>\n".
"@endcomponentClass @component('Illuminate\Tests\View\Blade\TestAlertComponent', [])
<?php \$component->withName('alert'); ?>
"@endcomponentClass @component('Illuminate\Tests\View\Blade\TestAlertComponent', 'alert', [])
<?php \$component->withAttributes([]); ?>\n".
'@endcomponentClass </div>', trim($result));
}
Expand All @@ -45,8 +43,7 @@ public function testBasicComponentWithEmptyAttributesParsing()
{
$result = $this->compiler(['alert' => TestAlertComponent::class])->compileTags('<div><x-alert type="" limit=\'\' @click="" required /></div>');

$this->assertSame("<div> @component('Illuminate\Tests\View\Blade\TestAlertComponent', [])
<?php \$component->withName('alert'); ?>
$this->assertSame("<div> @component('Illuminate\Tests\View\Blade\TestAlertComponent', 'alert', [])
<?php \$component->withAttributes(['type' => '','limit' => '','@click' => '','required' => true]); ?>\n".
'@endcomponentClass </div>', trim($result));
}
Expand All @@ -55,53 +52,47 @@ public function testDataCamelCasing()
{
$result = $this->compiler(['profile' => TestProfileComponent::class])->compileTags('<x-profile user-id="1"></x-profile>');

$this->assertSame("@component('Illuminate\Tests\View\Blade\TestProfileComponent', ['userId' => '1'])
<?php \$component->withName('profile'); ?>
$this->assertSame("@component('Illuminate\Tests\View\Blade\TestProfileComponent', 'profile', ['userId' => '1'])
<?php \$component->withAttributes([]); ?> @endcomponentClass", trim($result));
}

public function testColonData()
{
$result = $this->compiler(['profile' => TestProfileComponent::class])->compileTags('<x-profile :user-id="1"></x-profile>');

$this->assertSame("@component('Illuminate\Tests\View\Blade\TestProfileComponent', ['userId' => 1])
<?php \$component->withName('profile'); ?>
$this->assertSame("@component('Illuminate\Tests\View\Blade\TestProfileComponent', 'profile', ['userId' => 1])
<?php \$component->withAttributes([]); ?> @endcomponentClass", trim($result));
}

public function testColonAttributesIsEscapedIfStrings()
{
$result = $this->compiler(['profile' => TestProfileComponent::class])->compileTags('<x-profile :src="\'foo\'"></x-profile>');

$this->assertSame("@component('Illuminate\Tests\View\Blade\TestProfileComponent', [])
<?php \$component->withName('profile'); ?>
$this->assertSame("@component('Illuminate\Tests\View\Blade\TestProfileComponent', 'profile', [])
<?php \$component->withAttributes(['src' => \Illuminate\View\Compilers\BladeCompiler::sanitizeComponentAttribute('foo')]); ?> @endcomponentClass", trim($result));
}

public function testColonNestedComponentParsing()
{
$result = $this->compiler(['foo:alert' => TestAlertComponent::class])->compileTags('<x-foo:alert></x-foo:alert>');

$this->assertSame("@component('Illuminate\Tests\View\Blade\TestAlertComponent', [])
<?php \$component->withName('foo:alert'); ?>
$this->assertSame("@component('Illuminate\Tests\View\Blade\TestAlertComponent', 'foo:alert', [])
<?php \$component->withAttributes([]); ?> @endcomponentClass", trim($result));
}

public function testColonStartingNestedComponentParsing()
{
$result = $this->compiler(['foo:alert' => TestAlertComponent::class])->compileTags('<x:foo:alert></x-foo:alert>');

$this->assertSame("@component('Illuminate\Tests\View\Blade\TestAlertComponent', [])
<?php \$component->withName('foo:alert'); ?>
$this->assertSame("@component('Illuminate\Tests\View\Blade\TestAlertComponent', 'foo:alert', [])
<?php \$component->withAttributes([]); ?> @endcomponentClass", trim($result));
}

public function testSelfClosingComponentsCanBeCompiled()
{
$result = $this->compiler(['alert' => TestAlertComponent::class])->compileTags('<div><x-alert/></div>');

$this->assertSame("<div> @component('Illuminate\Tests\View\Blade\TestAlertComponent', [])
<?php \$component->withName('alert'); ?>
$this->assertSame("<div> @component('Illuminate\Tests\View\Blade\TestAlertComponent', 'alert', [])
<?php \$component->withAttributes([]); ?>\n".
'@endcomponentClass </div>', trim($result));
}
Expand Down Expand Up @@ -140,8 +131,7 @@ public function testComponentsCanBeCompiledWithHyphenAttributes()

$result = $this->compiler(['alert' => TestAlertComponent::class])->compileTags('<x-alert class="bar" wire:model="foo" x-on:click="bar" @click="baz" />');

$this->assertSame("@component('Illuminate\Tests\View\Blade\TestAlertComponent', [])
<?php \$component->withName('alert'); ?>
$this->assertSame("@component('Illuminate\Tests\View\Blade\TestAlertComponent', 'alert', [])
<?php \$component->withAttributes(['class' => 'bar','wire:model' => 'foo','x-on:click' => 'bar','@click' => 'baz']); ?>\n".
'@endcomponentClass', trim($result));
}
Expand All @@ -150,8 +140,7 @@ public function testSelfClosingComponentsCanBeCompiledWithDataAndAttributes()
{
$result = $this->compiler(['alert' => TestAlertComponent::class])->compileTags('<x-alert title="foo" class="bar" wire:model="foo" />');

$this->assertSame("@component('Illuminate\Tests\View\Blade\TestAlertComponent', ['title' => 'foo'])
<?php \$component->withName('alert'); ?>
$this->assertSame("@component('Illuminate\Tests\View\Blade\TestAlertComponent', 'alert', ['title' => 'foo'])
<?php \$component->withAttributes(['class' => 'bar','wire:model' => 'foo']); ?>\n".
'@endcomponentClass', trim($result));
}
Expand All @@ -160,17 +149,15 @@ public function testComponentsCanHaveAttachedWord()
{
$result = $this->compiler(['profile' => TestProfileComponent::class])->compileTags('<x-profile></x-profile>Words');

$this->assertSame("@component('Illuminate\Tests\View\Blade\TestProfileComponent', [])
<?php \$component->withName('profile'); ?>
$this->assertSame("@component('Illuminate\Tests\View\Blade\TestProfileComponent', 'profile', [])
<?php \$component->withAttributes([]); ?> @endcomponentClass Words", trim($result));
}

public function testSelfClosingComponentsCanHaveAttachedWord()
{
$result = $this->compiler(['alert' => TestAlertComponent::class])->compileTags('<x-alert/>Words');

$this->assertSame("@component('Illuminate\Tests\View\Blade\TestAlertComponent', [])
<?php \$component->withName('alert'); ?>
$this->assertSame("@component('Illuminate\Tests\View\Blade\TestAlertComponent', 'alert', [])
<?php \$component->withAttributes([]); ?>\n".
'@endcomponentClass Words', trim($result));
}
Expand All @@ -179,8 +166,7 @@ public function testSelfClosingComponentsCanBeCompiledWithBoundData()
{
$result = $this->compiler(['alert' => TestAlertComponent::class])->compileTags('<x-alert :title="$title" class="bar" />');

$this->assertSame("@component('Illuminate\Tests\View\Blade\TestAlertComponent', ['title' => \$title])
<?php \$component->withName('alert'); ?>
$this->assertSame("@component('Illuminate\Tests\View\Blade\TestAlertComponent', 'alert', ['title' => \$title])
<?php \$component->withAttributes(['class' => 'bar']); ?>\n".
'@endcomponentClass', trim($result));
}
Expand All @@ -190,8 +176,7 @@ public function testPairedComponentTags()
$result = $this->compiler(['alert' => TestAlertComponent::class])->compileTags('<x-alert>
</x-alert>');

$this->assertSame("@component('Illuminate\Tests\View\Blade\TestAlertComponent', [])
<?php \$component->withName('alert'); ?>
$this->assertSame("@component('Illuminate\Tests\View\Blade\TestAlertComponent', 'alert', [])
<?php \$component->withAttributes([]); ?>
@endcomponentClass", trim($result));
}
Expand All @@ -207,8 +192,7 @@ public function testClasslessComponents()

$result = $this->compiler()->compileTags('<x-anonymous-component :name="\'Taylor\'" :age="31" wire:model="foo" />');

$this->assertSame("@component('Illuminate\View\AnonymousComponent', ['view' => 'components.anonymous-component','data' => ['name' => 'Taylor','age' => 31,'wire:model' => 'foo']])
<?php \$component->withName('anonymous-component'); ?>
$this->assertSame("@component('Illuminate\View\AnonymousComponent', 'anonymous-component', ['view' => 'components.anonymous-component','data' => ['name' => 'Taylor','age' => 31,'wire:model' => 'foo']])
<?php \$component->withAttributes(['name' => \Illuminate\View\Compilers\BladeCompiler::sanitizeComponentAttribute('Taylor'),'age' => 31,'wire:model' => 'foo']); ?>\n".
'@endcomponentClass', trim($result));
}
Expand Down
8 changes: 2 additions & 6 deletions tests/View/Blade/BladeComponentsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,13 @@ public function testComponentsAreCompiled()
$this->assertSame('<?php $__env->startComponent(\'foo\'); ?>', $this->compiler->compileString('@component(\'foo\')'));
}

public function testExtraAttributesCanBePassedToComponents()
{
$this->assertSame('<?php $__env->startComponent(\'foo\', ["foo" => "bar"], ["foo" => "bar"]); ?>', $this->compiler->compileString('@component(\'foo\', ["foo" => "bar"], ["foo" => "bar"])'));
}

public function testClassComponentsAreCompiled()
{
$this->assertSame('<?php if (isset($component)) { $__componentOriginal35bda42cbf6f9717b161c4f893644ac7a48b0d98 = $component; } ?>
<?php $component = $__env->getContainer()->make(Test::class, ["foo" => "bar"]); ?>
<?php $component->withName(\'test\'); ?>
<?php if ($component->shouldRender()): ?>
<?php $__env->startComponent($component->resolveView(), $component->data()); ?>', $this->compiler->compileString('@component(\'Test::class\', ["foo" => "bar"])'));
<?php $__env->startComponent($component->resolveView(), $component->data()); ?>', $this->compiler->compileString('@component(\'Test::class\', \'test\', ["foo" => "bar"])'));
}

public function testEndComponentsAreCompiled()
Expand Down