From a0057039072f68bd605e8bacdca64692d57f612e Mon Sep 17 00:00:00 2001 From: Steven Lambert <2433219+straker@users.noreply.github.com> Date: Thu, 17 Oct 2024 15:07:07 -0600 Subject: [PATCH] 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. --- lib/core/utils/get-ancestry.js | 2 +- test/core/utils/get-ancestry.js | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/core/utils/get-ancestry.js b/lib/core/utils/get-ancestry.js index f3ed38d272..5652c69ac0 100644 --- a/lib/core/utils/get-ancestry.js +++ b/lib/core/utils/get-ancestry.js @@ -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})`; diff --git a/test/core/utils/get-ancestry.js b/test/core/utils/get-ancestry.js index 071853fb8d..8f570e907a 100644 --- a/test/core/utils/get-ancestry.js +++ b/test/core/utils/get-ancestry.js @@ -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);