Skip to content

Fix nofollow and target-blank-links extras breaking <area> and <abbr> tags - #782

Open
bardrop wants to merge 2 commits into
trentm:masterfrom
bardrop-oss:fix-nofollow-area-abbr-tags
Open

bardrop wants to merge 2 commits into
trentm:masterfrom
bardrop-oss:fix-nofollow-area-abbr-tags

Conversation

@bardrop

@bardrop bardrop commented Nov 18, 2026

Copy link
Copy Markdown

The nofollow and target-blank-links extras add rel/target with a regex that matches <a followed by anything up to href=. It has no word boundary on either side, so it also hits other tags and other attributes:

>>> markdown2.markdown('<map name="m">\n<area shape="rect" coords="0,0,1,1" href="http://x">\n</map>\n', extras=["nofollow"])
'<map name="m">\n<a rel="nofollow"rea shape="rect" coords="0,0,1,1" href="http://x">\n</map>\n'

>>> markdown2.markdown('x <abbr title="t" data-href="http://y">A</abbr>', extras=["nofollow"])
'<p>x <a rel="nofollow"bbr title="t" data-href="http://y">A</abbr></p>\n'

The <area> and <abbr> tags are turned into broken <a ...rea / <a ...bbr tags, and <a data-href="..."> (no real href) also gets rel/target.

Fix: require whitespace after the a tag name, and require href= to not be the end of a longer attribute name ((?<![\w-])).

Test: new test/tm-cases/link_nofollow_other_tags (<area>, <abbr data-href>, <a data-href> and a normal link, with both extras). It fails before the change and passes after. make testone and make testredos pass.

… tags

The regex that adds rel/target to links matched any tag starting with
"<a" and any attribute ending in "href=". So <area href=...> became
<a rel="nofollow"rea href=...>, <abbr data-href=...> became a broken
<a ...bbr> tag, and <a data-href=...> without an href got rel/target.

Require whitespace after the tag name and a real href attribute.
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