From 1c6f6d873a8990da3b9c89ca0d08f374a399a54a Mon Sep 17 00:00:00 2001 From: Nikhil Kumar Date: Sun, 25 Oct 2026 03:00:33 +0530 Subject: [PATCH] Ignore empty abbreviation definitions in element tails Apply the same nonempty-definition check used for element text to tails. Cover empty and nonempty glossary entries around inline elements and record the behavior in the unreleased changelog. Assisted-by: OpenAI GPT-6 --- docs/changelog.md | 2 ++ markdown/extensions/abbr.py | 7 ++++--- tests/test_syntax/extensions/test_abbr.py | 8 ++++++++ 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/docs/changelog.md b/docs/changelog.md index 5dc7b0f8..5b8c4e61 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -16,6 +16,8 @@ See the [Contributing Guide](contributing.md) for details. ### Fixed +* Ignore glossary abbreviations with empty definitions after inline elements, + consistently with abbreviations before inline elements. * Strip complete HTML tags with quoted `>` characters from `toc` labels and generated heading IDs. * Fix quadratic-time regex backtracking in `attr_list` when a block ends with an diff --git a/markdown/extensions/abbr.py b/markdown/extensions/abbr.py index 9f609810..659d310f 100644 --- a/markdown/extensions/abbr.py +++ b/markdown/extensions/abbr.py @@ -119,9 +119,10 @@ def iter_element(self, el: etree.Element, parent: etree.Element | None = None) - index = list(parent).index(el) + 1 if not isinstance(tail, AtomicString): for m in reversed(list(self.RE.finditer(tail))): - abbr = self.create_element(self.abbrs[m.group(0)], m.group(0), tail[m.end():]) - parent.insert(index, abbr) - tail = tail[:m.start()] + if self.abbrs[m.group(0)]: + abbr = self.create_element(self.abbrs[m.group(0)], m.group(0), tail[m.end():]) + parent.insert(index, abbr) + tail = tail[:m.start()] el.tail = tail def run(self, root: etree.Element) -> etree.Element | None: diff --git a/tests/test_syntax/extensions/test_abbr.py b/tests/test_syntax/extensions/test_abbr.py index 6654d919..f748c9f8 100644 --- a/tests/test_syntax/extensions/test_abbr.py +++ b/tests/test_syntax/extensions/test_abbr.py @@ -178,6 +178,14 @@ def test_abbr_glossary(self): extensions=[AbbrExtension(glossary=glossary)] ) + def test_abbr_glossary_empty_definition(self): + self.assertMarkdownRenders( + 'HTML W3C HTML *word* HTML W3C HTML', + '

HTML W3C HTML ' + 'word HTML W3C HTML

', + extensions=[AbbrExtension(glossary={'HTML': '', 'W3C': 'World Wide Web Consortium'})] + ) + def test_abbr_glossary_2(self): glossary = {