Skip to content

Commit

Permalink
100% coverage for html.py. Simplify handling of img max-width.
Browse files Browse the repository at this point in the history
  • Loading branch information
jamadden committed Aug 4, 2016
1 parent b059b95 commit 299ec64
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
14 changes: 7 additions & 7 deletions src/nti/contentfragments/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,8 @@ def disallowed_token(self, token):

if self._ignoring_stack:
# element data beneath something we're rejecting
return None
# XXX: JAM: I can't get this condition to happen in tests!
return None # pragma: no cover

# Otherwise, don't escape the tag, simply drop the tag name, but
# preserve the contents.
Expand Down Expand Up @@ -343,12 +344,11 @@ def sanitize_user_html(user_input, method='html'):
# for user-provided images might be preferable.
elif node.tag == 'img':
node.attrib.pop('max-width', None)
style = node.attrib.get('style')
if style and 'max-width' in style:
new_style = [x for x in style.split(';') if 'max-width' not in x]
style = ';'.join(new_style)
style = style or ''
node.attrib['style'] = '%s;%s;' % (style, 'max-width: 100%')
style = node.attrib.get('style') or ''
# max-width is not in our allowed list of styles
assert 'max-width' not in style
new_style = style + (' ' if style else '') + 'max-width: 100%;'
node.attrib['style'] = new_style

if method == 'text':
return _doc_to_plain_text(doc)
Expand Down
34 changes: 33 additions & 1 deletion src/nti/contentfragments/tests/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from hamcrest import is_
from hamcrest import assert_that
from hamcrest import same_instance

from nti.testing.matchers import verifiably_provides

Expand All @@ -18,6 +19,7 @@
from nti.contentfragments import interfaces as frg_interfaces

from nti.contentfragments.tests import ContentfragmentsLayerTest
from nti.contentfragments import html as frag_html

def _check_sanitized(inp, expect, expect_iface=frg_interfaces.IUnicodeContentFragment):
was = frg_interfaces.IUnicodeContentFragment(inp)
Expand All @@ -40,7 +42,7 @@ def test_sanitize_data_uri(self):

_ = _check_sanitized("<audio data-id='ichigo' />",
u'<html><body><audio data-id="ichigo"></audio></body></html>')

def test_normalize_html_text_to_par(self):
html = u'<html><body><p style=" text-align: left;"><span style="font-family: \'Helvetica\'; font-size: 12pt; color: black;">The pad replies to my note.</span></p>The server edits it.</body></html>'
exp = u'<html><body><p style="text-align: left;"><span>The pad replies to my note.</span></p><p style="text-align: left;">The server edits it.</p></body></html>'
Expand Down Expand Up @@ -107,3 +109,33 @@ def test_sanitize_user_html_chat(self):
exp = u'<html><a href=\'http://tag:nextthought.com,2011-10:julie.zhu-OID-0x148a37:55736572735f315f54657374:hjJe3dfZMVb,"body":["5:::{\\"args\\\'>foo</html>'
plain_text = frg_interfaces.IPlainTextContentFragment(exp)
assert_that(plain_text, verifiably_provides(frg_interfaces.IPlainTextContentFragment))

# idempotent
assert_that(frag_html._sanitize_user_html_to_text(plain_text),
is_(same_instance(plain_text)))
assert_that(frag_html._html_to_sanitized_text(plain_text),
is_(same_instance(plain_text)))


def test_sanitize_img(self):
html = '<html><body><img style="color: blue; text-align: left; max-width: 10px" href="foo"></body></html>'
exp = '<html><body><img href="foo" style="color: blue; text-align: left; max-width: 100%;" /></body></html>'
_check_sanitized(html, exp)

html = '<html><body><img style="" href="foo"></body></html>'
exp = '<html><body><img href="foo" style="max-width: 100%;" /></body></html>'
_check_sanitized(html, exp)

html = '<html><body><img max-width="1%" href="foo"></body></html>'
exp = '<html><body><img href="foo" style="max-width: 100%;" /></body></html>'
_check_sanitized(html, exp)

def test_sanitize_empty_span(self):
html = '<html><body><span></span></body></html>'
exp = ''
_check_sanitized(html, exp)

def test_sanitize_remove_inner_elems(self):
html = '<html><body><script><div /><span>Hi</span></script><style><span>hi</span></style></body></html>'
exp = ''
_check_sanitized(html, exp)

0 comments on commit 299ec64

Please sign in to comment.