Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_no_href_links(self):
s = '<a name="anchor">x</a>'
assert linkify(s) == s
def test_ports(data, expected_data):
"""URLs can contain port numbers."""
out = '<a rel="nofollow" href="{0}">{0}</a>{1}'
assert linkify(data) == out.format(*expected_data)
def test_mangle_text():
"""We can muck with the inner text of a link."""
def ft(attrs, new=False):
attrs['_text'] = 'bar'
return attrs
assert (
linkify('http://ex.mp <a href="http://ex.mp/foo">foo</a>', callbacks=[ft]) ==
'<a href="http://ex.mp">bar</a> <a href="http://ex.mp/foo">bar</a>'
)
msg = ""
try:
bits = self.description.split(".. :changelog:", 1)
description = bits[0]
parts = publish_parts(source=smart_str(description), writer_name="html4css1", settings_overrides=docutils_settings)
except SystemMessage:
msg = None
else:
if parts is None or len(s.getvalue()) > 0:
msg = None
else:
cnt = force_unicode(parts["fragment"])
cnt = bleach.clean(cnt, tags=ALLOWED_TAGS, attributes=ALLOWED_ATTRIBUTES)
cnt = bleach.linkify(cnt, skip_pre=True, parse_email=True)
msg = jinja2.Markup(cnt)
sys.stderr = old_stderr
self._description_html = msg
return self._description_html
def convert_markdown(text):
# https://pythonadventures.wordpress.com/tag/markdown/
allowed_tags = [
'a', 'abbr', 'acronym', 'b',
'blockquote', 'code', 'em',
'i', 'li', 'ol', 'pre', 'strong',
'ul', 'h1', 'h2', 'h3', 'p', 'br', 'ins', 'del',
]
unsafe_html = markdown.markdown(
text,
extensions=["markdown.extensions.fenced_code"],
)
html = bleach.linkify(bleach.clean(unsafe_html, tags=allowed_tags))
return Markup(html)
def on_changed_body(target, value, oldvalue, initiator):
allowed_tags = ['a', 'abbr', 'acronym', 'b', 'blockquote', 'code','em', 'i', 'li', 'ol', 'pre', 'strong', 'ul',
'h1', 'h2', 'h3', 'p']
target.body_html = bleach.linkify(bleach.clean(markdown(value, output_format="html"), tags=allowed_tags,
strip=True))
db.event.listen(Post.body, 'set', Post.on_changed_body)
def title_clean(t):
allowed_tags = []
title = bleach.linkify(bleach.clean(
markdown(t, output_format='html'),
tags=allowed_tags, strip=True))
return title
def html_clean(htmlstr):
tags = ['a', 'abbr', 'acronym', 'b', 'blockquote', 'code', 'em', 'i', 'li', 'ol', 'strong', 'ul']
tags.extend(['div','p','hr','br','pre','code','span','h1','h2','h3','h4','h5','del','dl','img','sub','sup','u',
'table','thead','tr','th','td','tbody','dd','caption','blockquote','section'])
attributes = {'*':['class','id'],'a': ['href', 'title','target'],'img':['src','style','width','height']}
return bleach.linkify(bleach.clean(htmlstr,tags=tags,attributes=attributes))
def safe_fcps_emerg_html(text: str, base_url: str) -> str:
def translate_link_attr( # pylint: disable=unused-argument
attrs: Mapping[Union[str, Tuple[Optional[str]]], str], new: bool = False
) -> Optional[Mapping[Union[str, Tuple[Optional[str]]], str]]:
for key in tuple(attrs.keys()):
if isinstance(key, tuple) and "href" in key:
# Translate links that don't specify a protocol/host
# For example, /a/b will be translated to (something like) https://fcps.edu/a/b
attrs[key] = urllib.parse.urljoin(base_url, attrs[key])
return attrs
return bleach.linkify(
bleach.clean(text, strip=True, tags=ALLOWED_TAGS, attributes=ALLOWED_ATTRIBUTES, styles=ALLOWED_STYLES), [translate_link_attr]
)
def html(self, node):
"""The cleaned HTML of the page"""
sanitized_content = render_content(self.content, node=node)
try:
return linkify(
sanitized_content,
[nofollow, ],
)
except TypeError:
logger.warning('Returning unlinkified content.')
return sanitized_content