Skip to content

Commit

Permalink
fix: cater for axe response multiarray of result target object (#690)
Browse files Browse the repository at this point in the history
  • Loading branch information
navateja-alagam authored May 29, 2024
1 parent 2eca6d2 commit 353ccd0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
17 changes: 17 additions & 0 deletions packages/format/__tests__/filter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,23 @@ describe('a11y results filter', () => {
expect(ruleIDs.filter((ruleID) => ruleID !== validRule)).toStrictEqual(filteredRuleIDs);
});

it('should filter results appropriately for exception list with multi-array result target', () => {
const modifiedViolations: AxeResults = JSON.parse(JSON.stringify(violations)) as AxeResults;
modifiedViolations.forEach((violation) => {
violation.nodes.forEach((node) => {
node.target = [node.target];
});
});
const validRule = 'document-title'; // Rule with valid css selector that matches violations
const mixedExceptionList = { 'document-title': ['html'], 'link-name': ['html'], 'bypass': ['a'] };
const ruleIDs = modifiedViolations.map((violation) => violation.id);
const filteredViolations = exceptionListFilter(modifiedViolations, mixedExceptionList);
const filteredRuleIDs = filteredViolations.map((violation) => violation.id);
expect(filteredRuleIDs).not.toContain(validRule);
expect(ruleIDs).toContain(validRule);
expect(ruleIDs.filter((ruleID) => ruleID !== validRule)).toStrictEqual(filteredRuleIDs);
});

it('should filter violations based on selector keywords', () => {
// add ancestry keys
mockViolations[0].nodes[0]['ancestry'] = [
Expand Down
17 changes: 14 additions & 3 deletions packages/format/src/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,20 @@ export function exceptionListFilter(violations: AxeResults, exceptionList: Excep
filteredViolations.push(violation);
} else {
for (const result of violation.nodes) {
const filteredResults = result.target.filter(
(cssSelector) => !exceptionList[violation.id].includes(cssSelector)
);
const filteredResults = [];
result.target.forEach((cssSelectorItem) => {
if (Array.isArray(cssSelectorItem)) {
cssSelectorItem.forEach((cssSelector) => {
if (!exceptionList[violation.id].includes(cssSelector)) {
filteredResults.push(cssSelector);
}
});
} else {
if (!exceptionList[violation.id].includes(cssSelectorItem)) {
filteredResults.push(cssSelectorItem);
}
}
});
if (filteredResults.length > 0) {
filteredViolations.push(violation);
}
Expand Down

0 comments on commit 353ccd0

Please sign in to comment.