Skip to content

Commit

Permalink
replace badcells arr with set
Browse files Browse the repository at this point in the history
  • Loading branch information
engineerklimov committed Nov 12, 2024
1 parent 1722053 commit 5d1440e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
20 changes: 10 additions & 10 deletions lib/checks/tables/td-headers-attr-evaluate.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,18 @@ export default function tdHeadersAttrEvaluate(node) {
}

const badCells = {
[selfRef]: [],
[notInTable]: [],
[notTh]: [],
[emptyHdrs]: []
[selfRef]: new Set(),
[notInTable]: new Set(),
[notTh]: new Set(),
[emptyHdrs]: new Set()
};
cells.forEach(cell => {
if (!cell.hasAttribute('headers') || !isVisibleToScreenReaders(cell)) {
return;
}
const headersAttr = cell.getAttribute('headers').trim();
if (!headersAttr) {
badCells[emptyHdrs].push(cell);
badCells[emptyHdrs].add(cell);
return;
}

Expand All @@ -52,22 +52,22 @@ export default function tdHeadersAttrEvaluate(node) {
headers.forEach(headerId => {
if (cellId && headerId === cellId) {
// Header references its own cell
badCells[selfRef].push(cell);
badCells[selfRef].add(cell);
} else if (!cellRoleById[headerId]) {
// Header references a cell that is not in the table
badCells[notInTable].push(cell);
badCells[notInTable].add(cell);
} else if (
!['columnheader', 'rowheader'].includes(cellRoleById[headerId])
) {
// Header references a cell that is not a row or column header
badCells[notTh].push(cell);
badCells[notTh].add(cell);
}
});
});

for (const messageKey of messageKeys) {
if (badCells[messageKey].length > 0) {
this.relatedNodes(badCells[messageKey]);
if (badCells[messageKey].size > 0) {
this.relatedNodes([...badCells[messageKey]]);
if (messageKey === emptyHdrs) {
return undefined;
}
Expand Down
16 changes: 16 additions & 0 deletions test/checks/tables/td-headers-attr.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,22 @@ describe('td-headers-attr', function () {
assert.isTrue(check.call(checkContext, node));
});

it('relatedNodes contains each cell only once', function () {
fixtureSetup(`
<table>
<tr> <td id="hi1">hello</td> </tr>
<tr> <td id="hi2">hello</td> </tr>
<tr> <td id="bye" headers="hi1 hi2">goodbye</td> </tr>
</table>'
`);

var node = fixture.querySelector('table');
check.call(checkContext, node);
assert.deepEqual(checkContext._relatedNodes, [
fixture.querySelector('#bye')
]);
});

it('returns false if the header refers to the same cell', function () {
fixtureSetup(
'<table id="hi">' +
Expand Down

0 comments on commit 5d1440e

Please sign in to comment.