Skip to content

Commit

Permalink
Merge ce238bb into e47bb6b
Browse files Browse the repository at this point in the history
  • Loading branch information
cutz committed Oct 5, 2021
2 parents e47bb6b + ce238bb commit cf72994
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/nti/contentfragments/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,12 @@ def __iter__(self):
yield token

def _find_links_in_text(self, token):
if self.link_finder is None:
yield token
return

text = token['data']
text_and_links = self.link_finder.find_links(text) if self.link_finder else ()
text_and_links = self.link_finder.find_links(text)
if len(text_and_links) != 1 or text_and_links[0] != text:

def _unicode(x):
Expand Down
27 changes: 25 additions & 2 deletions src/nti/contentfragments/tests/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from zope import interface

from nti.contentfragments.interfaces import IAllowedAttributeProvider
from nti.contentfragments.interfaces import IHyperlinkFormatter

from nti.testing.matchers import verifiably_provides

Expand All @@ -38,7 +39,7 @@ def _check_sanitized(inp, expect, expect_iface=frg_interfaces.IUnicodeContentFra
assert_that(was, verifiably_provides(expect_iface))
return was

class TestHTTML(ContentfragmentsLayerTest):
class TestHTML(ContentfragmentsLayerTest):

def test_sanitize_html(self):
with open(os.path.join(os.path.dirname(__file__), 'contenttypes-notes-tosanitize.plist'), 'rb') as f:
Expand Down Expand Up @@ -103,7 +104,7 @@ def test_rejected_tags(self):
exp = u'<html><body><p style="text-align: left;">The text</p></body></html>'
_check_sanitized(html, exp, frg_interfaces.ISanitizedHTMLContentFragment)

html = u'foo<div><br></div><div>http://google.com</div><div><br></div><div>bar</div><div><br></div><div>http://yahoo.com</div>'''
html = u'foo<div><br></div><div>http://google.com</div><div><br></div><div>bar</div><div><br></div><div>http://yahoo.com</div>'
exp = u'<html><body>foo<br /><a href="http://google.com">http://google.com</a><br />bar<br /><a href="http://yahoo.com">http://yahoo.com</a></body></html>'
_check_sanitized(html, exp, frg_interfaces.ISanitizedHTMLContentFragment)

Expand Down Expand Up @@ -193,6 +194,12 @@ def test_link_creation(self):
'<a href="http://www.google.com">www.google.com</a></p></body></html>'
_check_sanitized(html, exp)

def test_no_link_formatter(self):
with _link_formatter(None):
html = '<p>look at this</p>'
exp = '<html><body><p>look at this</p></body></html>'
_check_sanitized(html, exp)

def test_nested_anchors(self):
# Links should not be created for the url-like text and nesting
# will be split
Expand All @@ -210,6 +217,22 @@ def test_disallowed_within_anchor(self):
html = '<a href="www.nextthought.com"><div>test</div></a>'
_check_sanitized(html, u'<html><body><a href="www.nextthought.com">test</a></body></html>')

@contextlib.contextmanager
def _link_formatter(util):
gsm = component.getGlobalSiteManager()
current = gsm.getUtility(IHyperlinkFormatter)
if current is not None:
gsm.unregisterUtility(current, IHyperlinkFormatter)

if util is not None:
gsm.registerUtility(util, IHyperlinkFormatter)
try:
yield
finally:
if util is not None:
gsm.unregisterUtility(util, IHyperlinkFormatter)
if current is not None:
gsm.registerUtility(current, IHyperlinkFormatter)

@contextlib.contextmanager
def _provide_utility(util):
Expand Down

0 comments on commit cf72994

Please sign in to comment.