Skip to content

Commit

Permalink
[8.x] Support for escaping bound attributes (#36042)
Browse files Browse the repository at this point in the history
* Support for escaping bound attributes

* StyleCI
  • Loading branch information
inxilpro authored Jan 25, 2021
1 parent f364d7f commit 1f8d0f3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Illuminate/View/Compilers/ComponentTagCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,10 @@ protected function getAttributesFromAttributeString(string $attributeString)
$value = "'".$this->compileAttributeEchos($value)."'";
}

if (Str::startsWith($attribute, '::')) {
$attribute = substr($attribute, 1);
}

return [$attribute => $value];
})->toArray();
}
Expand Down Expand Up @@ -490,7 +494,7 @@ protected function parseBindAttributes(string $attributeString)
{
$pattern = "/
(?:^|\s+) # start of the string or whitespace between attributes
: # attribute needs to start with a semicolon
:(?!:) # attribute needs to start with a single colon
([\w\-:.@]+) # match the actual attribute name
= # only match attributes that have a value
/xm";
Expand Down
8 changes: 8 additions & 0 deletions tests/View/Blade/BladeComponentTagCompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ public function testColonData()
<?php \$component->withAttributes([]); ?> @endComponentClass##END-COMPONENT-CLASS##", trim($result));
}

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

$this->assertSame("##BEGIN-COMPONENT-CLASS##@component('Illuminate\Tests\View\Blade\TestProfileComponent', 'profile', ['userId' => 1])
<?php \$component->withAttributes([':title' => 'user.name']); ?> @endComponentClass##END-COMPONENT-CLASS##", trim($result));
}

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

0 comments on commit 1f8d0f3

Please sign in to comment.