Skip to content

Commit 50466fa

Browse files
authored
Merge branch 'main' into AXE-2194-label-content-name-mismatch
2 parents c9da509 + 8a1e104 commit 50466fa

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

lib/core/utils/dq-element.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,9 @@ DqElement.prototype = {
246246
* @return {String}
247247
*/
248248
get selector() {
249+
if (axe._cache.get('targetFormat') === 'ancestry') {
250+
return this.spec.selector || [getAncestry(this.element)];
251+
}
249252
if (axe._cache.get('runTypeAOpt')) {
250253
return this.spec.selector || [generateSelectorWithShadow(this.element)];
251254
}

lib/core/utils/get-ancestry.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,28 @@ function generateAncestry(node) {
1313
nodeName !== 'body' &&
1414
parent.children.length > 1
1515
) {
16-
const index = Array.prototype.indexOf.call(parent.children, node) + 1;
17-
nthChild = `:nth-child(${index})`;
16+
let index = 0;
17+
if (parent.nodeName === 'BODY') {
18+
let count = 0;
19+
// Single pass over siblings: count valid children & locate node position.
20+
for (
21+
let sib = parent.firstElementChild;
22+
sib;
23+
sib = sib.nextElementSibling
24+
) {
25+
if (sib.hasAttribute('data-percy-injected')) {
26+
continue;
27+
}
28+
count++;
29+
if (sib === node) {
30+
index = count;
31+
}
32+
}
33+
nthChild = count > 1 ? `:nth-child(${index})` : '';
34+
} else {
35+
index = Array.prototype.indexOf.call(parent.children, node) + 1;
36+
nthChild = `:nth-child(${index})`;
37+
}
1838
}
1939

2040
return generateAncestry(parent) + ' > ' + nodeName + nthChild;

0 commit comments

Comments
 (0)