Skip to content

Commit ea1a5a3

Browse files
committed
faster walk
1 parent c75b169 commit ea1a5a3

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

src/textual/walk.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,23 +56,31 @@ def walk_depth_first(
5656
Returns:
5757
An iterable of DOMNodes, or the type specified in ``filter_type``.
5858
"""
59-
from textual.dom import DOMNode
60-
6159
stack: list[Iterator[DOMNode]] = [iter(root.children)]
6260
pop = stack.pop
6361
push = stack.append
64-
check_type = filter_type or DOMNode
6562

66-
if with_root and isinstance(root, check_type):
67-
yield root
68-
while stack:
69-
if (node := next(stack[-1], None)) is None:
70-
pop()
71-
else:
72-
if isinstance(node, check_type):
63+
if filter_type is None:
64+
if with_root:
65+
yield root
66+
while stack:
67+
if (node := next(stack[-1], None)) is None:
68+
pop()
69+
else:
7370
yield node
74-
if children := node._nodes:
75-
push(iter(children))
71+
if children := node._nodes:
72+
push(iter(children))
73+
else:
74+
if with_root and isinstance(root, filter_type):
75+
yield root
76+
while stack:
77+
if (node := next(stack[-1], None)) is None:
78+
pop()
79+
else:
80+
if isinstance(node, filter_type):
81+
yield node
82+
if children := node._nodes:
83+
push(iter(children))
7684

7785

7886
if TYPE_CHECKING:

0 commit comments

Comments
 (0)