Skip to content
Open
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
44 changes: 44 additions & 0 deletions src/Runtime/BlazeAttributeBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ public static function make(array $attributes, array $boundKeys = [], array $ori
/** {@inheritdoc} */
public function merge(array $attributeDefaults = [], $escape = true): static
{
// Fast path: merge(['class' => 'string']) — the most common pattern. Skipped when the
// bag contains a style attribute, which merge() normalizes and reorders alongside class.
if (count($attributeDefaults) === 1 && isset($attributeDefaults['class']) && is_string($attributeDefaults['class']) && ! isset($this->attributes['style'])) {
return $this->withMergedClass($escape ? e($attributeDefaults['class']) : $attributeDefaults['class']);
}

if ($escape) {
foreach ($attributeDefaults as $key => $value) {
if ($this->shouldEscapeAttributeValue($escape, $value)) {
Expand Down Expand Up @@ -103,6 +109,10 @@ public function merge(array $attributeDefaults = [], $escape = true): static
/** {@inheritdoc} */
public function class($classList): static
{
if (is_string($classList) && ! isset($this->attributes['style'])) {
return $this->withMergedClass(e($classList));
}

$classes = $this->toCssClasses(Arr::wrap($classList));

return $this->merge(['class' => $classes]);
Expand All @@ -111,11 +121,45 @@ public function class($classList): static
/** {@inheritdoc} */
public function style($styleList): static
{
if (is_string($styleList) && ! isset($this->attributes['class'])) {
$default = e(rtrim($styleList, ';').';');
$current = $this->attributes['style'] ?? null;

if ($current !== null) {
$current = rtrim((string) $current, ';').';';
$style = $current === $default ? $default : $default.' '.$current;
} else {
$style = $default;
}

return new static(['style' => $style] + $this->attributes);
}

$styles = $this->toCssStyles((array) $styleList);

return $this->merge(['style' => $styles]);
}

/**
* Return a new bag with the given class default merged into the current class attribute.
*/
protected function withMergedClass(string $default): static
{
$current = $this->attributes['class'] ?? '';

if (! $current || $current === $default) {
$class = $default;
} elseif (! $default) {
$class = $current ?: '';
} else {
$class = $default.' '.$current;
}

// Array union places class first, matching merge()'s
// array_merge($attributeDefaults, $attributes) key ordering.
return new static(['class' => $class] + $this->attributes);
}

/**
* Convert class list to CSS classes string.
*/
Expand Down
18 changes: 18 additions & 0 deletions tests/ComparisonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,24 @@
BLADE
));

test('merge preserves attribute ordering', fn () => compare(<<<'BLADE'
<x-merge-class wire:model="data" class="extra" wire:ignore />
BLADE
));

test('merge class with style attribute present', fn () => compare(<<<'BLADE'
<x-merge-class wire:model="data" style="color:blue" class="extra" />
BLADE
));

test('class and style string arguments', fn () => compare(<<<'BLADE'
<x-class-style-string wire:model="data" class="extra" style="color:blue" />
<x-class-style-string wire:model="data" class="extra" />
<x-class-style-string wire:model="data" style="color:blue" />
<x-class-style-string data-x="1" />
BLADE
));

test('deeply nested same component with different components interleaved', fn () => compare(<<<'BLADE'
<x-card class="outer">
<x-wrapper>
Expand Down
4 changes: 4 additions & 0 deletions tests/fixtures/views/components/class-style-string.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@blaze

<div {{ $attributes->class('base') }}></div>
<div {{ $attributes->style('color: red') }}></div>
3 changes: 3 additions & 0 deletions tests/fixtures/views/components/merge-class.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@blaze

<div {{ $attributes->merge(['class' => 'default-class']) }}></div>
Loading