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.
This commit is contained in:
Jun Pataleta
2024-04-12 11:45:07 +08:00
parent 8a525dcce6
commit 25e2df4fe0
+9 -1
View File
@@ -1830,7 +1830,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);
}