From 04b84e82a019b12d79519082d79e6636238b31a7 Mon Sep 17 00:00:00 2001 From: Adam Turner <9087854+AA-Turner@users.noreply.github.com> Date: Wed, 1 Jun 2022 00:31:12 +0100 Subject: [PATCH] Fix `findall` usage in KeyboardTransform --- sphinx/builders/html/transforms.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/sphinx/builders/html/transforms.py b/sphinx/builders/html/transforms.py index ea596cb4b..7f96c4e60 100644 --- a/sphinx/builders/html/transforms.py +++ b/sphinx/builders/html/transforms.py @@ -40,7 +40,10 @@ class KeyboardTransform(SphinxPostTransform): def run(self, **kwargs: Any) -> None: matcher = NodeMatcher(nodes.literal, classes=["kbd"]) - for node in self.document.findall(matcher): # type: nodes.literal + # this list must be pre-created as during iteration new nodes + # are added which match the condition in the NodeMatcher. + nodes_to_expand = list(self.document.findall(matcher)) + for node in nodes_to_expand: # type: nodes.literal parts = self.pattern.split(node[-1].astext()) if len(parts) == 1 or self.is_multiwords_key(parts): continue @@ -61,6 +64,7 @@ class KeyboardTransform(SphinxPostTransform): node += nodes.Text(sep) except IndexError: pass + _a = 1 def is_multiwords_key(self, parts: List[str]) -> bool: if len(parts) >= 3 and parts[1].strip() == '':