Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Commit 3d2c99c

Browse files
authored
Fix cubic processing time in the legacy_attrs extension
`LegacyAttrs.handleAttributes` ran `ATTR_RE.sub` over each text. At a `{@` with no closing `}`, the first group runs to the end of the text, backtracks to every `=`, and the second group runs to the end again from each one, so text made of n unclosed `{@A=` costs O(n^3): 8 KB takes about 25 seconds to convert. Neither group can contain `}`, so a definition that starts at `{@` ends at the next `}`, and if the first `{@` before a `}` does not match, no later one before that `}` can. Split the text on `}` and try `ATTR_RE.match` once per piece, at its first `{@`. The text returned and the attributes set are unchanged.
1 parent 6c359af commit 3d2c99c

4 files changed

Lines changed: 26 additions & 3 deletions

File tree

‎.spell-dict‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ refactored
109109
refactors
110110
registerExtension
111111
rescan
112+
rescanned
113+
rescanning
112114
resettable
113115
richeland
114116
RSS

‎docs/changelog.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ See the [Contributing Guide](contributing.md) for details.
1616

1717
### Fixed
1818

19+
* Fix cubic processing time in the `legacy_attrs` extension for text with many
20+
`{@` that are not closed by a `}` (#1721).
1921
* Footnote definitions are no longer recognized within a footnote definition,
2022
matching PHP Markdown Extra: an indented definition nested in another
2123
footnote's content now remains as text in the parent footnote instead of

‎markdown/extensions/legacy_attrs.py‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,19 @@ def run(self, doc: etree.Element) -> None:
5757

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

6474

6575
class LegacyAttrExtension(Extension):

‎tests/test_syntax/extensions/test_legacy_attrs.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,12 @@ def testLegacyAttrsSanitizesName(self):
7373
'<p x_svg_onload_alert_1_="y">text more</p>',
7474
extensions=['legacy_attrs']
7575
)
76+
77+
def testLegacyAttrsManyUnclosed(self):
78+
# Each `{@` with no closing `}` used to be rescanned to the end of the text for every `=`.
79+
text = '{@a=' * 2000
80+
self.assertMarkdownRenders(
81+
text,
82+
f'<p>{text}</p>',
83+
extensions=['legacy_attrs']
84+
)

0 commit comments

Comments
 (0)