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] Compile Echos Within Blade Component Attributes #32558

Merged
merged 4 commits into from
Apr 28, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
evaluate echos within attributes
  • Loading branch information
taylorotwell committed Apr 27, 2020
commit 3f537ca1c7cbf14af34dcf697801db75f2b173d3
2 changes: 1 addition & 1 deletion src/Illuminate/View/Compilers/BladeCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ protected function compileComponentTags($value)
}

return (new ComponentTagCompiler(
$this->classComponentAliases
$this, $this->classComponentAliases
))->compile($value);
}

Expand Down
41 changes: 39 additions & 2 deletions src/Illuminate/View/Compilers/ComponentTagCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Contracts\View\Factory;
use Illuminate\Support\Str;
use Illuminate\View\AnonymousComponent;
use Illuminate\View\Compilers\BladeCompiler;
use InvalidArgumentException;
use ReflectionClass;

Expand All @@ -16,6 +17,13 @@
*/
class ComponentTagCompiler
{
/**
* The Blade compiler instance.
*
* @var \Illuminate\View\Compilers\BladeCompiler
*/
protected $blade;

/**
* The component class aliases.
*
Expand All @@ -36,8 +44,9 @@ class ComponentTagCompiler
* @param array $aliases
* @return void
*/
public function __construct(array $aliases = [])
public function __construct(BladeCompiler $blade, array $aliases = [])
{
$this->blade = $blade;
$this->aliases = $aliases;
}

Expand Down Expand Up @@ -355,7 +364,7 @@ protected function getAttributesFromAttributeString(string $attributeString)

$this->boundAttributes[$attribute] = true;
} else {
$value = "'".str_replace("'", "\\'", $value)."'";
$value = "'".$this->compileAttributeEchos($value)."'";
}

return [$attribute => $value];
Expand All @@ -380,6 +389,34 @@ protected function parseBindAttributes(string $attributeString)
return preg_replace($pattern, ' bind:$1=', $attributeString);
}

/**
* Compile any Blade echo statements that are present in the attribute string.
*
* These echo statements need to be converted to string concatenation statements.
*
* @param string $attributeString
* @return string
*/
protected function compileAttributeEchos(string $attributeString)
{
$value = $this->blade->compileEchos($attributeString);

$value = collect(token_get_all($value))->map(function ($token) {
if (! is_array($token)) {
return $token;
}

return $token[0] === T_INLINE_HTML
? str_replace("'", "\\'", $token[1])
: $token[1];
})->implode('');

$value = str_replace('<?php echo ', '\'.', $value);
$value = str_replace('; ?>', '.\'', $value);

return $value;
}

