diff --git a/packages/shared/src/extractor/util.ts b/packages/shared/src/extractor/util.ts index 5b779d04..8f8ecd59 100644 --- a/packages/shared/src/extractor/util.ts +++ b/packages/shared/src/extractor/util.ts @@ -330,6 +330,19 @@ export function visibleRect( // check if the element is hidden by an ancestor let parent: HTMLElement | Node | null = el; + const parentUntilNonStatic = (currentNode: HTMLElement | Node | null) => { + // find a parent element that is not static + let parent = currentNode?.parentElement; + while (parent) { + const style = currentWindow.getComputedStyle(parent); + if (style.position !== 'static') { + return parent; + } + parent = parent.parentElement; + } + return null; + }; + while (parent && parent !== currentDocument.body) { if (!(parent instanceof currentWindow.HTMLElement)) { parent = parent.parentElement; @@ -354,10 +367,15 @@ export function visibleRect( } } // if the parent is a fixed element, stop the search - if (parentStyle.position === 'fixed') { + if (parentStyle.position === 'fixed' || parentStyle.position === 'sticky') { break; } - parent = parent.parentElement; + + if (parentStyle.position === 'absolute') { + parent = parentUntilNonStatic(parent); + } else { + parent = parent.parentElement; + } } return { diff --git a/packages/web-integration/tests/unit-test/__snapshots__/web-extractor.test.ts.snap b/packages/web-integration/tests/unit-test/__snapshots__/web-extractor.test.ts.snap index fa3e86f1..a1c221cb 100644 --- a/packages/web-integration/tests/unit-test/__snapshots__/web-extractor.test.ts.snap +++ b/packages/web-integration/tests/unit-test/__snapshots__/web-extractor.test.ts.snap @@ -602,6 +602,20 @@ exports[`extractor > basic 1`] = ` }, "content": "", }, + { + "attributes": { + "htmlTagName": "
", + "nodeType": "TEXT Node", + }, + "content": "AAA", + }, + { + "attributes": { + "htmlTagName": "
", + "nodeType": "TEXT Node", + }, + "content": "This should be collected", + }, { "attributes": { "htmlTagName": "", @@ -2938,6 +2952,72 @@ exports[`extractor > basic 2`] = ` "children": [], "node": null, }, + { + "children": [ + { + "children": [], + "node": null, + }, + { + "children": [ + { + "children": [], + "node": { + "attributes": { + "htmlTagName": "
", + "nodeType": "TEXT Node", + }, + "content": "AAA", + "indexId": 78, + }, + }, + ], + "node": null, + }, + { + "children": [], + "node": null, + }, + { + "children": [ + { + "children": [], + "node": null, + }, + ], + "node": null, + }, + { + "children": [], + "node": null, + }, + { + "children": [ + { + "children": [], + "node": { + "attributes": { + "htmlTagName": "
", + "nodeType": "TEXT Node", + }, + "content": "This should be collected", + "indexId": 79, + }, + }, + ], + "node": null, + }, + { + "children": [], + "node": null, + }, + ], + "node": null, + }, + { + "children": [], + "node": null, + }, ], "node": null, }, @@ -2957,7 +3037,7 @@ exports[`extractor > basic 2`] = ` "nodeType": "TEXT Node", }, "content": "Child Page", - "indexId": 79, + "indexId": 81, }, }, ], @@ -2977,7 +3057,7 @@ exports[`extractor > basic 2`] = ` "nodeType": "TEXT Node", }, "content": "This is a child page.", - "indexId": 80, + "indexId": 82, }, }, ], @@ -2997,7 +3077,7 @@ exports[`extractor > basic 2`] = ` "nodeType": "TEXT Node", }, "content": "Click me", - "indexId": 81, + "indexId": 83, }, }, ], diff --git a/packages/web-integration/tests/unit-test/fixtures/web-extractor/index.html b/packages/web-integration/tests/unit-test/fixtures/web-extractor/index.html index 30a84251..22248ed5 100644 --- a/packages/web-integration/tests/unit-test/fixtures/web-extractor/index.html +++ b/packages/web-integration/tests/unit-test/fixtures/web-extractor/index.html @@ -348,6 +348,17 @@

Form

+ + +
+
AAA
+
BBB
+
+ This should be collected +
+ + +
\ No newline at end of file diff --git a/packages/web-integration/tests/unit-test/fixtures/web-extractor/input.png b/packages/web-integration/tests/unit-test/fixtures/web-extractor/input.png index 219b3a70..a48a29d9 100644 Binary files a/packages/web-integration/tests/unit-test/fixtures/web-extractor/input.png and b/packages/web-integration/tests/unit-test/fixtures/web-extractor/input.png differ diff --git a/packages/web-integration/tests/unit-test/fixtures/web-extractor/output.png b/packages/web-integration/tests/unit-test/fixtures/web-extractor/output.png index 896880f6..b9a9ec3e 100644 Binary files a/packages/web-integration/tests/unit-test/fixtures/web-extractor/output.png and b/packages/web-integration/tests/unit-test/fixtures/web-extractor/output.png differ