Skip to content

Commit

Permalink
Merge pull request #413 from niektenhoopen/v5
Browse files Browse the repository at this point in the history
[v5] There should be an alt attribute on images if it's specified as empty
  • Loading branch information
khalwat authored Oct 19, 2024
2 parents 8bbba2f + 079b4fb commit d2d820c
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 d2d820c

Please sign in to comment.