Package lxml :: Package tests :: Module test_htmlparser
[hide private]
[frames] | no frames]

Source Code for Module lxml.tests.test_htmlparser

  1  # -*- coding: utf-8 -*- 
  2   
  3  """ 
  4  HTML parser test cases for etree 
  5  """ 
  6   
  7  import unittest 
  8  import tempfile, os 
  9   
 10  from common_imports import StringIO, etree, fileInTestDir 
 11  from common_imports import SillyFileLike, HelperTestCase 
 12   
13 -class HtmlParserTestCase(HelperTestCase):
14 """HTML parser test cases 15 """ 16 etree = etree 17 18 html_str = "<html><head><title>test</title></head><body><h1>page title</h1></body></html>" 19 broken_html_str = "<html><head><title>test<body><h1>page title</h3></p></html>" 20 uhtml_str = u"<html><head><title>test á\uF8D2</title></head><body><h1>page á\uF8D2 title</h1></body></html>" 21
22 - def tearDown(self):
23 self.etree.setDefaultParser() 24 super(HtmlParserTestCase, self).tearDown()
25
26 - def test_module_HTML(self):
27 element = self.etree.HTML(self.html_str) 28 self.assertEqual(self.etree.tostring(element), 29 self.html_str)
30
31 - def test_module_HTML_unicode(self):
32 element = self.etree.HTML(self.uhtml_str) 33 self.assertEqual(unicode(self.etree.tostring(element, 'UTF8'), 'UTF8'), 34 unicode(self.uhtml_str.encode('UTF8'), 'UTF8'))
35
37 parser = self.etree.HTMLParser(recover=False) 38 parse = self.etree.parse 39 f = StringIO("<html></body>") 40 self.assertRaises(self.etree.XMLSyntaxError, 41 parse, f, parser)
42
44 parser = self.etree.HTMLParser(recover=False) 45 parse = self.etree.parse 46 f = StringIO(self.broken_html_str) 47 self.assertRaises(self.etree.XMLSyntaxError, 48 parse, f, parser)
49
50 - def test_module_HTML_broken(self):
51 element = self.etree.HTML(self.broken_html_str) 52 self.assertEqual(self.etree.tostring(element), 53 self.html_str)
54
55 - def test_module_HTML_cdata(self):
56 # by default, libxml2 generates CDATA nodes for <script> content 57 html = '<html><head><style>foo</style></head></html>' 58 element = self.etree.HTML(html) 59 self.assertEquals(element[0][0].text, "foo")
60
61 - def test_module_HTML_access(self):
62 element = self.etree.HTML(self.html_str) 63 self.assertEqual(element[0][0].tag, 'title')
64
65 - def test_module_parse_html(self):
66 parser = self.etree.HTMLParser() 67 filename = tempfile.mktemp(suffix=".html") 68 open(filename, 'wb').write(self.html_str) 69 try: 70 f = open(filename, 'r') 71 tree = self.etree.parse(f, parser) 72 f.close() 73 self.assertEqual(self.etree.tostring(tree.getroot()), self.html_str) 74 finally: 75 os.remove(filename)
76
78 parser = self.etree.HTMLParser() 79 f = SillyFileLike(self.html_str) 80 tree = self.etree.parse(f, parser) 81 html = self.etree.tostring(tree.getroot(), 'UTF-8') 82 self.assertEqual(html, self.html_str)
83 84 ## def test_module_parse_html_filelike_unicode(self): 85 ## parser = self.etree.HTMLParser() 86 ## f = SillyFileLike(self.uhtml_str) 87 ## tree = self.etree.parse(f, parser) 88 ## html = self.etree.tostring(tree.getroot(), 'UTF-8') 89 ## self.assertEqual(unicode(html, 'UTF-8'), self.uhtml_str) 90
91 - def test_html_file_error(self):
92 parser = self.etree.HTMLParser() 93 parse = self.etree.parse 94 self.assertRaises(IOError, 95 parse, "__some_hopefully_nonexisting_file__.html", 96 parser)
97
99 self.assertRaises(self.etree.XMLSyntaxError, 100 self.etree.parse, StringIO(self.broken_html_str)) 101 102 self.etree.setDefaultParser( self.etree.HTMLParser() ) 103 104 tree = self.etree.parse(StringIO(self.broken_html_str)) 105 self.assertEqual(self.etree.tostring(tree.getroot()), 106 self.html_str) 107 108 self.etree.setDefaultParser() 109 110 self.assertRaises(self.etree.XMLSyntaxError, 111 self.etree.parse, StringIO(self.broken_html_str))
112
113 -def test_suite():
114 suite = unittest.TestSuite() 115 suite.addTests([unittest.makeSuite(HtmlParserTestCase)]) 116 return suite
117 118 if __name__ == '__main__': 119 unittest.main() 120