Skip to content

Don't apply strike inside inline HTML tags - #786

Open
alphaNumericEntity wants to merge 2 commits into
trentm:masterfrom
alphaNumericEntity:fix-strike-in-html-tags
Open

alphaNumericEntity wants to merge 2 commits into
trentm:masterfrom
alphaNumericEntity:fix-strike-in-html-tags

Conversation

@alphaNumericEntity

@alphaNumericEntity alphaNumericEntity commented Nov 23, 2026 •

Copy link
Copy Markdown

With the strike extra on, the ~~ substitution ran over the whole span text, inline HTML tags included. ~~ inside an attribute value was turned into <s> tags and broke the tag:

markdown2.markdown('<img src="/img/a~~b~~c.png" alt="~~Alt~~">', extras=["strike"])
<p><img src="/img/a<s>b</s>c.png" alt="<s>Alt</s>"></p>

Expected: <p><img src="/img/a~~b~~c.png" alt="~~Alt~~"></p>

The same happened to Markdown link and image titles ([a](https://example.com/ "~~T~~") gave title="<s>T</s>") and to inline HTML comments (<!-- ~~x~~ --> gave <!-- <s>x</s> -->). A ~~ in an attribute could also pair with a ~~ in the text after the tag, so the <s> started inside the attribute: <img src="x.png" alt="a ~~">~~b~~ gave <img src="x.png" alt="a <s>"></s>b~~.

Emphasis doesn't have this problem because _escape_special_chars hides * and _ inside tags, but ~ isn't in the escape table.

Fix: Strike.run now swaps inline tags and comments for hash placeholders using the existing _sorta_html_tokenize_re, runs the substitution, then puts the tags back in a single re.sub pass, so a paragraph with many tags stays linear. This is the same approach the smarty-pants extra uses since #735. The placeholders aren't whitespace, so ~~ next to a tag (~~<b>x</b>~~) and strikes that span a tag (~~a <b>c</b> d~~) work as before.

Not changed: the text of an auto-link like <http://x.com/~~a~~> is still struck. By the time the extra runs it is an ordinary <a> element, and its text can't be told apart from link text such as [~~a~~](url), which should be struck.

Tests:

  • New case tm-cases/strike_html_attributes covers a raw <img> with ~~ in src and alt, a <span title>, Markdown link and image titles, an inline comment, and a strike around a tag. It fails without the fix.
  • make testone: 311 tests OK with .[all] installed (310 on master).
  • make testredos: 12 tests OK.
  • I compared master and this branch with strike on (alone, and with footnotes, tables + fenced-code-blocks, smarty-pants, markdown-in-html + header-ids + toc + task_list) and each safe_mode, on the test files plus inputs with strikes around and next to tags, code spans, escaped \~~, and links. Outputs only changed inside tags and comments.
  • A paragraph of 40,000 distinct tags each followed by a strike converts in about the same time as on master (0.4s).
  • pycodestyle lib/markdown2.py: same warning count as master.

The CHANGES.md entry is [pull #786].

The strike extra ran over the whole span text, so ~~ in tag attributes,
link and image titles and inline HTML comments was turned into <s> tags,
breaking the markup (e.g. <img src="a~~b~~c.png"> became
src="a<s>b</s>c.png").

Swap tags and comments out for placeholders while the substitution runs,
as the smarty-pants extra does.
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.

1 participant