Пример #1
0
    def test_sanitizer_extensions(self):
        sanitizer = html.Sanitizer(elements=["img"], attributes=["src"])
        examples = [('<img src="cat.gif" />', '<img src="cat.gif">'),
                    ('<script src="doge.js"></script>', '')]

        for (input, expected) in examples:
            self.assertEqual(html.sanitize(sanitizer, input), expected)
Пример #2
0
    def test_sanitizer(self):
        sanitizer = html.Sanitizer(elements=[], attributes=[])
        examples = [
            ('Look: <img src="..." />', 'Look: '),
            ('<a href="http://example.org/">Ha</a>',
             ['<a href="http://example.org/" rel="nofollow noopener">Ha</a>',
              '<a rel="nofollow noopener" href="http://example.org/">Ha</a>']),
            ('<a href="sms:+1234567890">Ha</a>', '<a>Ha</a>'),
            ('<p style="visibility: hidden;">Test</p>', '<p>Test</p>'),
            ('<script>alert("Onoe")</script>', 'alert("Onoe")')]

        for (input, expected) in examples:
            if isinstance(expected, list):
                self.assertIn(html.sanitize(sanitizer, input), expected)
            else:
                self.assertEqual(html.sanitize(sanitizer, input), expected)
Пример #3
0
    def test_sanitizer_extensions(self):
        sanitizer = html.Sanitizer(elements=["img"], attributes=["src"])
        examples = [
            ('<img src="cat.gif" />', '<img src="cat.gif">'),
            ('<script src="doge.js"></script>', '')]

        for (input, expected) in examples:
            self.assertEqual(html.sanitize(sanitizer, input), expected)
Пример #4
0
    def test_sanitizer(self):
        sanitizer = html.Sanitizer(elements=[], attributes=[])
        examples = [
            ('Look: <img src="..." />', 'Look: '),
            ('<a href="http://example.org/">Ha</a>', [
                '<a href="http://example.org/" rel="nofollow noopener">Ha</a>',
                '<a rel="nofollow noopener" href="http://example.org/">Ha</a>'
            ]), ('<a href="sms:+1234567890">Ha</a>', '<a>Ha</a>'),
            ('<p style="visibility: hidden;">Test</p>', '<p>Test</p>'),
            ('<script>alert("Onoe")</script>', 'alert("Onoe")')
        ]

        for (input, expected) in examples:
            if isinstance(expected, list):
                self.assertIn(html.sanitize(sanitizer, input), expected)
            else:
                self.assertEqual(html.sanitize(sanitizer, input), expected)