Skip to content

Fix exception during textInfos.moveToCodepointOffset when some characters correspond to 0 pythonic characters#16447

Merged
seanbudd merged 2 commits into
nvaccess:betafrom
mltony:codepointZero
May 10, 2024
Merged

Fix exception during textInfos.moveToCodepointOffset when some characters correspond to 0 pythonic characters#16447
seanbudd merged 2 commits into
nvaccess:betafrom
mltony:codepointZero

Conversation

@mltony

@mltony mltony commented Apr 25, 2024

Copy link
Copy Markdown
Contributor

Link to issue number:

Partially closes #16406.

Summary of the issue:

When trying to navigate by style an exception is thrown:

RuntimeError: Unable to find desired offset in TextInfo.

Description of user facing changes

N/A

Description of development approach

Investigation showed that in Outlook with word UIA enabled, sometimes there are characters that translate into 0 codepoint characters. To illustrate this further:

>>> p=focus.treeInterceptor.makeTextInfo('caret')
>>> p.expand('paragraph')
>>> p.text
'View in browser'
>>> p.collapse(end=True)
>>> p.move('character', -1, 'start')
-1
>>> p.text
''
>>> len(p.text)
0

E.g. we move a collapsed text info start by -1 character, and the resulting textInfo still appears to be empty- at least its Python text is empty.
I anticipated this to happen somewhere, but didn't encounter a single example when I was implementing moveToCodepointOffset function, so I missed one edge case. Now fixing this edge case. Also adding unit tests.

Testing strategy:

Unit test.
Tested on test case from the original issue with UIA enabled.

Known issues with pull request:

N/A

Code Review Checklist:

  • Documentation:
    • Change log entry
    • User Documentation
    • Developer / Technical Documentation
    • Context sensitive help for GUI changes
  • Testing:
    • Unit tests
    • System (end to end) tests
    • Manual testing
  • UX of all users considered:
    • Speech
    • Braille
    • Low Vision
    • Different web browsers
    • Localization in other languages / culture than English
  • API is compatible with existing add-ons.
  • Security precautions taken.

@mltony mltony marked this pull request as ready for review April 25, 2024 02:56
@mltony mltony requested a review from a team as a code owner April 25, 2024 02:56
@mltony mltony requested a review from seanbudd April 25, 2024 02:56
@seanbudd seanbudd changed the base branch from master to beta April 30, 2024 00:09
@seanbudd seanbudd changed the title Fix exception during textInfos.moveToCodepointOffset when some charac… Fix exception during textInfos.moveToCodepointOffset when some characters correspond to 0 pythonic characters Apr 30, 2024
@seanbudd seanbudd added the conceptApproved Similar 'triaged' for issues, PR accepted in theory, implementation needs review. label Apr 30, 2024
@seanbudd seanbudd added this to the 2024.2 milestone May 1, 2024
seanbudd
seanbudd previously approved these changes May 1, 2024
Comment thread source/textInfos/__init__.py Outdated
@LeonarddeR

Copy link
Copy Markdown
Collaborator

@mltony Could you please look into the following issue, which seems related? Still reproducible wit this pr?

In word, type: 1. Hello

Then

>>> info = focus.makeTextInfo("all")
>>> info.text
'1. hello'
>>> info.moveToCodepointOffset(0)
<NVDAObjects.UIA.wordDocument.WordDocumentTextInfo object at 0x0AF75050>
>>> info.moveToCodepointOffset(1)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "D:\Development\A11y\nvda\source\textInfos\__init__.py", line 794, in moveToCodepointOffset
    raise RuntimeError("Unable to find desired offset in TextInfo.")
RuntimeError: Unable to find desired offset in TextInfo.
>>> info.moveToCodepointOffset(2)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "D:\Development\A11y\nvda\source\textInfos\__init__.py", line 794, in moveToCodepointOffset
    raise RuntimeError("Unable to find desired offset in TextInfo.")
RuntimeError: Unable to find desired offset in TextInfo.
>>> info.moveToCodepointOffset(3)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "D:\Development\A11y\nvda\source\textInfos\__init__.py", line 818, in moveToCodepointOffset
    raise RuntimeError("Unable to find desired offset in TextInfo.")
RuntimeError: Unable to find desired offset in TextInfo.
>>> info.moveToCodepointOffset(4)
<NVDAObjects.UIA.wordDocument.WordDocumentTextInfo object at 0x06DE32F0>

Is this expected behavior, or should the function for values 1, 2 and 3 just return the collapsed start of the textInfo?

@mltony

mltony commented May 7, 2024

Copy link
Copy Markdown
Contributor Author

@LeonarddeR,
That's expected. That's because in MSWord in a list the first character (when moving by textInfos.UNIT_CHARACTER) corresponds to 4 charactters in Python: "1. H". Thus binary search fails as expected.
I would strongly argue against quietly swallowing that exception and trying to resolve it somehow inside moveToCodepointOffset function. For example if we do it, then during sentence navigation we might get stuck at a particular sentence when pressing alt+DownArrow because moveToCodepointOffset would quietly take us a few characters back every time. Dealing with this problem is hard but it needs to be dealt with outside moveToCodepointOffset depending on your use case.
One thing I regret though is raising generic RuntimeError - I should've declared a custom exception class so that it's easy to catch.

@seanbudd seanbudd added blocked/needs-info The issue can not be progressed until more information is provided. and removed queued for merge blocked/needs-info The issue can not be progressed until more information is provided. labels May 10, 2024
@seanbudd seanbudd merged commit 928cf02 into nvaccess:beta May 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

conceptApproved Similar 'triaged' for issues, PR accepted in theory, implementation needs review.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Navigating to different style text in MS outlook, NVDA often cannot find desired offset in TextInfo.

3 participants