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(get-ancestry): don't error when there is no parent #4617

Merged
merged 2 commits into from
Oct 17, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/core/utils/get-ancestry.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function generateAncestry(node) {
if (
nodeName !== 'head' &&
nodeName !== 'body' &&
parentNode.children.length > 1
parentNode?.children.length > 1
Copy link
Contributor

Choose a reason for hiding this comment

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

I didn't realize that the optional chaining operation short circuited like this; I got worried that this would need to be parentNode?.children?.length, but it turns out this is fine

) {
const index = Array.prototype.indexOf.call(parentNode.children, node) + 1;
nthChild = `:nth-child(${index})`;
Expand Down
5 changes: 5 additions & 0 deletions test/core/utils/get-ancestry.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ describe('axe.utils.getAncestry', () => {
assert.isNotNull(document.querySelector(sel1));
});

it('should not throw when there is no parent', () => {
const node = document.createElement('section');
assert.doesNotThrow(() => axe.utils.getAncestry(node));
});

it('generates selectors of nested shadow trees', () => {
const node = document.createElement('section');
fixture.appendChild(node);
Expand Down
Loading