Skip to content

Commit fbfcdbf

Browse files
committed
fix: correctly collect elements in absolute container
1 parent 38eb53d commit fbfcdbf

File tree

5 files changed

+114
-5
lines changed

5 files changed

+114
-5
lines changed

packages/shared/src/extractor/util.ts

+20-2
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,19 @@ export function visibleRect(
330330

331331
// check if the element is hidden by an ancestor
332332
let parent: HTMLElement | Node | null = el;
333+
const parentUntilNonStatic = (currentNode: HTMLElement | Node | null) => {
334+
// find a parent element that is not static
335+
let parent = currentNode?.parentElement;
336+
while (parent) {
337+
const style = currentWindow.getComputedStyle(parent);
338+
if (style.position !== 'static') {
339+
return parent;
340+
}
341+
parent = parent.parentElement;
342+
}
343+
return null;
344+
};
345+
333346
while (parent && parent !== currentDocument.body) {
334347
if (!(parent instanceof currentWindow.HTMLElement)) {
335348
parent = parent.parentElement;
@@ -354,10 +367,15 @@ export function visibleRect(
354367
}
355368
}
356369
// if the parent is a fixed element, stop the search
357-
if (parentStyle.position === 'fixed') {
370+
if (parentStyle.position === 'fixed' || parentStyle.position === 'sticky') {
358371
break;
359372
}
360-
parent = parent.parentElement;
373+
374+
if (parentStyle.position === 'absolute') {
375+
parent = parentUntilNonStatic(parent);
376+
} else {
377+
parent = parent.parentElement;
378+
}
361379
}
362380

363381
return {

packages/web-integration/tests/unit-test/__snapshots__/web-extractor.test.ts.snap

+83-3
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,20 @@ exports[`extractor > basic 1`] = `
602602
},
603603
"content": "",
604604
},
605+
{
606+
"attributes": {
607+
"htmlTagName": "<div>",
608+
"nodeType": "TEXT Node",
609+
},
610+
"content": "AAA",
611+
},
612+
{
613+
"attributes": {
614+
"htmlTagName": "<div>",
615+
"nodeType": "TEXT Node",
616+
},
617+
"content": "This should be collected",
618+
},
605619
{
606620
"attributes": {
607621
"htmlTagName": "",
@@ -2938,6 +2952,72 @@ exports[`extractor > basic 2`] = `
29382952
"children": [],
29392953
"node": null,
29402954
},
2955+
{
2956+
"children": [
2957+
{
2958+
"children": [],
2959+
"node": null,
2960+
},
2961+
{
2962+
"children": [
2963+
{
2964+
"children": [],
2965+
"node": {
2966+
"attributes": {
2967+
"htmlTagName": "<div>",
2968+
"nodeType": "TEXT Node",
2969+
},
2970+
"content": "AAA",
2971+
"indexId": 78,
2972+
},
2973+
},
2974+
],
2975+
"node": null,
2976+
},
2977+
{
2978+
"children": [],
2979+
"node": null,
2980+
},
2981+
{
2982+
"children": [
2983+
{
2984+
"children": [],
2985+
"node": null,
2986+
},
2987+
],
2988+
"node": null,
2989+
},
2990+
{
2991+
"children": [],
2992+
"node": null,
2993+
},
2994+
{
2995+
"children": [
2996+
{
2997+
"children": [],
2998+
"node": {
2999+
"attributes": {
3000+
"htmlTagName": "<div>",
3001+
"nodeType": "TEXT Node",
3002+
},
3003+
"content": "This should be collected",
3004+
"indexId": 79,
3005+
},
3006+
},
3007+
],
3008+
"node": null,
3009+
},
3010+
{
3011+
"children": [],
3012+
"node": null,
3013+
},
3014+
],
3015+
"node": null,
3016+
},
3017+
{
3018+
"children": [],
3019+
"node": null,
3020+
},
29413021
],
29423022
"node": null,
29433023
},
@@ -2957,7 +3037,7 @@ exports[`extractor > basic 2`] = `
29573037
"nodeType": "TEXT Node",
29583038
},
29593039
"content": "Child Page",
2960-
"indexId": 79,
3040+
"indexId": 81,
29613041
},
29623042
},
29633043
],
@@ -2977,7 +3057,7 @@ exports[`extractor > basic 2`] = `
29773057
"nodeType": "TEXT Node",
29783058
},
29793059
"content": "This is a child page.",
2980-
"indexId": 80,
3060+
"indexId": 82,
29813061
},
29823062
},
29833063
],
@@ -2997,7 +3077,7 @@ exports[`extractor > basic 2`] = `
29973077
"nodeType": "TEXT Node",
29983078
},
29993079
"content": "Click me",
3000-
"indexId": 81,
3080+
"indexId": 83,
30013081
},
30023082
},
30033083
],

packages/web-integration/tests/unit-test/fixtures/web-extractor/index.html

+11
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,17 @@ <h3>Form</h3>
348348
<iframe src="https://www.bytedance.com" frameborder="0" width="400px" height="200px"></iframe>
349349
</div>
350350
</div>
351+
352+
353+
<div style="overflow: hidden; width: 100px; height: 100px; background-color: #ccc;">
354+
<div style="width: 120px; height: 120px; background-color: #EEE;">AAA</div>
355+
<div style="width: 120px; height: 120px; background-color: #EEE;">BBB</div>
356+
<div style="position: absolute; bottom: 200px; right: 0; width: 120px; height: 120px; background-color: #CCC;">
357+
This should be collected
358+
</div>
359+
360+
361+
</div>
351362
</body>
352363

353364
</html>
Loading
Loading

0 commit comments

Comments
 (0)