Skip to content

Commit

Permalink
fix: prevent undefined relatedNodes from halting axe (#3778)
Browse files Browse the repository at this point in the history
* fix: prevent undefined relatedNodes from halting axe

* Add all options
  • Loading branch information
WilcoFiers committed Nov 14, 2022
1 parent d6cef9a commit efefb18
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
14 changes: 7 additions & 7 deletions lib/core/reporters/helpers/process-aggregate.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ function normalizeRelatedNodes(node, options) {
.forEach(checkRes => {
checkRes.relatedNodes = checkRes.relatedNodes.map(relatedNode => {
var res = {
html: relatedNode.source
html: relatedNode?.source ?? 'Undefined'
};
if (options.elementRef && !relatedNode.fromFrame) {
res.element = relatedNode.element;
if (options.elementRef && !relatedNode?.fromFrame) {
res.element = relatedNode?.element ?? null;
}
if (options.selectors !== false || relatedNode.fromFrame) {
res.target = relatedNode.selector;
if (options.selectors !== false || relatedNode?.fromFrame) {
res.target = relatedNode?.selector ?? ':root';
}
if (options.ancestry) {
res.ancestry = relatedNode.ancestry;
res.ancestry = relatedNode?.ancestry ?? ':root';
}
if (options.xpath) {
res.xpath = relatedNode.xpath;
res.xpath = relatedNode?.xpath ?? '/';
}
return res;
});
Expand Down
18 changes: 18 additions & 0 deletions test/core/reporters/helpers/process-aggregate.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,24 @@ describe('helpers.processAggregate', function () {
assert.isUndefined(ruleResult.nodes[0].node);
});

it('handles when a relatedNode is undefined', () => {
// Add undefined to failed-rule
results[1].violations[0].any[0].relatedNodes.unshift(undefined);
const resultObject = helpers.processAggregate(results, {
xpath: true,
elementRef: true,
ancestry: true
});
const { relatedNodes } = resultObject.violations[0].nodes[0].any[0];
assert.deepEqual(relatedNodes[0], {
html: 'Undefined',
target: ':root',
ancestry: ':root',
xpath: '/',
element: null
});
});

describe('`options` argument', function () {
describe('`resultTypes` option', function () {
it('should reduce the unwanted result types to 1 in the `resultObject`', function () {
Expand Down

0 comments on commit efefb18

Please sign in to comment.