def test_http_equiv(self): self.assertEquals( [ html_linter.HTTPEquivMessage( line=2, column=7, http_equiv='content-type'), html_linter.HTTPEquivMessage( line=3, column=7, http_equiv='content-language'), html_linter.HTTPEquivMessage( line=4, column=7, http_equiv='pragma'), html_linter.HTTPEquivMessage( line=5, column=7, http_equiv='expires'), html_linter.HTTPEquivMessage( line=6, column=7, http_equiv='set-cookie'), html_linter.HTTPEquivMessage( line=7, column=7, http_equiv='foo') ], html_linter.HTML5Linter('<meta charset="utf-8">\n' + '<meta http-equiv="content-type">\n' + '<meta http-equiv="content-language">\n' + '<meta http-equiv="pragma">\n' + '<meta http-equiv="expires">\n' + '<meta http-equiv="set-cookie">\n' + '<meta http-equiv="foo">').messages) self.assertEquals( [], html_linter.HTML5Linter( '<meta charset="utf-8">\n' + '<meta http-equiv="refresh">\n' + '<meta http-equiv="default-style">\n' + '<meta http-equiv="x-ua-compatible">').messages)
def test_trailing_whitespace(self): self.assertEquals( [html_linter.TrailingWhitespaceMessage( line=1, column=4, whitespace=' ')], html_linter.HTML5Linter('foo \n').messages ) self.assertEquals( [html_linter.TrailingWhitespaceMessage( line=1, column=4, whitespace=' '), html_linter.TrailingWhitespaceMessage( line=2, column=5, whitespace=' ')], html_linter.HTML5Linter('foo \nbarz \n').messages ) self.assertEquals( [html_linter.TrailingWhitespaceMessage( line=1, column=4, whitespace='\t \t'), html_linter.TabMessage(line=1, column=4), html_linter.TabMessage(line=1, column=6)], html_linter.HTML5Linter('foo\t \t\r').messages ) # Only complaint before a newline self.assertEquals( [], html_linter.HTML5Linter('a ').messages )
def test_charset_not_present(self): self.assertEquals( [html_linter.CharsetMessage(line=1, column=1)], html_linter.HTML5Linter('<meta description="foo">').messages) # We add the attribute so the optional tag check is not raised self.assertEquals( [html_linter.CharsetMessage(line=2, column=22)], html_linter.HTML5Linter('\n<head data-lang="en">').messages)
def test_entity_reference_must_have_semicolon(self): self.assertEquals( [], html_linter.HTML5Linter( '<a href="foo?foo=foo&bar=bar&baz=baz">').messages) self.assertEquals( [], html_linter.HTML5Linter('<a href=" á= ">').messages)
def test_tabs(self): self.assertEquals([html_linter.TabMessage(line=1, column=3)], html_linter.HTML5Linter(' \t\t').messages) self.assertEquals([ html_linter.TabMessage(line=1, column=3), html_linter.TabMessage(line=2, column=1) ], html_linter.HTML5Linter(' \ta\n\ta').messages)
def test_a_tag_with_javascript(self): self.assertEquals( [ html_linter.ConcernsSeparationMessage( line=1, column=10, tag='a', attribute='href') ], html_linter.HTML5Linter('<a href="javascript:foo();">').messages) self.assertEquals([], html_linter.HTML5Linter('<a href="foo">').messages)
def test_script_type(self): self.assertEquals( [html_linter.TypeAttributeMessage(line=1, column=9, tag='script')], html_linter.HTML5Linter( '<script type="text/javascript" src="foo.js">').messages) self.assertEquals([], html_linter.HTML5Linter( '<script src="foo.js">\n' + '<script type="foo" src="foo.foo">\n').messages)
def test_link_type(self): self.assertEquals( [html_linter.TypeAttributeMessage(line=1, column=7, tag='link')], html_linter.HTML5Linter( '<link type="text/css" href="foo.css">').messages) self.assertEquals([], html_linter.HTML5Linter( '<link href="foo.css">\n' + '<link type="foo" href="foo.foo">\n').messages)
def test_charset(self): self.assertEquals( [html_linter.CharsetMessage(line=1, column=16, charset='foo')], html_linter.HTML5Linter('<meta charset="foo">').messages) self.assertEquals( [html_linter.CharsetMessage(line=1, column=16, charset='UTF-8')], html_linter.HTML5Linter('<meta charset="UTF-8">').messages) self.assertEquals( [], html_linter.HTML5Linter('<meta charset="utf-8">').messages)
def test_entity_references(self): self.assertEquals([ html_linter.EntityReferenceMessage( line=1, column=2, entity='á') ], html_linter.HTML5Linter(' á ').messages) self.assertEquals( [], html_linter.HTML5Linter(' < > & ').messages)
def test_script_with_content(self): self.assertEquals( [ html_linter.ConcernsSeparationMessage( line=1, column=1, tag='script') ], html_linter.HTML5Linter('<script></script>').messages) self.assertEquals( [], html_linter.HTML5Linter('<script src="foo.js"></script>').messages)
def test_entity_references_in_attributes(self): self.assertEquals( [ html_linter.EntityReferenceMessage( line=1, column=11, entity='á') ], html_linter.HTML5Linter('<a href=" á ">').messages) self.assertEquals([], html_linter.HTML5Linter( '<a href="< > &">').messages)
def test_open_optional_tag(self): self.assertEquals([ html_linter.OptionalTagMessage( line=1, column=1, tag='html', opening=True), html_linter.OptionalTagMessage( line=1, column=10, tag='body', opening=True) ], html_linter.HTML5Linter('<html>foo<body>').messages) self.assertEquals( [], html_linter.HTML5Linter( '<html data-lang="en">foo<body data-lang="en">').messages)
def test_inline_script_with_charset(self): self.assertEquals([ html_linter.ConcernsSeparationMessage( line=1, column=1, tag='script'), html_linter.InvalidAttributeMessage( line=1, column=9, attribute='charset') ], html_linter.HTML5Linter( '<script charset="utf-8"></script>').messages) self.assertEquals( [], html_linter.HTML5Linter( '<script src="foo.js" charset="utf-8"></script>').messages)
def test_urls_have_protocol(self): self.assertEquals( [ html_linter.ProtocolMessage( line=1, column=10, protocol='http:'), html_linter.ProtocolMessage( line=2, column=11, protocol='https:') ], html_linter.HTML5Linter('<a href="http://foo.com">\n' + '<img src="https://foo.com">').messages) self.assertEquals( [], html_linter.HTML5Linter( '<a href="//foo.com">\n<img src="//foo.com">').messages)
def test_boolean_attribute(self): self.assertEquals([ html_linter.BooleanAttributeMessage( line=1, column=21, attribute='checked', value='checked'), html_linter.BooleanAttributeMessage( line=2, column=8, attribute='autoplay', value='') ], html_linter.HTML5Linter( '<input type="radio" checked="checked">\n' + '<video autoplay="">').messages) self.assertEquals( [], html_linter.HTML5Linter( '<input type="radio" checked>\n<video autoplay>').messages)
def test_script_with_obsolete_language(self): self.assertEquals( [html_linter.ConcernsSeparationMessage( line=1, column=1, tag='script'), html_linter.InvalidAttributeMessage( line=1, column=9, attribute='language')], html_linter.HTML5Linter( '<script language="foo"></script>').messages ) self.assertEquals( [html_linter.InvalidAttributeMessage( line=1, column=22, attribute='language')], html_linter.HTML5Linter( '<script src="foo.js" language="utf-8"></script>').messages )
def get_linter(last_data, last_data_position): """Returns a linter instance with the last_data set.""" linter = html_linter.HTML5Linter('') linter._last_data = last_data linter._last_data_position = last_data_position return linter
def test_tag_with_event_handler(self): self.assertEquals( [ html_linter.ConcernsSeparationMessage( line=1, column=7, tag='body', attribute='onload') ], html_linter.HTML5Linter('<body onload="foo();">').messages)
def test_style_type(self): self.assertEquals( [html_linter.ConcernsSeparationMessage( line=1, column=1, tag='style'), html_linter.TypeAttributeMessage(line=1, column=8, tag='style')], html_linter.HTML5Linter('<style type="text/css">').messages )
def test_style_attribute(self): self.assertEquals( [ html_linter.ConcernsSeparationMessage( line=1, column=4, tag='a', attribute='style') ], html_linter.HTML5Linter('<a style="color:red">a</a>').messages)
def test_quotation(self): self.assertEquals( [ html_linter.QuotationMessage(line=1, column=9, quotation="'"), html_linter.QuotationMessage(line=1, column=22, quotation='') ], html_linter.HTML5Linter('<a href=\'foo\' target=_blank>').messages)
def test_close_optional_tags(self): self.assertEquals( [html_linter.OptionalTagMessage(line=1, column=7, tag='p'), html_linter.OptionalTagMessage(line=2, column=3, tag='body'), html_linter.OptionalTagMessage(line=3, column=1, tag='html')], html_linter.HTML5Linter('<p>foo</p>\n </body>\n</html>').messages )
def test_char_references_in_attributes(self): self.assertEquals( [ html_linter.EntityReferenceMessage( line=1, column=11, entity=' ') ], html_linter.HTML5Linter('<a href="   ">').messages)
def test_case_with_numeric_attribute(self): # Tests https://github.com/deezer/html-linter/issues/3, because of # python bug http://bugs.python.org/issue13822. self.assertEquals( [], html_linter.HTML5Linter( '<a href="" 0>foo</a>').messages )
def test_doctype(self): # Non HTML5 doctype self.assertEquals( [ html_linter.DocumentTypeMessage( line=1, column=1, declaration='<!DOCTYPE html PUBLIC>') ], html_linter.HTML5Linter('<!DOCTYPE html PUBLIC>').messages) # Extra whitespace self.assertEquals([ html_linter.DocumentTypeMessage( line=1, column=1, declaration='<!DOCTYPE html>') ], html_linter.HTML5Linter('<!DOCTYPE html>').messages) # The right doctype self.assertEquals([], html_linter.HTML5Linter('<!DOCTYPE html>').messages)
def test_formatting(self): self.assertEquals( [html_linter.FormattingMessage(line=1, column=5, tag='li'), html_linter.FormattingMessage(line=1, column=9, tag='div'), html_linter.FormattingMessage(line=1, column=14, tag='table'), html_linter.FormattingMessage(line=1, column=21, tag='tr'), html_linter.FormattingMessage(line=1, column=25, tag='td')], html_linter.HTML5Linter('<ul><li><div><table><tr><td>').messages )
def test_tag_with_event_handler_and_js_protocol(self): self.assertEquals([ html_linter.ConcernsSeparationMessage( line=1, column=7, tag='body', attribute='onload'), html_linter.InvalidHandlerMessage( line=1, column=15, attribute='onload') ], html_linter.HTML5Linter( '<body onload="javascript:foo();">').messages)
def test_names(self): self.assertEquals( [ html_linter.NameMessage( line=1, column=10, attribute='id', value='a_b'), html_linter.NameMessage( line=2, column=13, attribute='class', value='Foo'), html_linter.NameMessage( line=3, column=13, attribute='class', value='a_b'), html_linter.NameMessage( line=3, column=22, attribute='id', value='Foo') ], html_linter.HTML5Linter('<div id="a_b">\n' + '<img class="Foo">\n' + '<div class="a_b" id="Foo">').messages) self.assertEquals([], html_linter.HTML5Linter( '<div id="a-b">\n<img class="foo">').messages)
def test_a_tag_with_void_zero(self): self.assertEquals( [ html_linter.VoidZeroMessage(line=1, column=10), html_linter.VoidZeroMessage(line=2, column=10), html_linter.VoidZeroMessage(line=3, column=10) ], html_linter.HTML5Linter('<a href="javascript:void(0);">\n' + '<a href="javascript: void(0);">\n' + '<a href="javascript: void(0)">').messages)