Repository: nvaccess/nvda

Issue:
Strange behavior regarding font color reporting by NVDA
### Steps to reproduce:
1. enable font color reporting in document setting of nvda
2. open a word file and create a table, and filled it with some text
3. move between table cells using left or right arrow, even though the color of the fonts has no change, NVDA will still report the color

### Actual behavior:
after moving from one cell to another cell, Nvda will report the font color as well as background color, even the font color and the backgorund color between the texts of the two cells are the same

### Expected behavior:
NVDA will not anounce such info if the colors are the same

### System configuration
#### NVDA installed/portable/running from source:
installed
#### NVDA version:
nvda 2021.3.3
#### Windows version:
Windows home ed, 21H1

#### Name and version of other software in use when reproducing the issue:
Microsoft Office 365 installed version

#### Other information about your system:

### Other questions
#### Does the issue still occur after restarting your computer?
Yes
#### Have you tried any other versions of NVDA? If so, please report their behaviors.
No 
#### If NVDA add-ons are disabled, is your problem still occurring?
Not tried
#### Does the issue still occur after you run the COM Registration Fixing Tool in NVDA's tools menu?
Not tried
Strange behavior regarding reporting of font related attributes in MS Word
Note:
I just discovered that this is not a specific problem related to reporting of font color as what I have first reported in #13458 

### Steps to reproduce:
1. open document setting of nvda
2. check some of the items that are related to the styles and attributes of fonts, e.g. color, text-alignment, etc.
3. open a word file, probably with a table 
4. in some of the places, edit cursor moving from one place to another, nvda will still report the font style or attribute, as if there is a change in the style or attribute

For example:
1. press ctrl and end jump to end of document,
2. press nvda and f, nvda report that there is no format information
3. press up arrow 
4. nvda will report font style and attributes based on what I have selected in the document setting, e.g. in my situation, it report "black, background color is white, left aligned"

The above problem can also be found when inside a table cell and press end, which move the cursor to the end of that cell,
or when moving from one table cell to another table cell

### Actual behavior:

### Expected behavior:
As what I have checked , end of the document, end of the table cell, nvda will report that the cursor position has no styling information, so nvda should ignore this when checking for style or attribute changes

### System configuration
#### NVDA installed/portable/running from source:
installed

#### NVDA version:
nvda2021.3.3
#### Windows version:
Windows 10 Home 21H1
#### Name and version of other software in use when reproducing the issue:
MS Word 365 installed version
#### Other information about your system:

### Other questions
#### Does the issue still occur after restarting your computer?
Yes
#### Have you tried any other versions of NVDA? If so, please report their behaviors.
Not tried.
#### If NVDA add-ons are disabled, is your problem still occurring?
Not tried.
#### Does the issue still occur after you run the COM Registration Fixing Tool in NVDA's tools menu?
Not tried.


Discussion:
A couple of notes fom my testing of this:

1) This happens in Word with UIA enabled, but not with that setting (in NVDA's advanced settings) disabled.

2) This only happens when you navigate with arrows / control+arrows etc, but not when using the table navigation commands (control+alt+arrows) or tab.

3) It also happens when reading with NVDA+down arrow / NVDA+a, although interestingly not on every cell, but on the second cell in each row I think?

4) Nothing odd in the log.

My system:
- NVDA 2021.3.3
- Windows 10 (64-bit) Version: 21H2 (2009), Build: 19044.1586
- Office 365 (64-bit) Version: 16.0.14931.20118
Another place which this issue will occur.
1. enable reporting of font color in nvda
2. open any word file, either blank or with some text
3. press ctrl and end, 
4. press up arrow, nvda will report the color
Re moving to the end of a document and up one line - I can replicate only when the last line of the document is blank.  IE, if there is text on the last line and no new line afterwards, it does not report the colour if it hasn't changed.
Sorry, I have reframed the issue into a bigger picture and is posted as a new issue #13459
As you have already commented here, so I have not edited this issue, and so I posted a new one.
I am not sure how to handel merging or deleting one of this issue, may you please take necessary action if needed, thanks.

I have done some tests arrowing from a cell containing normal text to an empty cell. It results that the following formatting information is unduly reported:
* Font name
* Font size
* Font attribute
* Font superscript/subscript
* Font style
* Font color
* Paragraph alignment



Resolving patch:
diff --git a/source/NVDAObjects/UIA/__init__.py b/source/NVDAObjects/UIA/__init__.py
--- a/source/NVDAObjects/UIA/__init__.py
+++ b/source/NVDAObjects/UIA/__init__.py
@@ -592,7 +592,7 @@ def _getTextWithFields_text(self,textRange,formatConfig,UIAFormatUnits=None):
 		rangeIter=iterUIARangeByUnit(textRange,unit) if unit is not None else [textRange]
 		for tempRange in rangeIter:
 			text=self._getTextFromUIARange(tempRange) or ""
-			if text:
+			if text is not None:
 				if debug:
 					log.debug("Chunk has text. Fetching formatting")
 				try:
@@ -607,7 +607,9 @@ def _getTextWithFields_text(self,textRange,formatConfig,UIAFormatUnits=None):
 					continue
 				if debug:
 					log.debug("Yielding formatting and text")
-				yield field
+					log.debug(f"field: {field}, text: {text}")
+				if field:
+					yield field
 				yield text
 		if debug:
 			log.debug("Done _getTextWithFields_text")
diff --git a/source/UIAHandler/utils.py b/source/UIAHandler/utils.py
--- a/source/UIAHandler/utils.py
+++ b/source/UIAHandler/utils.py
@@ -121,7 +121,9 @@ def iterUIARangeByUnit(rangeObj,unit,reverse=False):
 	tempRange=rangeObj.clone()
 	tempRange.MoveEndpointByRange(Endpoint_relativeEnd,rangeObj,Endpoint_relativeStart)
 	endRange=tempRange.Clone()
+	loopCount = 0
 	while relativeGTOperator(endRange.Move(unit,minRelativeDistance),0):
+		loopCount += 1
 		tempRange.MoveEndpointByRange(Endpoint_relativeEnd,endRange,Endpoint_relativeStart)
 		pastEnd=relativeGTOperator(tempRange.CompareEndpoints(Endpoint_relativeEnd,rangeObj,Endpoint_relativeEnd),0)
 		if pastEnd:
@@ -139,6 +141,11 @@ def iterUIARangeByUnit(rangeObj,unit,reverse=False):
 	if relativeLTOperator(tempRange.CompareEndpoints(Endpoint_relativeEnd,rangeObj,Endpoint_relativeEnd),0):
 		tempRange.MoveEndpointByRange(Endpoint_relativeEnd,rangeObj,Endpoint_relativeEnd)
 		yield tempRange.clone()
+	elif loopCount == 0:
+		# We Could not walk at all.
+		# So just yield the original range
+		yield rangeObj
+
 
 def getEnclosingElementWithCacheFromUIATextRange(textRange,cacheRequest):
 	"""A thin wrapper around IUIAutomationTextRange3::getEnclosingElementBuildCache if it exists, otherwise IUIAutomationTextRange::getEnclosingElement and then IUIAutomationElement::buildUpdatedCache."""


Test patch: