diff --git a/CHANGES.md b/CHANGES.md index 8911b5cb..adcae02a 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -17,6 +17,7 @@ - [pull #720] Add `wiki-links` extra for `[[Page Name]]` style links (#221) - [pull #722] Harden URL safety checks and sanitization in safe mode (#721) - [pull #728] Fix XSS from zero-padded colon entities in link URLs (#726) +- [pull #735] Fix `smarty-pants` extra converting quotes inside inline HTML tags and comments (#150) ## python-markdown2 2.5.5 diff --git a/lib/markdown2.py b/lib/markdown2.py index b3c29847..db497f5b 100755 --- a/lib/markdown2.py +++ b/lib/markdown2.py @@ -4167,6 +4167,18 @@ def run(self, text: str): for a discussion of some diversion from the original SmartyPants. """ + # Swap out inline HTML tags and comments so their attribute quotes + # and `--` are left alone (#150). The placeholders are non-space, so + # the quote rules see the same context around them as before. + tags = {} + + def hash_tag(match: re.Match[str]) -> str: + key = _hash_text(match.group(0)) + tags[key] = match.group(0) + return key + + text = self.md._sorta_html_tokenize_re.sub(hash_tag, text) + if "'" in text: # guard for perf text = self.contractions(text) text = self._opening_single_quote_re.sub("‘", text) @@ -4182,11 +4194,8 @@ def run(self, text: str): text = text.replace(" . . . ", "…") text = text.replace(". . .", "…") - # TODO: Temporary hack to fix https://github.com/trentm/python-markdown2/issues/150 - if "footnotes" in self.md.extras and "footnote-ref" in text: - # Quotes in the footnote back ref get converted to "smart" quotes - # Change them back here to ensure they work. - text = text.replace('class="footnote-ref”', 'class="footnote-ref"') + for key, tag in tags.items(): + text = text.replace(key, tag) return text diff --git a/test/tm-cases/smarty_pants_html_attributes_issue150.html b/test/tm-cases/smarty_pants_html_attributes_issue150.html new file mode 100644 index 00000000..cbc7a06d --- /dev/null +++ b/test/tm-cases/smarty_pants_html_attributes_issue150.html @@ -0,0 +1,5 @@ +

An inline image Alt Text keeps its attributes.

+ +

A raw link and a “quoted” word.

+ +

An inline is left alone, but “quotes” and dashes – outside it still change.

diff --git a/test/tm-cases/smarty_pants_html_attributes_issue150.opts b/test/tm-cases/smarty_pants_html_attributes_issue150.opts new file mode 100644 index 00000000..d0390c68 --- /dev/null +++ b/test/tm-cases/smarty_pants_html_attributes_issue150.opts @@ -0,0 +1 @@ +{"extras": ["smarty-pants"]} diff --git a/test/tm-cases/smarty_pants_html_attributes_issue150.text b/test/tm-cases/smarty_pants_html_attributes_issue150.text new file mode 100644 index 00000000..af3417f4 --- /dev/null +++ b/test/tm-cases/smarty_pants_html_attributes_issue150.text @@ -0,0 +1,5 @@ +An inline image Alt Text keeps its attributes. + +A raw link and a "quoted" word. + +An inline is left alone, but "quotes" and dashes -- outside it still change.