Skip to content
Open
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
2 changes: 2 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ See the [Contributing Guide](contributing.md) for details.

### Fixed

* Fix cubic processing time in the `legacy_attrs` extension for text with many
`{@` that are not closed by a `}`.
* The `toc` extension no longer raises `TypeError` for a heading whose
`data-toc-label` attribute is present but valueless, as the `attr_list`
extension's bare-attribute syntax (`{: data-toc-label }`) writes it: like a
Expand Down
16 changes: 13 additions & 3 deletions markdown/extensions/legacy_attrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,19 @@ def run(self, doc: etree.Element) -> None:

def handleAttributes(self, el: etree.Element, txt: str) -> str:
""" Set attributes and return text without definitions. """
def attributeCallback(match: re.Match[str]):
el.set(sanitize_name(match.group(1)), match.group(2).replace('\n', ' '))
return ATTR_RE.sub(attributeCallback, txt)
# Neither part of a definition can contain `}`, so one that starts at `{@` ends at the next
# `}`, and if the first `{@` before a `}` does not match, no later one before it can. Matching
# only there avoids `ATTR_RE.sub` rescanning to the end of the text from every `{@`.
parts = txt.split('}')
for i in range(len(parts) - 1):
part = parts[i] + '}'
start = part.find('{@')
m = ATTR_RE.match(part, start) if start != -1 else None
if m:
el.set(sanitize_name(m.group(1)), m.group(2).replace('\n', ' '))
part = part[:start]
parts[i] = part
return ''.join(parts)


class LegacyAttrExtension(Extension):
Expand Down
9 changes: 9 additions & 0 deletions tests/test_syntax/extensions/test_legacy_attrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,12 @@ def testLegacyAttrsSanitizesName(self):
'<p x_svg_onload_alert_1_="y">text more</p>',
extensions=['legacy_attrs']
)

def testLegacyAttrsManyUnclosed(self):
# Each `{@` with no closing `}` used to be rescanned to the end of the text for every `=`.
text = '{@a=' * 2000
self.assertMarkdownRenders(
text,
f'<p>{text}</p>',
extensions=['legacy_attrs']
)
Loading