Ejemplo n.º 1
0
def test_sanitize():
    # plain html should pass through
    assert h.sanitize("hello") == "hello"
    assert h.sanitize("<p>hello</p>") == "<p>hello</p>"

    # broken html must be corrected
    assert h.sanitize("<p>hello") == "<p>hello</p>"

    # css class is fine
    assert h.sanitize('<p class="foo">hello</p>') == '<p class="foo">hello</p>'

    # style attribute must be stripped
    assert h.sanitize('<p style="color: red">hello</p>') == '<p>hello</p>'

    # style tags must be stripped
    assert h.sanitize(
        '<style type="text/css">p{color: red;}</style><p>hello</p>'
    ) == '<p>hello</p>'

    # script tags must be stripped
    assert h.sanitize('<script>alert("dhoom")</script>hello') == 'hello'

    # rel="nofollow" must be added absolute links
    assert h.sanitize(
        '<a href="https://example.com">hello</a>'
    ) == '<a href="https://example.com" rel="nofollow">hello</a>'
    # relative links should pass through
    assert h.sanitize(
        '<a href="relpath">hello</a>') == '<a href="relpath">hello</a>'
Ejemplo n.º 2
0
def test_sanitize():
    # plain html should pass through
    assert h.sanitize("hello") == "hello"
    assert h.sanitize("<p>hello</p>") == "<p>hello</p>"
    
    # broken html must be corrected
    assert h.sanitize("<p>hello") == "<p>hello</p>"
    
    # css class is fine
    assert h.sanitize('<p class="foo">hello</p>') == '<p class="foo">hello</p>'

    # style attribute must be stripped
    assert h.sanitize('<p style="color: red">hello</p>') == '<p>hello</p>'
    
    # style tags must be stripped
    assert h.sanitize('<style type="text/css">p{color: red;}</style><p>hello</p>') == '<p>hello</p>'
    
    # script tags must be stripped
    assert h.sanitize('<script>alert("dhoom")</script>hello') == 'hello'
    
    # rel="nofollow" must be added absolute links
    assert h.sanitize('<a href="http://example.com">hello</a>') == '<a href="http://example.com" rel="nofollow">hello</a>'
    # relative links should pass through
    assert h.sanitize('<a href="relpath">hello</a>') == '<a href="relpath">hello</a>'
Ejemplo n.º 3
0
 def convert(self):
     html = markdown.Markdown.convert(self)
     return h.sanitize(html)