Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not remove anchors in safe_mode #284

Merged
merged 2 commits into from Jan 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/markdown2.py
Expand Up @@ -1406,7 +1406,8 @@ def _do_links(self, text):
curr_pos = start_idx + len(result)
text = text[:start_idx] + result + text[url_end_idx:]
elif start_idx >= anchor_allowed_pos:
if self.safe_mode and not self._safe_protocols.match(url):
safe_link = self._safe_protocols.match(url) or url.startswith('#')
if self.safe_mode and not safe_link:
result_head = '<a href="#"%s>' % (title_str)
else:
result_head = '<a href="%s"%s>' % (_html_escape_url(url, safe_mode=self.safe_mode), title_str)
Expand Down
2 changes: 1 addition & 1 deletion test/tm-cases/fenced_code_blocks_leading_lang_space.tags
@@ -1 +1 @@
extra fenced-code-blocks
extra fenced-code-blocks pygments
11 changes: 11 additions & 0 deletions test/tm-cases/link_safe_urls.html
@@ -0,0 +1,11 @@
<p><a href="https://www.example.com">Safe link 1</a></p>

<p><a href="http://www.example.com">Safe link 2</a></p>

<p><a href="ftp://www.example.com">Safe link 3</a></p>

<p><a href="#anchor">Safe link 4</a></p>

<p><a href="#">Unsafe link 1</a></p>

<p><a href="#">Unsafe link 2</a></p>
1 change: 1 addition & 0 deletions test/tm-cases/link_safe_urls.opts
@@ -0,0 +1 @@
{"safe_mode": "escape"}
1 change: 1 addition & 0 deletions test/tm-cases/link_safe_urls.tags
@@ -0,0 +1 @@
safe_mode
11 changes: 11 additions & 0 deletions test/tm-cases/link_safe_urls.text
@@ -0,0 +1,11 @@
[Safe link 1](https://www.example.com)

[Safe link 2](http://www.example.com)

[Safe link 3](ftp://www.example.com)

[Safe link 4](#anchor)

[Unsafe link 1](unknown://www.example.com)

[Unsafe link 2](example)