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
8 changes: 7 additions & 1 deletion src/BladeRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,13 @@ function (string $input) use ($path) {
}

return $input;
}
},
// Runs after component tags are compiled, so it covers the attributes of Blaze and Blade components
fn (string $input) => str_replace(
'\Illuminate\View\Compilers\BladeCompiler::sanitizeComponentAttribute(',
'\Livewire\Blaze\Runtime\BlazeRuntime::sanitizeFoldedAttribute(',
$input,
),
],
'path' => null,
'forElseCounter' => 0,
Expand Down
9 changes: 8 additions & 1 deletion src/Folder/Foldable.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,17 @@ protected function storeAttribute(Attribute $attribute, string $type): mixed
return $attribute->getStaticValue();
}

$placeholder = 'BLAZE_PLACEHOLDER_' . $type . '_' . $this->placeholderIndex++ . '_';
$index = $this->placeholderIndex++;
$placeholder = 'BLAZE_PLACEHOLDER_' . $type . '_' . $index . '_';

$this->attributeByPlaceholder[$placeholder] = $attribute;

// Escaping a prop while folding turns it into an attribute placeholder with the same index
// @see \Livewire\Blaze\Runtime\BlazeRuntime::sanitizeFoldedAttribute()
if ($type === 'PROP') {
$this->attributeByPlaceholder['BLAZE_PLACEHOLDER_ATTR_' . $index . '_'] = $attribute;
}

return $placeholder;
}

Expand Down
25 changes: 24 additions & 1 deletion src/Runtime/BlazeRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Livewire\Blaze\Runtime;

use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Contracts\View\Factory;
use Illuminate\Foundation\Application;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -160,6 +161,26 @@ public function mergedComponentSlots(): array
return $result;
}

/**
* Escape an attribute value like Blade, while folding.
*
* Escaping leaves a placeholder unchanged, so a PROP placeholder, which is restored raw,
* is turned into an ATTR placeholder of the same attribute, which is restored escaped.
*
* @see \Livewire\Blaze\BladeRenderer::render()
*/
public static function sanitizeFoldedAttribute(mixed $value): mixed
{
$sanitized = BladeCompiler::sanitizeComponentAttribute($value);

// Like Blade, trusted HTML stays unescaped, so its placeholder stays raw
if ($value instanceof Htmlable || ! is_string($sanitized)) {
return $sanitized;
}

return str_replace('BLAZE_PLACEHOLDER_PROP_', 'BLAZE_PLACEHOLDER_ATTR_', $sanitized);
}

/**
* Merge an attribute bag passed with :attributes into the component's own attributes.
*
Expand All @@ -172,7 +193,9 @@ public function mergeAttributes(array $attributes): array
$parent = $attributes['attributes'] ?? null;

if (! $parent instanceof ComponentAttributeBag) {
$attributes['attributes'] = BladeCompiler::sanitizeComponentAttribute($parent);
$attributes['attributes'] = $this->folding
? static::sanitizeFoldedAttribute($parent)
: BladeCompiler::sanitizeComponentAttribute($parent);

return $attributes;
}
Expand Down
5 changes: 5 additions & 0 deletions tests/FluxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@
BLADE, ['value' => 'a"b<c>&d\'e']
));

test('folded wrapper binds its prop to an input error attribute', fn () => compare(<<<'BLADE'
<x-foldable.flux-input-unblaze :error-label="$value" />
BLADE, ['value' => 'a"b<c>&d\'e']
));

/**
* Checks that attributes passed through delegate component are escaped exactly once, like in Blade
*/
Expand Down
32 changes: 32 additions & 0 deletions tests/Folder/UnblazeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,35 @@

expect($before)->toBe($after);
});

test('escapes props bound to child attributes', function () {
$input = '<x-foldable.forwarded-prop-unblaze :value="$value" />';

$node = app(Parser::class)->parse($input)[0];
$foldable = new Foldable($node, fixture_path('views/components/foldable/forwarded-prop-unblaze.blade.php'), app(BladeRenderer::class), app(BladeService::class));

expect($foldable->fold())->toEqualCollapsingWhitespace(
sprintf('<div %s ></div>', join('', [
'<?php if (isset($scope)) $__scope = $scope; ?>',
'<?php $scope = array ( \'attributes\' => array ( \'aria-label\' => \Illuminate\View\Compilers\BladeCompiler::sanitizeComponentAttribute($value), ), ); ?>',
' {{ new \Illuminate\View\ComponentAttributeBag($scope[\'attributes\']) }} ',
'<?php if (isset($__scope)) { $scope = $__scope; unset($__scope); } ?>'
]))
);
});

test('keeps props bound to child attributes as trusted html unescaped', function () {
$input = '<x-foldable.forwarded-html-prop-unblaze :value="$value" />';

$node = app(Parser::class)->parse($input)[0];
$foldable = new Foldable($node, fixture_path('views/components/foldable/forwarded-html-prop-unblaze.blade.php'), app(BladeRenderer::class), app(BladeService::class));

expect($foldable->fold())->toEqualCollapsingWhitespace(
sprintf('<div %s ></div>', join('', [
'<?php if (isset($scope)) $__scope = $scope; ?>',
'<?php $scope = array ( \'attributes\' => array ( \'aria-label\' => $value, ), ); ?>',
' {{ new \Illuminate\View\ComponentAttributeBag($scope[\'attributes\']) }} ',
'<?php if (isset($__scope)) { $scope = $__scope; unset($__scope); } ?>'
]))
);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@blaze(fold: true)

<div
@unblaze(scope: ['attributes' => $attributes->getAttributes()])
{{ new \Illuminate\View\ComponentAttributeBag($scope['attributes']) }}
@endunblaze
></div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@blaze(fold: true, safe: ['errorLabel'])

@props(['errorLabel'])

<flux:input label="Email" :error:aria-label="$errorLabel" />
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@blaze(fold: true, safe: ['value'])

@props(['value'])

<x-foldable.attributes-unblaze :aria-label="new \Illuminate\Support\HtmlString($value)" />
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@blaze(fold: true, safe: ['value'])

@props(['value'])

<x-foldable.attributes-unblaze :aria-label="$value" />
Loading