import unittest
from lxml.tests.common_imports import make_doctest
import lxml.html
from lxml.html.clean import Cleaner, clean_html
class CleanerTest(unittest.TestCase):
def test_allow_tags(self):
html = """
"""
cleaner = Cleaner(remove_unknown_tags=True)
result = cleaner.clean_html(html)
self.assertEqual(
result,
clean_html,
msg="Unknown tags not removed. Got: %s" % result,
)
def test_safe_attrs_included(self):
html = """
Cyan
"""
safe_attrs=set(lxml.html.defs.safe_attrs)
safe_attrs.add('style')
cleaner = Cleaner(
safe_attrs_only=True,
safe_attrs=safe_attrs)
result = cleaner.clean_html(html)
self.assertEqual(html, result)
def test_safe_attrs_excluded(self):
html = """
Cyan
"""
expected = """
Cyan
"""
safe_attrs=set()
cleaner = Cleaner(
safe_attrs_only=True,
safe_attrs=safe_attrs)
result = cleaner.clean_html(html)
self.assertEqual(expected, result)
def test_clean_invalid_root_tag(self):
# only testing that cleaning with invalid root tags works at all
s = lxml.html.fromstring('parent child')
self.assertEqual('parent child', clean_html(s).text_content())
s = lxml.html.fromstring('child')
self.assertEqual('child', clean_html(s).text_content())
def test_clean_with_comments(self):
html = """
Cyan
"""
s = lxml.html.fragment_fromstring(html)
self.assertEqual(
b'
',
cleaner.clean_html(html))
def test_sneaky_noscript_in_style(self):
# This gets parsed as through into the output.
html = '',
lxml.html.tostring(clean_html(s)))
def test_sneaky_js_in_math_style(self):
# This gets parsed as