Skip to content

Abbr should respect AtomicStrings #1513

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .pyspelling.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ matrix:
- alt
ignores:
- 'code, pre'
- '.autorefs-internal[title]'
captures:
- '[role=main] *|*:not(script,style)'
- pyspelling.filters.context:
Expand Down
2 changes: 2 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* `md_in_html` handle tags within inline code blocks better (#1075).
* `md_in_html` fix handling of one-liner block HTML handling (#1074).
* Ensure `<center>` is treated like a block-level element (#1481).
* Ensure that `abbr` extension respects `AtomicString` and does not process
perceived abbreviations in these strings (#1512).

## [3.7] -- 2024-08-16

Expand Down
24 changes: 13 additions & 11 deletions markdown/extensions/abbr.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,20 +98,22 @@ def iter_element(self, el: etree.Element, parent: etree.Element | None = None) -
for child in reversed(el):
self.iter_element(child, el)
if text := el.text:
for m in reversed(list(self.RE.finditer(text))):
if self.abbrs[m.group(0)]:
abbr = self.create_element(self.abbrs[m.group(0)], m.group(0), text[m.end():])
el.insert(0, abbr)
text = text[:m.start()]
el.text = text
if not isinstance(text, AtomicString):
for m in reversed(list(self.RE.finditer(text))):
if self.abbrs[m.group(0)]:
abbr = self.create_element(self.abbrs[m.group(0)], m.group(0), text[m.end():])
el.insert(0, abbr)
text = text[:m.start()]
el.text = text
if parent is not None and el.tail:
tail = el.tail
index = list(parent).index(el) + 1
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()]
el.tail = tail
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()]
el.tail = tail

def run(self, root: etree.Element) -> etree.Element | None:
''' Step through tree to find known abbreviations. '''
Expand Down
12 changes: 12 additions & 0 deletions tests/test_syntax/extensions/test_abbr.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ class TestAbbr(TestCase):

default_kwargs = {'extensions': ['abbr']}

def test_ignore_atomic(self):
self.assertMarkdownRenders(
self.dedent(
"""
This <https://example.com/{YAFR}>

*[YAFR]: Yet Another Feature Request
"""
),
'<p>This <a href="https://example.com/{YAFR}">https://example.com/{YAFR}</a></p>'
)

def test_abbr_upper(self):
self.assertMarkdownRenders(
self.dedent(
Expand Down
Loading