From 72144eb44f71b85854b80df44acfabf04e0fade3 Mon Sep 17 00:00:00 2001 From: Abhishek Bindra Date: Tue, 9 Sep 2025 13:52:55 +0530 Subject: [PATCH 1/3] add ancestry path support --- lib/core/utils/dq-element.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/core/utils/dq-element.js b/lib/core/utils/dq-element.js index 93498e91..373675e3 100644 --- a/lib/core/utils/dq-element.js +++ b/lib/core/utils/dq-element.js @@ -246,6 +246,9 @@ DqElement.prototype = { * @return {String} */ get selector() { + if (axe._cache.get('targetFormat') === 'ancestry') { + return this.spec.selector || [getAncestry(this.element)]; + } if (axe._cache.get('runTypeAOpt')) { return this.spec.selector || [generateSelectorWithShadow(this.element)]; } From 53eda3de547e875a8452ead013f45b36b16c6ca3 Mon Sep 17 00:00:00 2001 From: Abhishek Bindra Date: Mon, 15 Sep 2025 20:45:43 +0530 Subject: [PATCH 2/3] add ancestry path support --- lib/core/utils/get-ancestry.js | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/lib/core/utils/get-ancestry.js b/lib/core/utils/get-ancestry.js index b80edb93..a65fdf34 100644 --- a/lib/core/utils/get-ancestry.js +++ b/lib/core/utils/get-ancestry.js @@ -13,8 +13,29 @@ function generateAncestry(node) { nodeName !== 'body' && parent.children.length > 1 ) { - const index = Array.prototype.indexOf.call(parent.children, node) + 1; - nthChild = `:nth-child(${index})`; + let index = 0; + if (parent.nodeName === 'BODY') { + let count = 0; + // Single pass over siblings: count valid children & locate node position. + for ( + let sib = parent.firstElementChild; + sib; + sib = sib.nextElementSibling + ) { + if (sib.hasAttribute('data-percy-injected')) { + continue; + } + count++; + if (sib === node) { + index = count; + break; + } + } + nthChild = count > 1 ? `:nth-child(${index})` : ''; + } else { + index = Array.prototype.indexOf.call(parent.children, node) + 1; + nthChild = `:nth-child(${index})`; + } } return generateAncestry(parent) + ' > ' + nodeName + nthChild; From 1cfebcb25ec893b0ffc97f17da8e84c5d3f84839 Mon Sep 17 00:00:00 2001 From: Abhishek Bindra Date: Mon, 15 Sep 2025 21:24:15 +0530 Subject: [PATCH 3/3] add ancestry path support --- lib/core/utils/get-ancestry.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/core/utils/get-ancestry.js b/lib/core/utils/get-ancestry.js index a65fdf34..7321a6da 100644 --- a/lib/core/utils/get-ancestry.js +++ b/lib/core/utils/get-ancestry.js @@ -28,7 +28,6 @@ function generateAncestry(node) { count++; if (sib === node) { index = count; - break; } } nthChild = count > 1 ? `:nth-child(${index})` : '';