Skip to content
This repository has been archived by the owner on Feb 16, 2024. It is now read-only.

Commit

Permalink
Merge pull request #6 from jtolj/develop
Browse files Browse the repository at this point in the history
Add alias (merge) and test for mergeAttributes. Support string separated class names in addClass / merge
  • Loading branch information
jtolj authored Oct 1, 2020
2 parents 5bf2686 + 097e8b3 commit 85d1fd2
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/HtmlAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,19 @@ public function setAttribute($attribute, $value)
return $this;
}

/**
* Alias for mergeAttributes().
*
* @param string $attribute
* @param string|array $value
*
* @return HtmlAttributes
*/
public function merge($attribute, $value)
{
return $this->mergeAttributes($attribute, $value);
}

/**
* Add a value or values to an attribute, leaving existing values in place.
*
Expand All @@ -264,16 +277,23 @@ public function setAttribute($attribute, $value)
public function mergeAttributes($attribute, $value)
{
$values = $this->offsetGet($attribute);

if (!is_array($values)) {
$values = [$values];
}

if (is_string($value) || (is_object($value) && method_exists($value, '__toString'))) {
$values[] = (string) $value;
if ($attribute === 'class') {
$values = array_merge($values, explode(' ', (string) $value));
} else {
$values[] = (string) $value;
}
} elseif (is_array($value)) {
$values = array_merge($values, (array) $value);
} else {
throw new InvalidValueException();
}

$this->offsetSet($attribute, array_values(array_unique($values)));

return $this;
Expand Down
27 changes: 27 additions & 0 deletions tests/AttributesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,33 @@ public function it_can_have_all_classes_removed_from_it()
$this->assertEquals('', (string) $attributes);
}

/** @test */
public function it_can_have_attributes_merged_into_it()
{
$attributes = new HtmlAttributes(['class' => ['card', 'card--wide'], 'id' => 'card-1']);
$attributes_copy = new HtmlAttributes(['class' => ['card', 'card--wide'], 'id' => 'card-1']);

// Test String
$attributes->mergeAttributes('aria-description', 'A description.');
$attributes_copy->merge('aria-description', 'A description.');

$this->assertEquals((string) $attributes, (string) $attributes_copy);
$this->assertEquals('aria-description="A description." class="card card--wide" id="card-1"', (string) $attributes);

// Test String Classes with Spaces
$attributes->mergeAttributes('class', 'one two three');
$attributes_copy->merge('class', 'one two three');

$this->assertEquals((string) $attributes, (string) $attributes_copy);
$this->assertEquals('aria-description="A description." class="card card--wide one two three" id="card-1"', (string) $attributes);

// Test Array
$attributes->mergeAttributes('class', ['red', 'blue', 'green']);
$attributes_copy->merge('class', ['red', 'blue', 'green']);
$this->assertEquals((string) $attributes, (string) $attributes_copy);
$this->assertEquals('aria-description="A description." class="card card--wide one two three red blue green" id="card-1"', (string) $attributes);
}

/** @test */
public function it_can_disallow_unsafe_attributes()
{
Expand Down

0 comments on commit 85d1fd2

Please sign in to comment.