From 67ab95d579ec0b704b2ab63a69599f5bac9c04f9 Mon Sep 17 00:00:00 2001 From: facelessuser Date: Wed, 30 Sep 2026 07:17:49 -0600 Subject: [PATCH 1/2] Fix ancestor handling Ancestors used to continually add duplicate parents that were already present. Call `__build_ancestors` once at the beginning and then manage ancestors as appropriate. --- markdown/treeprocessors.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/markdown/treeprocessors.py b/markdown/treeprocessors.py index 4d2f318d..a148175f 100644 --- a/markdown/treeprocessors.py +++ b/markdown/treeprocessors.py @@ -371,12 +371,14 @@ def run(self, tree: etree.Element, ancestors: list[str] | None = None) -> etree. self.parent_map = {c: p for p in tree.iter() for c in p} stack = [(tree, tree_parents)] + self.__build_ancestors(tree, tree_parents) while stack: currElement, parents = stack.pop(0) self.ancestors = parents - self.__build_ancestors(currElement, self.ancestors) + if currElement is not tree: + self.ancestors.append(currElement.tag.lower()) insertQueue = [] for child in currElement: From 4ce0baa831d58fc36d3139ca348bc60e97a7a7d4 Mon Sep 17 00:00:00 2001 From: facelessuser Date: Wed, 30 Sep 2026 08:06:15 -0600 Subject: [PATCH 2/2] Update changelog --- docs/changelog.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/changelog.md b/docs/changelog.md index cc31289d..f20687c0 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -15,6 +15,7 @@ See the [Contributing Guide](contributing.md) for details. ## [Unreleased] * Update serializer to be non-recursive (#1644). +* Improve ancestor handling in the inline `Treeprocessor` (#1646). ## [3.11.0] - 2026-09-25