Skip to content
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
Copy Markdown
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