Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prevent undefined relatedNodes from halting axe #3778

Merged
merged 2 commits into from
Nov 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would have made this all lowercase so it looks like A Javascript stringification of a variable with a value of undefined, but nbd

};
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