/**
* Convert an array of attributes to a string.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/View/Compilers/Concerns/CompilesEchos.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ trait CompilesEchos
* @param string $value
* @return string
*/
protected function compileEchos($value)
public function compileEchos($value)
{
foreach ($this->getEchoMethods() as $method) {
$value = $this->$method($value);
Expand Down
49 changes: 29 additions & 20 deletions tests/View/Blade/BladeComponentTagCompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Container\Container;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\Factory;
use Illuminate\Filesystem\Filesystem;
use Illuminate\View\Compilers\BladeCompiler;
use Illuminate\View\Compilers\ComponentTagCompiler;
use Illuminate\View\Component;
Expand All @@ -20,7 +21,7 @@ public function tearDown(): void

public function testSlotsCanBeCompiled()
{
$result = (new ComponentTagCompiler)->compileSlots('<x-slot name="foo">
$result = $this->compiler()->compileSlots('<x-slot name="foo">
</x-slot>');

$this->assertSame("@slot('foo') \n".' @endslot', trim($result));
Expand All @@ -30,7 +31,7 @@ public function testBasicComponentParsing()
{
$this->mockViewFactory();

$result = (new ComponentTagCompiler(['alert' => TestAlertComponent::class]))->compileTags('<div><x-alert type="foo" limit="5" @click="foo" required /><x-alert /></div>');
$result = $this->compiler(['alert' => TestAlertComponent::class])->compileTags('<div><x-alert type="foo" limit="5" @click="foo" required /><x-alert /></div>');

$this->assertSame("<div> @component('Illuminate\Tests\View\Blade\TestAlertComponent', [])
<?php \$component->withName('alert'); ?>
Expand All @@ -43,7 +44,7 @@ public function testBasicComponentParsing()

public function testBasicComponentWithEmptyAttributesParsing()
{
$result = (new ComponentTagCompiler(['alert' => TestAlertComponent::class]))->compileTags('<div><x-alert type="" limit=\'\' @click="" required /></div>');
$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'); ?>
Expand All @@ -53,7 +54,7 @@ public function testBasicComponentWithEmptyAttributesParsing()

public function testDataCamelCasing()
{
$result = (new ComponentTagCompiler(['profile' => TestProfileComponent::class]))->compileTags('<x-profile user-id="1"></x-profile>');
$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'); ?>
Expand All @@ -62,7 +63,7 @@ public function testDataCamelCasing()

public function testColonData()
{
$result = (new ComponentTagCompiler(['profile' => TestProfileComponent::class]))->compileTags('<x-profile :user-id="1"></x-profile>');
$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'); ?>
Expand All @@ -71,7 +72,7 @@ public function testColonData()

public function testColonAttributesIsEscapedIfStrings()
{
$result = (new ComponentTagCompiler(['profile' => TestProfileComponent::class]))->compileTags('<x-profile :src="\'foo\'"></x-profile>');
$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'); ?>
Expand All @@ -80,7 +81,7 @@ public function testColonAttributesIsEscapedIfStrings()

public function testColonNestedComponentParsing()
{
$result = (new ComponentTagCompiler(['foo:alert' => TestAlertComponent::class]))->compileTags('<x-foo:alert></x-foo:alert>');
$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'); ?>
Expand All @@ -89,7 +90,7 @@ public function testColonNestedComponentParsing()

public function testColonStartingNestedComponentParsing()
{
$result = (new ComponentTagCompiler(['foo:alert' => TestAlertComponent::class]))->compileTags('<x:foo:alert></x-foo:alert>');
$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'); ?>
Expand All @@ -98,7 +99,7 @@ public function testColonStartingNestedComponentParsing()

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

$this->assertSame("<div> @component('Illuminate\Tests\View\Blade\TestAlertComponent', [])
<?php \$component->withName('alert'); ?>
Expand All @@ -113,7 +114,7 @@ public function testClassNamesCanBeGuessed()
$app->shouldReceive('getNamespace')->andReturn('App\\');
Container::setInstance($container);

$result = (new ComponentTagCompiler([]))->guessClassName('alert');
$result = $this->compiler()->guessClassName('alert');

$this->assertSame("App\View\Components\Alert", trim($result));

Expand All @@ -127,7 +128,7 @@ public function testClassNamesCanBeGuessedWithNamespaces()
$app->shouldReceive('getNamespace')->andReturn('App\\');
Container::setInstance($container);

$result = (new ComponentTagCompiler([]))->guessClassName('base.alert');
$result = $this->compiler()->guessClassName('base.alert');

$this->assertSame("App\View\Components\Base\Alert", trim($result));

Expand All @@ -138,7 +139,7 @@ public function testComponentsCanBeCompiledWithHyphenAttributes()
{
$this->mockViewFactory();

$result = (new ComponentTagCompiler(['alert' => TestAlertComponent::class]))->compileTags('<x-alert class="bar" wire:model="foo" x-on:click="bar" @click="baz" />');
$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'); ?>
Expand All @@ -148,7 +149,7 @@ public function testComponentsCanBeCompiledWithHyphenAttributes()

public function testSelfClosingComponentsCanBeCompiledWithDataAndAttributes()
{
$result = (new ComponentTagCompiler(['alert' => TestAlertComponent::class]))->compileTags('<x-alert title="foo" class="bar" wire:model="foo" />');
$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'); ?>
Expand All @@ -158,7 +159,7 @@ public function testSelfClosingComponentsCanBeCompiledWithDataAndAttributes()

public function testComponentsCanHaveAttachedWord()
{
$result = (new ComponentTagCompiler(['profile' => TestProfileComponent::class]))->compileTags('<x-profile></x-profile>Words');
$result = $this->compiler(['profile' => TestProfileComponent::class])->compileTags('<x-profile></x-profile>Words');

$this->assertSame("@component('Illuminate\Tests\View\Blade\TestProfileComponent', [])
<?php \$component->withName('profile'); ?>
Expand All @@ -167,7 +168,7 @@ public function testComponentsCanHaveAttachedWord()

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

$this->assertSame("@component('Illuminate\Tests\View\Blade\TestAlertComponent', [])
<?php \$component->withName('alert'); ?>
Expand All @@ -177,7 +178,7 @@ public function testSelfClosingComponentsCanHaveAttachedWord()

public function testSelfClosingComponentsCanBeCompiledWithBoundData()
{
$result = (new ComponentTagCompiler(['alert' => TestAlertComponent::class]))->compileTags('<x-alert :title="$title" class="bar" />');
$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'); ?>
Expand All @@ -187,7 +188,7 @@ public function testSelfClosingComponentsCanBeCompiledWithBoundData()

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

$this->assertSame("@component('Illuminate\Tests\View\Blade\TestAlertComponent', [])
Expand All @@ -205,7 +206,7 @@ public function testClasslessComponents()
$factory->shouldReceive('exists')->andReturn(true);
Container::setInstance($container);

$result = (new ComponentTagCompiler([]))->compileTags('<x-anonymous-component :name="\'Taylor\'" :age="31" wire:model="foo" />');
$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'); ?>
Expand Down Expand Up @@ -239,7 +240,7 @@ public function testItThrowsAnExceptionForNonExistingAliases()

$this->expectException(InvalidArgumentException::class);

(new ComponentTagCompiler(['alert' => 'foo.bar']))->compileTags('<x-alert />');
$this->compiler(['alert' => 'foo.bar'])->compileTags('<x-alert />');
}

public function testItThrowsAnExceptionForNonExistingClass()
Expand All @@ -253,7 +254,7 @@ public function testItThrowsAnExceptionForNonExistingClass()

$this->expectException(InvalidArgumentException::class);

(new ComponentTagCompiler)->compileTags('<x-alert />');
$this->compiler()->compileTags('<x-alert />');
}

protected function mockViewFactory($existsSucceeds = true)
Expand All @@ -263,6 +264,14 @@ protected function mockViewFactory($existsSucceeds = true)
$factory->shouldReceive('exists')->andReturn($existsSucceeds);
Container::setInstance($container);
}

protected function compiler($aliases = [])
{
return new ComponentTagCompiler(
new BladeCompiler(new Filesystem, __DIR__),
$aliases
);
}
}

class TestAlertComponent extends Component
Expand Down