Skip to content

Commit

Permalink
There should be an alt attribute on images if it's specified as empty
Browse files Browse the repository at this point in the history
  • Loading branch information
niektenhoopen committed Oct 15, 2024
1 parent 8bbba2f commit 079b4fb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
9 changes: 7 additions & 2 deletions src/models/ImgTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,13 @@ public function render(): Markup
if ($this->loadingStrategy !== 'eager') {
$attrs = $this->swapLazyLoadAttrs($this->loadingStrategy, $this->placeholder, $attrs);
}
// Remove any empty attributes
$attrs = array_filter($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);

// Render the tag
$tag = Html::tag('img', '', $attrs);

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

// Render the tag
$content .= Html::tag('img', '', $attrs);
// Handle the <picture> tag
Expand Down

0 comments on commit 079b4fb

Please sign in to comment.