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 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: