Skip to content

fix(uia): resolve selection retrieval failure with tabindex parent#1

Draft
sdp-ncd wants to merge 1 commit intomainfrom
sdp-ncd-patch-1
Draft

fix(uia): resolve selection retrieval failure with tabindex parent#1
sdp-ncd wants to merge 1 commit intomainfrom
sdp-ncd-patch-1

Conversation

@sdp-ncd
Copy link
Owner

@sdp-ncd sdp-ncd commented Feb 6, 2026

[WIP] tested, but not code reviewed

Description

This PR fixes an issue where text selection retrieval via UI Automation fails when the selected text resides within a parent element having tabindex="0".

Problem

In modern web browsers (Chrome/Edge), setting tabindex="0" on a container element (like a <div>) causes it to receive UI Automation focus instead of the specific text node or the document root. Since these container elements often do not implement the TextPattern interface (or delegate selection management to the document root), the previous implementation would fail to retrieve the selection, returning an empty result.

Additionally, the ElementFromPoint strategy would often target leaf nodes (like <span>) that also lack TextPattern support, missing the actual selection managed by ancestor nodes.

Solution

  • Implemented an ancestor traversal ("Walk Up") mechanism. When the initial targeted element (either from mouse position or focus) does not return a valid selection, the code now walks up the UI Automation tree (up to 10 levels) to find an ancestor (typically the Document element) that supports TextPattern and contains the active selection.
  • Refactored the text extraction logic into a reusable lambda helper (TryGetTextFromElement) to reduce code duplication and improve readability.
  • Prioritized the ElementFromPoint strategy with the new walk-up logic to better handle mouse-based selections.

Changes

  • Modified SelectionHook::GetTextViaUIAutomation to include ancestor traversal loops.
  • Added TryGetTextFromElement helper for consistent pattern checking.
  • Updated both mouse-based and focus-based strategies to use the new traversal logic.

Impact

This ensures reliable text selection retrieval in complex HTML structures involving tabindex and nested elements, significantly improving compatibility with web-based applications.

@sdp-ncd sdp-ncd marked this pull request as draft February 6, 2026 10:20
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.

1 participant