Skip to content

Improve element visibility detection#1196

Draft
sammacbeth wants to merge 6 commits intomainfrom
cursor/element-visibility-logic-1d8c
Draft

Improve element visibility detection#1196
sammacbeth wants to merge 6 commits intomainfrom
cursor/element-visibility-logic-1d8c

Conversation

@sammacbeth
Copy link
Copy Markdown
Collaborator

@sammacbeth sammacbeth commented Mar 11, 2026

Task/Issue URL: N/A

Description

Updates the isElementVisible function to use the native Element.checkVisibility() API (with a manual fallback for older browsers), replacing the previous getComputedStyle + offsetParent approach. This correctly detects elements hidden via visibility: hidden, opacity: 0, content-visibility: hidden, and ancestor hiding -- not just display: none.

Additionally, elementVisible in DomActions now temporarily disables the autoconsent prehide stylesheet before checking visibility, so that elements hidden by autoconsent's own prehide (opacity: 0) are still detected as visible during popup detection.

Changes

  • lib/utils.ts: isElementVisible uses checkVisibility({ checkVisibilityCSS: true, checkOpacity: true }) as the primary path, with a manual getComputedStyle fallback for environments without it.
  • lib/dom-actions.ts: elementVisible temporarily disables the autoconsent-prehide stylesheet during visibility checks so prehidden popups are still detected. Removed stale comment.
  • tests-wtr/dom-actions/dom-actions.element-visible.{html,ts}: Added test cases for:
    • display: none, visibility: hidden, visibility: collapse, opacity: 0, content-visibility: hidden
    • Ancestor hiding via visibility: hidden, opacity: 0, content-visibility: hidden
    • Fixed-position elements with various hiding methods
    • visibility: visible child overriding visibility: hidden parent
    • Prehidden elements: visible element stays reported as visible, already-hidden element stays not visible, element remains visible after undoPrehide

Steps to test this PR

  1. Run npm run test:lib
  2. Verify all tests pass, specifically the new "hiding methods" and "prehide interaction" test cases in dom-actions.element-visible.ts

Note: The rule-compaction test failure (expected 2861 to equal 2860) is pre-existing and unrelated to these changes.

…ility CSS properties

The isElementVisible function previously only checked for display:none and
offsetParent, missing elements hidden via other CSS methods. Now it also
checks:
- visibility: hidden/collapse (including inherited from ancestors)
- opacity: 0
- content-visibility: hidden

Added WTR test cases covering all these hiding methods, including edge
cases like fixed-position hidden elements and visible children inside
visibility:hidden parents.

Co-authored-by: Sam Macbeth <sammacbeth@users.noreply.github.com>
@cursor
Copy link
Copy Markdown

cursor bot commented Mar 11, 2026

Cursor Agent can help with this pull request. Just @cursor in comments and I'll start working on changes in this branch.
Learn more about Cursor Agents

@sammacbeth sammacbeth changed the title Element visibility logic Improve element visibility detection Mar 11, 2026
cursoragent and others added 5 commits March 11, 2026 13:14
Co-authored-by: Sam Macbeth <sammacbeth@users.noreply.github.com>
…isElementVisible

Co-authored-by: Sam Macbeth <sammacbeth@users.noreply.github.com>
…llback

Replace the manual getComputedStyle-based checks with the native
checkVisibility() API (checkVisibilityCSS + checkOpacity), falling back
to the manual approach for browsers that lack it. This correctly handles
ancestor opacity:0 and ancestor content-visibility:hidden, which the
previous implementation missed.

Add test cases for ancestor hiding via opacity:0 and
content-visibility:hidden. Correct the content-visibility:hidden
element-self test: the element's own box is still visible, only its
content is hidden, so checkVisibility() rightly returns true.

Remove stale 'check for display: none' comment in dom-actions.ts.

Co-authored-by: Sam Macbeth <sammacbeth@users.noreply.github.com>
elementVisible now temporarily disables the autoconsent-prehide
stylesheet before checking visibility, so that elements hidden by
autoconsent's own prehide (opacity: 0) are still detected as visible
during popup detection. Elements hidden by other means remain correctly
reported as not visible.

Added test cases covering:
- prehidden visible element is still reported as visible
- prehidden already-hidden element is still reported as not visible
- element remains visible after undoPrehide

Co-authored-by: Sam Macbeth <sammacbeth@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants