Skip to content

Commit

Permalink
[10.x] Anonymous component bound attribute values are evaluated twice…
Browse files Browse the repository at this point in the history
… (#50403)

* fix: Anonymous component attributes evaluated multiple times

* Update tests to use new compiled code

* Fix edge case with attribute bag and refactor

* Update ComponentTagCompiler.php

* Update BladeComponentsTest.php

* Update ComponentTagCompiler.php

* Update ComponentTagCompiler.php

* use loop

---------

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
danharrin and taylorotwell authored Mar 8, 2024
1 parent 420a39e commit ba1e042
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
33 changes: 31 additions & 2 deletions Compilers/ComponentTagCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,11 +258,11 @@ protected function componentString(string $component, array $attributes)
$parameters = $data->all();
}

return "##BEGIN-COMPONENT-CLASS##@component('{$class}', '{$component}', [".$this->attributesToString($parameters, $escapeBound = false).'])
return "##BEGIN-COMPONENT-CLASS##@component('{$class}', '{$component}', \$componentData = [".$this->attributesToString($parameters, $escapeBound = false).'])
<?php if (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag && $constructor = (new ReflectionClass('.$class.'::class))->getConstructor()): ?>
<?php $attributes = $attributes->except(collect($constructor->getParameters())->map->getName()->all()); ?>
<?php endif; ?>
<?php $component->withAttributes(['.$this->attributesToString($attributes->all(), $escapeAttributes = $class !== DynamicComponent::class).']); ?>';
<?php $component->withAttributes(['.$this->attributesToStringWithExistingComponentData($attributes->all(), $escapeAttributes = $class !== DynamicComponent::class).']); ?>';
}

/**
Expand Down Expand Up @@ -783,6 +783,35 @@ protected function attributesToString(array $attributes, $escapeBound = true)
->implode(',');
}

/**
* Convert an array of attributes to a string using existing component data that has already been evaluated.
*
* @param array $attributes
* @param bool $escapeBound
* @return string
*/
protected function attributesToStringWithExistingComponentData(array $attributes, $escapeBound = true)
{
$results = [];

foreach ($attributes as $attribute => $value) {
if (! $escapeBound ||
! isset($this->boundAttributes[$attribute]) ||
$value === 'true' ||
is_numeric($value)) {
$results[] = "'{$attribute}' => {$value}";

continue;
}

$results[] = "'{$attribute}' => \Illuminate\View\Compilers\BladeCompiler::sanitizeComponentAttribute(".(
($attribute === 'attributes') ? $value : ('$componentData[\'data\'][\''.Str::camel($attribute).'\'] ?? null')
).')';
}

return implode(',', $results);
}

/**
* Strip any quotes from the given string.
*
Expand Down
5 changes: 5 additions & 0 deletions Compilers/Concerns/CompilesComponents.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public static function compileClassComponentOpening(string $component, string $a
{
return implode("\n", [
'<?php if (isset($component)) { $__componentOriginal'.$hash.' = $component; } ?>',
'<?php if (isset($componentData)) { $__componentDataOriginal'.$hash.' = $componentData; } ?>',
'<?php if (isset($attributes)) { $__attributesOriginal'.$hash.' = $attributes; } ?>',
'<?php $component = '.$component.'::resolve('.($data ?: '[]').' + (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag ? (array) $attributes->getIterator() : [])); ?>',
'<?php $component->withName('.$alias.'); ?>',
Expand Down Expand Up @@ -100,6 +101,10 @@ public function compileEndComponentClass()
'<?php $attributes = $__attributesOriginal'.$hash.'; ?>',
'<?php unset($__attributesOriginal'.$hash.'); ?>',
'<?php endif; ?>',
'<?php if (isset($__componentDataOriginal'.$hash.')): ?>',
'<?php $componentData = $__componentDataOriginal'.$hash.'; ?>',
'<?php unset($__componentDataOriginal'.$hash.'); ?>',
'<?php endif; ?>',
'<?php if (isset($__componentOriginal'.$hash.')): ?>',
'<?php $component = $__componentOriginal'.$hash.'; ?>',
'<?php unset($__componentOriginal'.$hash.'); ?>',
Expand Down

0 comments on commit ba1e042

Please sign in to comment.