Skip to content

Commit

Permalink
fix(get-ancestry): don't error when there is no parent (#4617)
Browse files Browse the repository at this point in the history
Seems I removed the conditional check on the children lookup during a
refactor. Adding it back in and ensuring there's a test so this doesn't
happen again.
  • Loading branch information
straker authored Oct 17, 2024
1 parent fad3266 commit a005703
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
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
) {
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

0 comments on commit a005703

Please sign in to comment.