Skip to content

Don't apply smarty-pants inside inline HTML tags - #735

Open
LuisFigueroaG wants to merge 2 commits into
trentm:masterfrom
LuisFigueroaG:fix/smarty-pants-html-attributes
Open

LuisFigueroaG wants to merge 2 commits into
trentm:masterfrom
LuisFigueroaG:fix/smarty-pants-html-attributes

Conversation

@LuisFigueroaG

Copy link
Copy Markdown

Fixes #150

With the smarty-pants extra on, the quote and dash substitutions ran over the whole span text, inline HTML tags included. Quotes inside attributes were turned into entities and broke the tag:

markdown2.markdown('<img src="/path/to/img.png" alt="Alt Text">', extras=["smarty-pants"])
<p><img src="/path/to/img.png&#8221; alt="Alt Text"></p>

Inline HTML comments broke too, because -- became an en dash: <!-- note --> came out as <!&#8211; note &#8211;>.

The footnote case from the original report was already handled by a special case in SmartyPants.run that changed class="footnote-ref&#8221; back, but other tags still had the problem.

What I changed: SmartyPants.run now swaps the inline tags and comments for hash placeholders first, using the existing _sorta_html_tokenize_re. Then it does the substitutions and puts the tags back. Since the placeholders aren't whitespace, the text around a tag looks the same to the quote regexes as it did before, so output outside tags doesn't change. With tags skipped, the footnote special case isn't needed any more, so I removed it. footnotes_smarty-pants still passes.

To check that nothing else changed, I ran every .text file in the test dirs through master and through this branch with smarty-pants on (alone and with footnotes, fenced-code-blocks and tables). The only differences were inside tags (src/width/title attributes and <!-- --> comments), plus the randomly encoded mailto links, which are different on every run anyway.

Tests:

  • New case tm-cases/smarty_pants_html_attributes_issue150 covers an <img> with double-quoted attributes, an <a> with a single-quoted title, quotes inside a <span>, and an inline comment containing --. It fails without the fix.
  • make testone: 289 tests, OK (288 on master)
  • pycodestyle lib/markdown2.py: 413 lines vs 415 on master (no new warnings)
  • make testredos: 7 tests, OK

smarty-pants ran over the whole span text, so quotes in tag attributes
and the dashes in HTML comments were turned into entities, breaking the
markup (e.g. <img src="a.png" alt="b"> became src="a.png&#8221; ...).

Swap tags and comments out for placeholders while the substitutions run,
which also makes the footnote-ref special case unnecessary.

Fixes trentm#150
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incorrect HTML when using footnotes and smarty-pants together

1 participant