Skip to content

Commit

Permalink
feat: Allow for empty alt tags for screen readers as per WCAG ([411](
Browse files Browse the repository at this point in the history
  • Loading branch information
khalwat committed Oct 19, 2024
1 parent 86b6acc commit 4086f66
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 15 deletions.
19 changes: 19 additions & 0 deletions src/models/BaseTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ abstract class BaseTag extends Model implements TagInterface
{
use TagTrait;

/**
* Attributes that are allowed to be an empty string
*/
protected const ALLOWED_EMPTY_ATTRS = ['alt'];

/**
* @return string
*/
Expand All @@ -40,4 +45,18 @@ public function render(): Markup
{
return Template::raw('');
}

/**
* Filter out attributes with empty values in them, so they don't get rendered
*
* @param array $attrs
* @return array
*/
public function filterEmptyAttributes(array $attrs): array
{
return array_filter($attrs, static function($value, $key) {
// Keep certain attributes even if they are empty
return in_array($key, self::ALLOWED_EMPTY_ATTRS, true) || !empty($value);
}, ARRAY_FILTER_USE_BOTH);
}
}
9 changes: 2 additions & 7 deletions src/models/ImgTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,8 @@ public function render(): Markup
if ($this->loadingStrategy !== 'eager') {
$attrs = $this->swapLazyLoadAttrs($this->loadingStrategy, $this->placeholder, $attrs);
}

// Remove any empty attributes except the alt attribute
$attrs = array_filter($attrs, function($value, $key) {
// Keep the 'alt' attribute even if it's empty
return $key === 'alt' || !empty($value);
}, ARRAY_FILTER_USE_BOTH);

// Remove any empty attributes
$attrs = $this->filterEmptyAttributes($attrs);
// Render the tag
$tag = Html::tag('img', '', $attrs);

Expand Down
2 changes: 1 addition & 1 deletion src/models/LinkPreloadTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function render(): Markup
{
$attrs = $this->linkAttrs;
// Remove any empty attributes
$attrs = array_filter($attrs);
$attrs = $this->filterEmptyAttributes($attrs);
// Render the tag
$tag = Html::tag('link', '', $attrs);

Expand Down
9 changes: 2 additions & 7 deletions src/models/PictureTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,8 @@ public function render(): Markup
if ($this->loadingStrategy !== 'eager') {
$attrs = $this->swapLazyLoadAttrs($this->loadingStrategy, $this->placeholder, $attrs);
}

// Remove any empty attributes except the alt attribute
$attrs = array_filter($attrs, function($value, $key) {
// Keep the 'alt' attribute even if it's empty
return $key === 'alt' || !empty($value);
}, ARRAY_FILTER_USE_BOTH);

// Remove any empty attributes
$attrs = $this->filterEmptyAttributes($attrs);
// Render the tag
$content .= Html::tag('img', '', $attrs);
// Handle the <picture> tag
Expand Down

0 comments on commit 4086f66

Please sign in to comment.