Skip to content

Commit

Permalink
Fix HTML cleaning code in --pypi-strict mode (#33)
Browse files Browse the repository at this point in the history
Add a test for it because untested code is broken code.

NB:

  <p><a href="http://www.example.com" rel="nofollow">This is fine</a>.</p>

is what you get when you filter the original

  <p><a class="external reference" href="http://www.example.com">This is fine</a>.</p>

through readme.rst.clean().
  • Loading branch information
mgedmin committed Jul 16, 2015
1 parent 4ea97eb commit 8ffb1df
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/restview/restviewhttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ def rest_to_html(self, rest_input, settings=None, mtime=None, filename=None):
html = self.render_exception(e.__class__.__name__, str(e), rest_input, mtime=mtime)
else:
if self.pypi_strict:
writer.fragment = readme.rst.clean(''.join(writer.fragment))
writer.body = [readme.rst.clean(''.join(writer.body))]
writer.output = writer.apply_template()
html = writer.output
return self.inject_ajax(html, mtime=mtime)
Expand Down
38 changes: 38 additions & 0 deletions src/restview/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,44 @@ def doctest_RestViewer_rest_to_html_pypi_strict_and_error_handling():
"""


def doctest_RestViewer_rest_to_html_pypi_strict():
"""Test for RestViewer.rest_to_html in --pypi-strict mode
>>> stderr_patcher = patch('sys.stderr', StringIO())
>>> viewer = RestViewer('.')
>>> viewer.stylesheets = None
>>> viewer.pypi_strict = True
>>> print(viewer.rest_to_html(b'''
... Hello
... -----
...
... `This is fine <http://www.example.com>`__.
...
... ''').strip().replace("&quot;", '"'))
... # doctest: +ELLIPSIS,+REPORT_NDIFF
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
...
<title>Hello</title>
<style type="text/css">
...
</head>
<body>
<div class="document" id="hello">
<h1 class="title">Hello</h1>
<BLANKLINE>
<p><a href="http://www.example.com" rel="nofollow">This is fine</a>.</p>
</div>
</body>
</html>
"""


def doctest_RestViewer_inject_ajax():
"""Test for RestViewer.inject_ajax
Expand Down

0 comments on commit 8ffb1df

Please sign in to comment.