Home | Trees | Indices | Help |
|
---|
|
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 1214 """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 2511227 element = self.etree.HTML(self.html_str) 28 self.assertEqual(self.etree.tostring(element), 29 self.html_str)3032 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'))3537 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)4244 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)4951 element = self.etree.HTML(self.broken_html_str) 52 self.assertEqual(self.etree.tostring(element), 53 self.html_str)5456 # 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 6466 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)7678 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) 9092 parser = self.etree.HTMLParser() 93 parse = self.etree.parse 94 self.assertRaises(IOError, 95 parse, "__some_hopefully_nonexisting_file__.html", 96 parser)9799 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))114 suite = unittest.TestSuite() 115 suite.addTests([unittest.makeSuite(HtmlParserTestCase)]) 116 return suite117 118 if __name__ == '__main__': 119 unittest.main() 120
Home | Trees | Indices | Help |
|
---|
Generated by Epydoc 3.0 on Mon Feb 11 15:49:35 2008 | http://epydoc.sourceforge.net |