From 4544ea122bbdbe4165784a8f5dd04ef7060fdeee Mon Sep 17 00:00:00 2001 From: Jun Pataleta Date: Fri, 12 Apr 2024 11:44:22 +0800 Subject: [PATCH] MDL-70829 output: Remove the presentation role for html_writer::img() * A presentation role is not necessary for the img tag. - If a non-empty alt text is provided, the presentation role will conflict with the alt text. - An empty alt text denotes a decorative image. The presence of a presentation role is redundant. * Make sure that the alt text is set to an empty string. Otherwise, an img tag without an alt attribute will be produced which is an invalid markup. --- lib/outputcomponents.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/outputcomponents.php b/lib/outputcomponents.php index 8527eed722f..c4ed2ee0b12 100644 --- a/lib/outputcomponents.php +++ b/lib/outputcomponents.php @@ -1840,7 +1840,15 @@ class html_writer { public static function img($src, $alt, array $attributes = null) { $attributes = (array)$attributes; $attributes['src'] = $src; - $attributes['alt'] = $alt; + // In case a null alt text is provided, set it to an empty string. + $attributes['alt'] = $alt ?? ''; + if (array_key_exists('role', $attributes) && core_text::strtolower($attributes['role']) === 'presentation') { + // A presentation role is not necessary for the img tag. + // If a non-empty alt text is provided, the presentation role will conflict with the alt text. + // An empty alt text denotes a decorative image. The presence of a presentation role is redundant. + unset($attributes['role']); + debugging('The presentation role is not necessary for an img tag.', DEBUG_DEVELOPER); + } return self::empty_tag('img', $attributes); }