|
1 | 1 | import { tokenList } from '../../core/utils';
|
2 | 2 | import { isVisibleToScreenReaders } from '../../commons/dom';
|
| 3 | +import { getRole } from '../../commons/aria'; |
| 4 | + |
| 5 | +// Order determines the priority of reporting |
| 6 | +// Only if 0 of higher issues exists will the next be reported |
| 7 | +const messageKeys = [ |
| 8 | + 'cell-header-not-in-table', |
| 9 | + 'cell-header-not-th', |
| 10 | + 'header-refs-self', |
| 11 | + 'empty-hdrs' // incomplete |
| 12 | +]; |
| 13 | +const [notInTable, notTh, selfRef, emptyHdrs] = messageKeys; |
3 | 14 |
|
4 | 15 | export default function tdHeadersAttrEvaluate(node) {
|
5 | 16 | const cells = [];
|
6 |
| - const reviewCells = []; |
7 |
| - const badCells = []; |
8 |
| - |
| 17 | + const cellRoleById = {}; |
9 | 18 | for (let rowIndex = 0; rowIndex < node.rows.length; rowIndex++) {
|
10 | 19 | const row = node.rows[rowIndex];
|
11 | 20 |
|
12 | 21 | for (let cellIndex = 0; cellIndex < row.cells.length; cellIndex++) {
|
13 |
| - cells.push(row.cells[cellIndex]); |
| 22 | + const cell = row.cells[cellIndex]; |
| 23 | + cells.push(cell); |
| 24 | + |
| 25 | + // Save header id to set if it's th or td with roles columnheader/rowheader |
| 26 | + const cellId = cell.getAttribute('id'); |
| 27 | + if (cellId) { |
| 28 | + cellRoleById[cellId] = getRole(cell); |
| 29 | + } |
14 | 30 | }
|
15 | 31 | }
|
16 | 32 |
|
17 |
| - const ids = cells |
18 |
| - .filter(cell => cell.getAttribute('id')) |
19 |
| - .map(cell => cell.getAttribute('id')); |
20 |
| - |
| 33 | + const badCells = { |
| 34 | + [selfRef]: new Set(), |
| 35 | + [notInTable]: new Set(), |
| 36 | + [notTh]: new Set(), |
| 37 | + [emptyHdrs]: new Set() |
| 38 | + }; |
21 | 39 | cells.forEach(cell => {
|
22 |
| - let isSelf = false; |
23 |
| - let notOfTable = false; |
24 |
| - |
25 | 40 | if (!cell.hasAttribute('headers') || !isVisibleToScreenReaders(cell)) {
|
26 | 41 | return;
|
27 | 42 | }
|
28 |
| - |
29 | 43 | const headersAttr = cell.getAttribute('headers').trim();
|
30 | 44 | if (!headersAttr) {
|
31 |
| - return reviewCells.push(cell); |
| 45 | + badCells[emptyHdrs].add(cell); |
| 46 | + return; |
32 | 47 | }
|
33 | 48 |
|
| 49 | + const cellId = cell.getAttribute('id'); |
34 | 50 | // Get a list all the values of the headers attribute
|
35 | 51 | const headers = tokenList(headersAttr);
|
36 |
| - |
37 |
| - if (headers.length !== 0) { |
38 |
| - // Check if the cell's id is in this list |
39 |
| - if (cell.getAttribute('id')) { |
40 |
| - isSelf = headers.indexOf(cell.getAttribute('id').trim()) !== -1; |
| 52 | + headers.forEach(headerId => { |
| 53 | + if (cellId && headerId === cellId) { |
| 54 | + // Header references its own cell |
| 55 | + badCells[selfRef].add(cell); |
| 56 | + } else if (!cellRoleById[headerId]) { |
| 57 | + // Header references a cell that is not in the table |
| 58 | + badCells[notInTable].add(cell); |
| 59 | + } else if ( |
| 60 | + !['columnheader', 'rowheader'].includes(cellRoleById[headerId]) |
| 61 | + ) { |
| 62 | + // Header references a cell that is not a row or column header |
| 63 | + badCells[notTh].add(cell); |
41 | 64 | }
|
| 65 | + }); |
| 66 | + }); |
42 | 67 |
|
43 |
| - // Check if the headers are of cells inside the table |
44 |
| - notOfTable = headers.some(header => !ids.includes(header)); |
45 |
| - |
46 |
| - if (isSelf || notOfTable) { |
47 |
| - badCells.push(cell); |
| 68 | + for (const messageKey of messageKeys) { |
| 69 | + if (badCells[messageKey].size > 0) { |
| 70 | + this.relatedNodes([...badCells[messageKey]]); |
| 71 | + if (messageKey === emptyHdrs) { |
| 72 | + return undefined; |
48 | 73 | }
|
| 74 | + this.data({ messageKey }); |
| 75 | + return false; |
49 | 76 | }
|
50 |
| - }); |
51 |
| - |
52 |
| - if (badCells.length > 0) { |
53 |
| - this.relatedNodes(badCells); |
54 |
| - return false; |
55 |
| - } |
56 |
| - |
57 |
| - if (reviewCells.length) { |
58 |
| - this.relatedNodes(reviewCells); |
59 |
| - return undefined; |
60 | 77 | }
|
61 |
| - |
62 | 78 | return true;
|
63 | 79 | }
|
0 commit comments