HTML: RSPEC-1102 demands either id or scope on each th element

RSPEC-1102 erroneously demands that each th element has either an id or a scope attribute, even in simple tables. The table below is quoted from the HTML5 specification (but with end tags added where the spec omits the optional end tags):

<table>
  <caption>Characteristics with positive and negative sides</caption>
  <thead>
   <tr>
    <th>Characteristic</th>
    <th>Negative</th>
    <th>Positive</th>
   </tr>
  </thead>
  <tbody>
   <tr>
    <th>Mood</th>
    <td>Sad</td>
    <td>Happy</td>
   </tr>
   <tr>
    <th>Grade</th>
    <td>Failing</td>
    <td>Passing</td>
  </tr>
 </tbody>
</table>

For each of the th elements, SonarLint in VS Code says, “Add either an id or a scope attribute to this <th> tag”.
Since this is a simple table, this is not necessary. Screen readers can make perfect sense of this table with adding scope attributes or without adding id attributes to the th elements with headers attributes on the td elements that reference the th IDs.

For example, if you add scope=row to the th elements for “Mood” and “Grade”, this makes absolutely no difference when reading this table with NVDA or Narrator in Edge.

For an example of a table that has the scope attribute only on row header cells and not on column header cells (an no id attributes anywhere), see WCAG 2.1 Technique H51: Using table markup to present tabular information at the W3C.

The attributes scope and id make sense in complex tables but are redundant in simple tables such as the above. SonarLint should first check whether the table is a complex table, and prompt for the addition of either scope or id if it concludes that the table is complex.

The following statement in RSPEC-1102 is also erroneous:

Tables are considered as such when the headers are either all in the first row, or all in the first column. The two conditions must not apply together.

In reality, it is perfectly acceptable to have both row headers and column headers.

The above comments are also relevant to RSPEC-5260.

Hello again Christophe,

I wanted to take a second to thank you for all the threads you opened regarding HTML and accessibility. Your threads are detailed and thoughtful, with examples and links, which makes understanding them much easier.

Regarding this thread, I think the rule includes some cases for simple tables, however as you point out, it is missing the common case of having a thead tag. I also agree we can improve the description a bit in the paragraph you cite.

I created a ticket to improve both S1102 and S5260 as you suggested:
https://sonarsource.atlassian.net/issues/SONARHTML-224

Kinds regards,