Skip to content

Commit 713d144

Browse files
authored
Merge 53e4f57 into 5bd8872
2 parents 5bd8872 + 53e4f57 commit 713d144

5 files changed

Lines changed: 164 additions & 51 deletions

File tree

source/NVDAObjects/UIA/__init__.py

Lines changed: 80 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -456,33 +456,43 @@ def _getTextWithFields_text(self,textRange,formatConfig,UIAFormatUnits=None):
456456
@type UIAFormatUnits: List of UI Automation Text Units or None
457457
@rtype: a Generator yielding L{textInfos.FieldCommand} objects containing L{textInfos.FormatField} objects, and text strings.
458458
"""
459-
log.debug("_getTextWithFields_text start")
459+
debug = UIAHandler._isDebug() and log.isEnabledFor(log.DEBUG)
460+
if debug:
461+
log.debug("_getTextWithFields_text start")
460462
if UIAFormatUnits:
461463
unit=UIAFormatUnits[0]
462464
furtherUIAFormatUnits=UIAFormatUnits[1:]
463465
else:
464466
# Fetching text and formatting from the entire range will be tried once before any possible splitting.
465467
unit=None
466468
furtherUIAFormatUnits=self.UIAFormatUnits if UIAFormatUnits is None else []
467-
log.debug("Walking by unit %s"%unit)
468-
log.debug("With further units of: %s"%furtherUIAFormatUnits)
469+
if debug:
470+
log.debug(
471+
f"Walking by unit {unit}, "
472+
f"with further units of: {furtherUIAFormatUnits}"
473+
)
469474
rangeIter=iterUIARangeByUnit(textRange,unit) if unit is not None else [textRange]
470475
for tempRange in rangeIter:
471476
text=self._getTextFromUIARange(tempRange) or ""
472477
if text:
473-
log.debug("Chunk has text. Fetching formatting")
478+
if debug:
479+
log.debug("Chunk has text. Fetching formatting")
474480
try:
475481
field=self._getFormatFieldAtRange(tempRange,formatConfig,ignoreMixedValues=len(furtherUIAFormatUnits)==0)
476482
except UIAMixedAttributeError:
477-
log.debug("Mixed formatting. Trying higher resolution unit")
483+
if debug:
484+
log.debug("Mixed formatting. Trying higher resolution unit")
478485
for subfield in self._getTextWithFields_text(tempRange,formatConfig,UIAFormatUnits=furtherUIAFormatUnits):
479486
yield subfield
480-
log.debug("Done yielding higher resolution unit")
487+
if debug:
488+
log.debug("Done yielding higher resolution unit")
481489
continue
482-
log.debug("Yielding formatting and text")
490+
if debug:
491+
log.debug("Yielding formatting and text")
483492
yield field
484493
yield text
485-
log.debug("Done _getTextWithFields_text")
494+
if debug:
495+
log.debug("Done _getTextWithFields_text")
486496

487497
def _getTextWithFieldsForUIARange(self,rootElement,textRange,formatConfig,includeRoot=False,alwaysWalkAncestors=True,recurseChildren=True,_rootElementClipped=(True,True)):
488498
"""
@@ -503,8 +513,8 @@ def _getTextWithFieldsForUIARange(self,rootElement,textRange,formatConfig,includ
503513
@type _rootElementClipped: 2-tuple
504514
@rtype: A generator that yields L{textInfo.FieldCommand} objects and text strings.
505515
"""
506-
507-
if log.isEnabledFor(log.DEBUG):
516+
debug = UIAHandler._isDebug() and log.isEnabledFor(log.DEBUG)
517+
if debug:
508518
log.debug("_getTextWithFieldsForUIARange")
509519
log.debug("rootElement: %s"%rootElement.currentLocalizedControlType if rootElement else None)
510520
log.debug("full text: %s"%textRange.getText(-1))
@@ -519,69 +529,78 @@ def _getTextWithFieldsForUIARange(self,rootElement,textRange,formatConfig,includ
519529
recurseChildren=False
520530
parentElements=[]
521531
if alwaysWalkAncestors:
522-
log.debug("Fetching parents starting from enclosingElement")
532+
if debug:
533+
log.debug("Fetching parents starting from enclosingElement")
523534
try:
524535
parentElement=getEnclosingElementWithCacheFromUIATextRange(textRange,self._controlFieldUIACacheRequest)
525536
except COMError:
526537
parentElement=None
527538
while parentElement:
528539
isRoot=UIAHandler.handler.clientObject.compareElements(parentElement,rootElement)
529540
if isRoot:
530-
log.debug("Hit root")
541+
if debug:
542+
log.debug("Hit root")
531543
parentElements.append((parentElement,_rootElementClipped))
532544
break
533545
else:
534-
if log.isEnabledFor(log.DEBUG):
546+
if debug:
535547
log.debug("parentElement: %s"%parentElement.currentLocalizedControlType)
536548
try:
537549
parentRange=self.obj.UIATextPattern.rangeFromChild(parentElement)
538550
except COMError:
539551
parentRange=None
540552
if not parentRange:
541-
log.debug("parentRange is NULL. Breaking")
553+
if debug:
554+
log.debug("parentRange is NULL. Breaking")
542555
break
543556
clippedStart=textRange.CompareEndpoints(UIAHandler.TextPatternRangeEndpoint_Start,parentRange,UIAHandler.TextPatternRangeEndpoint_Start)>0
544557
clippedEnd=textRange.CompareEndpoints(UIAHandler.TextPatternRangeEndpoint_End,parentRange,UIAHandler.TextPatternRangeEndpoint_End)<0
545558
parentElements.append((parentElement,(clippedStart,clippedEnd)))
546559
parentElement=UIAHandler.handler.baseTreeWalker.getParentElementBuildCache(parentElement,self._controlFieldUIACacheRequest)
547560
else:
548561
parentElements.append((rootElement,_rootElementClipped))
549-
log.debug("Done fetching parents")
562+
if debug:
563+
log.debug("Done fetching parents")
550564
enclosingElement=parentElements[0][0] if parentElements else rootElement
551565
if not includeRoot and parentElements:
552566
del parentElements[-1]
553567
parentFields=[]
554-
log.debug("Generating controlFields for parents")
568+
if debug:
569+
log.debug("Generating controlFields for parents")
555570
windowHandle=self.obj.windowHandle
556571
controlFieldNVDAObjectClass=self.controlFieldNVDAObjectClass
557572
for index,(parentElement,parentClipped) in enumerate(parentElements):
558-
if log.isEnabledFor(log.DEBUG):
573+
if debug:
559574
log.debug("parentElement: %s"%parentElement.currentLocalizedControlType)
560575
startOfNode=not parentClipped[0]
561576
endOfNode=not parentClipped[1]
562577
try:
563578
obj=controlFieldNVDAObjectClass(windowHandle=windowHandle,UIAElement=parentElement,initialUIACachedPropertyIDs=self._controlFieldUIACachedPropertyIDs)
564579
field=self._getControlFieldForObject(obj,isEmbedded=(index==0 and not recurseChildren),startOfNode=startOfNode,endOfNode=endOfNode)
565580
except LookupError:
566-
log.debug("Failed to fetch controlField data for parentElement. Breaking")
581+
if debug:
582+
log.debug("Failed to fetch controlField data for parentElement. Breaking")
567583
continue
568584
if not field:
569585
continue
570586
parentFields.append(field)
571-
log.debug("Done generating controlFields for parents")
572-
log.debug("Yielding control starts for parents")
587+
if debug:
588+
log.debug("Done generating controlFields for parents")
589+
log.debug("Yielding control starts for parents")
573590
for field in reversed(parentFields):
574591
yield textInfos.FieldCommand("controlStart",field)
575-
log.debug("Done yielding control starts for parents")
592+
if debug:
593+
log.debug("Done yielding control starts for parents")
576594
del parentElements
577-
log.debug("Yielding balanced fields for textRange")
595+
if debug:
596+
log.debug("Yielding balanced fields for textRange")
578597
# Move through the text range, collecting text and recursing into children
579598
#: This variable is used to span lengths of plain text between child ranges as we iterate over getChildren
580599
childCount=childElements.length if recurseChildren else 0
581600
if childCount>0:
582601
tempRange=textRange.clone()
583602
tempRange.MoveEndpointByRange(UIAHandler.TextPatternRangeEndpoint_End,tempRange,UIAHandler.TextPatternRangeEndpoint_Start)
584-
if log.isEnabledFor(log.DEBUG):
603+
if debug:
585604
log.debug("Child count: %s"%childElements.length)
586605
log.debug("Walking children")
587606
lastChildIndex=childCount-1
@@ -591,69 +610,90 @@ def _getTextWithFieldsForUIARange(self,rootElement,textRange,formatConfig,includ
591610
for index in range(childCount):
592611
childElement=childElements.getElement(index)
593612
if not childElement or UIAHandler.handler.clientObject.compareElements(childElement,enclosingElement):
594-
log.debug("NULL childElement. Skipping")
613+
if debug:
614+
log.debug("NULL childElement. Skipping")
595615
continue
596616
if rootElementControlType==UIAHandler.UIA_DataItemControlTypeId:
597617
# #9090: MS Word has a rare bug where a child of a table cell's UIA textRange can be its containing page.
598618
# At very least stop the infinite recursion.
599619
childAutomationID=childElement.cachedAutomationId or ""
600620
if childAutomationID.startswith('UIA_AutomationId_Word_Page_'):
601621
continue
602-
if log.isEnabledFor(log.DEBUG):
622+
if debug:
603623
log.debug("Fetched child %s (%s)"%(index,childElement.currentLocalizedControlType))
604624
try:
605625
childRange=documentTextPattern.rangeFromChild(childElement)
606626
except COMError as e:
607-
log.debug("rangeFromChild failed with %s"%e)
627+
if debug:
628+
log.debug(f"rangeFromChild failed with {e}")
608629
childRange=None
609630
if not childRange:
610-
log.debug("NULL childRange. Skipping")
631+
if debug:
632+
log.debug("NULL childRange. Skipping")
611633
continue
612634
clippedStart=clippedEnd=False
613635
if index==lastChildIndex and childRange.CompareEndpoints(UIAHandler.TextPatternRangeEndpoint_Start,textRange,UIAHandler.TextPatternRangeEndpoint_End)>=0:
614-
log.debug("Child at or past end of textRange. Breaking")
636+
if debug:
637+
log.debug("Child at or past end of textRange. Breaking")
615638
break
616639
if index==lastChildIndex:
617640
lastChildEndDelta=childRange.CompareEndpoints(UIAHandler.TextPatternRangeEndpoint_End,textRange,UIAHandler.TextPatternRangeEndpoint_End)
618641
if lastChildEndDelta>0:
619-
log.debug("textRange ended part way through the child. Crop end of childRange to fit")
642+
if debug:
643+
log.debug(
644+
"textRange ended part way through the child. "
645+
"Crop end of childRange to fit"
646+
)
620647
childRange.MoveEndpointByRange(UIAHandler.TextPatternRangeEndpoint_End,textRange,UIAHandler.TextPatternRangeEndpoint_End)
621648
clippedEnd=True
622649
childStartDelta=childRange.CompareEndpoints(UIAHandler.TextPatternRangeEndpoint_Start,tempRange,UIAHandler.TextPatternRangeEndpoint_End)
623650
if childStartDelta>0:
624651
# plain text before this child
625652
tempRange.MoveEndpointByRange(UIAHandler.TextPatternRangeEndpoint_End,childRange,UIAHandler.TextPatternRangeEndpoint_Start)
626-
log.debug("Plain text before child")
653+
if debug:
654+
log.debug("Plain text before child")
627655
for field in self._getTextWithFields_text(tempRange,formatConfig):
628656
yield field
629657
elif childStartDelta<0:
630-
log.debug("textRange started part way through child. Cropping Start of child range to fit" )
658+
if debug:
659+
log.debug(
660+
"textRange started part way through child. "
661+
"Cropping Start of child range to fit"
662+
)
631663
childRange.MoveEndpointByRange(UIAHandler.TextPatternRangeEndpoint_Start,tempRange,UIAHandler.TextPatternRangeEndpoint_End)
632664
clippedStart=True
633665
if (index==0 or index==lastChildIndex) and childRange.CompareEndpoints(UIAHandler.TextPatternRangeEndpoint_Start,childRange,UIAHandler.TextPatternRangeEndpoint_End)==0:
634-
log.debug("childRange is degenerate. Skipping")
666+
if debug:
667+
log.debug("childRange is degenerate. Skipping")
635668
continue
636-
log.debug("Recursing into child %s"%index)
669+
if debug:
670+
log.debug(f"Recursing into child {index}")
637671
for field in self._getTextWithFieldsForUIARange(childElement,childRange,formatConfig,includeRoot=True,alwaysWalkAncestors=False,_rootElementClipped=(clippedStart,clippedEnd)):
638672
yield field
639-
log.debug("Done recursing into child %s"%index)
673+
if debug:
674+
log.debug(f"Done recursing into child {index}")
640675
tempRange.MoveEndpointByRange(UIAHandler.TextPatternRangeEndpoint_Start,childRange,UIAHandler.TextPatternRangeEndpoint_End)
641-
log.debug("children done")
676+
if debug:
677+
log.debug("children done")
642678
# Plain text after the final child
643679
if tempRange.CompareEndpoints(UIAHandler.TextPatternRangeEndpoint_Start,textRange,UIAHandler.TextPatternRangeEndpoint_End)<0:
644680
tempRange.MoveEndpointByRange(UIAHandler.TextPatternRangeEndpoint_End,textRange,UIAHandler.TextPatternRangeEndpoint_End)
645-
log.debug("Yielding final text")
681+
if debug:
682+
log.debug("Yielding final text")
646683
for field in self._getTextWithFields_text(tempRange,formatConfig):
647684
yield field
648685
else: #no children
649-
log.debug("no children")
650-
log.debug("Yielding text")
686+
if debug:
687+
log.debug("no children")
688+
log.debug("Yielding text")
651689
for field in self._getTextWithFields_text(textRange,formatConfig):
652690
yield field
653691
for field in parentFields:
654-
log.debug("Yielding controlEnd for parentElement")
692+
if debug:
693+
log.debug("Yielding controlEnd for parentElement")
655694
yield textInfos.FieldCommand("controlEnd",field)
656-
log.debug("_getTextWithFieldsForUIARange end")
695+
if debug:
696+
log.debug("_getTextWithFieldsForUIARange end")
657697

658698
def getTextWithFields(self,formatConfig=None):
659699
if not formatConfig:

source/UIAHandler.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
# Because Windows 7 SP1 (NT 6.1) or later is supported, just assume UIA can be used unless told otherwise.
1616
try:
1717
from _UIAHandler import *
18+
from _UIAHandler import _isDebug
1819
isUIAAvailable=True
1920
except ImportError:
2021
log.debugWarning("Unable to import _UIAHandler",exc_info=True)

0 commit comments

Comments
 (0)