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
43 changes: 43 additions & 0 deletions src/Runtime/BlazeAttributeBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ 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.
if (count($attributeDefaults) === 1 && isset($attributeDefaults['class']) && is_string($attributeDefaults['class'])) {
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 +108,10 @@ public function merge(array $attributeDefaults = [], $escape = true): static
/** {@inheritdoc} */
public function class($classList): static
{
if (is_string($classList)) {
return $this->withMergedClass(e($classList));
}

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

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

if ($current) {
$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
5 changes: 5 additions & 0 deletions tests/ComparisonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@
BLADE
));

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

test('deeply nested same component with different components interleaved', fn () => compare(<<<'BLADE'
<x-card class="outer">
<x-wrapper>
Expand Down
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