Skip to content

Commit a005703

Browse files
authored
fix(get-ancestry): don't error when there is no parent (#4617)
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.
1 parent fad3266 commit a005703

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

lib/core/utils/get-ancestry.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ function generateAncestry(node) {
99
if (
1010
nodeName !== 'head' &&
1111
nodeName !== 'body' &&
12-
parentNode.children.length > 1
12+
parentNode?.children.length > 1
1313
) {
1414
const index = Array.prototype.indexOf.call(parentNode.children, node) + 1;
1515
nthChild = `:nth-child(${index})`;

test/core/utils/get-ancestry.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ describe('axe.utils.getAncestry', () => {
2626
assert.isNotNull(document.querySelector(sel1));
2727
});
2828

29+
it('should not throw when there is no parent', () => {
30+
const node = document.createElement('section');
31+
assert.doesNotThrow(() => axe.utils.getAncestry(node));
32+
});
33+
2934
it('generates selectors of nested shadow trees', () => {
3035
const node = document.createElement('section');
3136
fixture.appendChild(node);

0 commit comments

Comments
 (0)