From 079b4fb09369d48273ac56612344f4ed31cf9382 Mon Sep 17 00:00:00 2001 From: Niek ten Hoopen Date: Tue, 15 Oct 2024 08:52:16 +0200 Subject: [PATCH] There should be an alt attribute on images if it's specified as empty --- src/models/ImgTag.php | 9 +++++++-- src/models/PictureTag.php | 9 +++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/models/ImgTag.php b/src/models/ImgTag.php index d20ed7c..d5ecdc1 100644 --- a/src/models/ImgTag.php +++ b/src/models/ImgTag.php @@ -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); diff --git a/src/models/PictureTag.php b/src/models/PictureTag.php index d38d2a1..846be1a 100644 --- a/src/models/PictureTag.php +++ b/src/models/PictureTag.php @@ -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 tag