MDL-68343 output: fix html table structure

A <th> that contains no text may result in cells with missing or
incorrect header information. WAVE advises that we should make a cell
a <td> rather than a <th> if the cell must remain empty (such as the
top-left cell in a data table).

Also note that the scope attribute is only valid for <th>
This commit is contained in:
Shamim Rezaie 2020-04-06 17:03:51 +10:00
parent 788dfb9c7d
commit d28b4ec326

View File

@ -2207,8 +2207,9 @@ class html_writer {
$heading->header = true;
}
if ($heading->header && empty($heading->scope)) {
$heading->scope = 'col';
$tagtype = 'td';
if ($heading->header && (string)$heading->text != '') {
$tagtype = 'th';
}
$heading->attributes['class'] .= ' header c' . $key;
@ -2224,16 +2225,15 @@ class html_writer {
$heading->attributes['class'] .= ' ' . $table->colclasses[$key];
}
$heading->attributes['class'] = trim($heading->attributes['class']);
$attributes = array_merge($heading->attributes, array(
$attributes = array_merge($heading->attributes, [
'style' => $table->align[$key] . $table->size[$key] . $heading->style,
'scope' => $heading->scope,
'colspan' => $heading->colspan,
));
]);
$tagtype = 'td';
if ($heading->header === true) {
$tagtype = 'th';
if ($tagtype == 'th') {
$attributes['scope'] = !empty($heading->scope) ? $heading->scope : 'col';
}
$output .= html_writer::tag($tagtype, $heading->text, $attributes) . "\n";
}
$output .= html_writer::end_tag('tr') . "\n";