Skip to content

Commit 53eda3d

Browse files
author
Abhishek Bindra
committed
add ancestry path support
1 parent 72144eb commit 53eda3d

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

lib/core/utils/get-ancestry.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,29 @@ 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+
break;
32+
}
33+
}
34+
nthChild = count > 1 ? `:nth-child(${index})` : '';
35+
} else {
36+
index = Array.prototype.indexOf.call(parent.children, node) + 1;
37+
nthChild = `:nth-child(${index})`;
38+
}
1839
}
1940

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

0 commit comments

Comments
 (0)