Repository: nvaccess/nvda

Issue:
Elements list: name filtering fails after searching for an element name that does not exist
Hi,

Considered very critical:

### Steps to reproduce:
1. Visit any website.
2. Open elements list (NVDA+F7).
3. Search for an existing link.
4. Then search for a link that you know does not exist via filter field.
5. Press Home to move to the start of the text, and press Delete key.
6. Search for another link via filter field.

### Actual behavior:
Errors are thrown and the tree view is not populated.

### Expected behavior:
No problems.

### System configuration:

#### NVDA Installed/portable/running from source:
Installed

#### NVDA version:
Alpha.16126

#### Windows version:
Windows 10 Version 1803

#### Name and version of other software in use when reproducing the issue:
N/A

#### Other information about your system:

### Other questions:

#### Does the issue still occur after restarting your PC?
Yes

#### Have you tried any other versions of NVDA?
Yes - 2018.2.1.

### Traceback:
ERROR - unhandled exception (09:57:08.263):
Traceback (most recent call last):
  File "browseMode.pyc", line 1025, in onFilterEditTextChange
  File "browseMode.pyc", line 890, in filter
wxAssertionError: C++ assertion "item.IsOk()" failed at ..\..\src\msw\treectrl.cpp(1032) in wxTreeCtrl::GetItemParam(): invalid tree item

I'm thinking that this may have to do with an invalid character or tree view items being deleted.

Priority shall be set to P1 as this is a possible regression with wxPython 4 (2018.3) and due to massive impact this bug will have (cases were reported in the wild).

Thanks.


Discussion:
Hi,

The process used to cause this error is as follows:

1. Use enters text to filter field.
2. The text change event will call filter method defined in elements list dialog.
3. Initially, the element tree is not empty, so search can take place.
4. As more characters are entered into the filter field, search takes place inside the tree, not the whole document/elements tree.
5. At some point, the entered text will yield no results, hence the search tree is empty.
6. The next character entered will then cause filter method to run. But since the search tree is empty, TreeItem.GetSelection would fail.

One possible workaround (for now) would be to check the item count like so:

1. Set default element to initial element. Since the bug in question does not set this flag, we can ignore this.
2. If not, check item count for the search tree, and select an item if and only if there are items in there.
3. If the empty filter text case is not caught, catch this now (I'm sure it'll catch this case automatically).

Thanks.

Resolving patch:
diff --git a/source/browseMode.py b/source/browseMode.py
--- a/source/browseMode.py
+++ b/source/browseMode.py
@@ -887,7 +887,11 @@ def initElementType(self, elType):
 	def filter(self, filterText, newElementType=False):
 		# If this is a new element type, use the element nearest the cursor.
 		# Otherwise, use the currently selected element.
-		defaultElement = self._initialElement if newElementType else self.tree.GetItemData(self.tree.GetSelection())
+		# #8753: wxPython 4 returns "invalid tree item" when the tree view is empty, so use initial element if appropriate.
+		try:
+			defaultElement = self._initialElement if newElementType else self.tree.GetItemData(self.tree.GetSelection())
+		except:
+			defaultElement = self._initialElement
 		# Clear the tree.
 		self.tree.DeleteChildren(self.treeRoot)
 


Test patch: