def testMatchingAttr(self): html = HTMLReader().readString(self._html) self.assertEqual( html.tagWithMatchingAttr('lang', 'en').name(), 'body') self.assertEqual( html.tagWithMatchingAttr('id', 'dataTable').name(), 'table') self.assertEqual(html.tagWithId('dataTable').name(), 'table')
def checkMatchingAttr(self): html = HTMLReader().readString(self._html) self.assertEqual( html.tagWithMatchingAttr('lang', 'en').name(), 'body') self.assertEqual( html.tagWithMatchingAttr('id', 'dataTable').name(), 'table') self.assertEqual(html.tagWithId('dataTable').name(), 'table')
def checkBasics(self): reader = HTMLReader() tag = reader.readString('<html> </html>') self.assertEqual(tag.name(), 'html') self.assertEqual(reader.rootTag(), tag) self.assertTrue(reader.filename() is None) out = StringIO() tag.pprint(out) self.assertEqual(out.getvalue(), '<html>\n</html>\n')
def checkBasics(self): reader = HTMLReader() tag = reader.readString('<html> </html>') assert tag.name() == 'html' assert reader.rootTag() == tag assert reader.filename() == None out = StringIO() tag.pprint(out) assert out.getvalue() == '<html>\n</html>\n'
def checkReuseReader(self): reader = HTMLReader() reader.readString('<html> </html>') tag = reader.readString('<html> <body> </body> </html>') self.assertFalse(reader.rootTag() is None) self.assertEqual(reader.rootTag(), tag) tag = reader.readString('<html> </html>', retainRootTag=0) self.assertFalse(tag is None) self.assertTrue(reader.rootTag() is None)
def checkReuseReader(self): reader = HTMLReader() reader.readString('<html> </html>') tag = reader.readString('<html> <body> </body> </html>') assert reader.rootTag() is not None assert reader.rootTag() == tag tag = reader.readString('<html> </html>', retainRootTag=0) assert tag is not None assert reader.rootTag() is None
def checkAccess(self): html = HTMLReader().readString(self._html) # Name self.assertEqual(html.name(), 'html') # Attrs self.assertEqual(html.numAttrs(), 0) self.assertFalse(html.hasAttr('foo')) self.assertRaises(KeyError, html.attr, 'foo') self.assertTrue(html.attr('foo', None) is None) # Children and subtags, when both are the same. for numFoos, fooAt, foos in [[ html.numChildren, html.childAt, html.children ], [html.numSubtags, html.subtagAt, html.subtags]]: self.assertEqual(numFoos(), 2) self.assertEqual(len(foos()), 2) self.assertEqual(fooAt(0).name(), 'head') self.assertEqual(fooAt(1).name(), 'body') # Children and subtags when they're different body = html.subtagAt(1) p = body.subtagAt(0) self.assertEqual(p.name(), 'p') self.assertEqual(p.numChildren(), 3) self.assertEqual(p.numSubtags(), 1)
def checkAccess(self): html = HTMLReader().readString(self._html) # Name assert html.name() == 'html' # Attrs assert html.numAttrs() == 0 assert not html.hasAttr('foo') self.assertRaises(KeyError, html.attr, 'foo') assert html.attr('foo', None) is None # Children and subtags, when both are the same. for numFoos, fooAt, foos in [[ html.numChildren, html.childAt, html.children ], [html.numSubtags, html.subtagAt, html.subtags]]: assert numFoos() == 2 assert len(foos()) == 2 assert fooAt(0).name() == 'head' assert fooAt(1).name() == 'body' # Children and subtags when they're different body = html.subtagAt(1) p = body.subtagAt(0) assert p.name() == 'p' assert p.numChildren() == 3 assert p.numSubtags() == 1
def checkInvalidHTML(self): from WebUtils.HTMLTag import HTMLTagUnbalancedError, HTMLTagIncompleteError reader = HTMLReader() html = '<html> <body> <table> </body> </html>' self.assertRaises(HTMLTagUnbalancedError, reader.readString, html) html = '<html> <body>' self.assertRaises(HTMLTagIncompleteError, reader.readString, html)
def checkAccess(self): html = HTMLReader().readString(self._html) # Name self.assertEqual(html.name(), 'html') # Attrs self.assertEqual(html.numAttrs(), 0) self.assertFalse(html.hasAttr('foo')) self.assertRaises(KeyError, html.attr, 'foo') self.assertTrue(html.attr('foo', None) is None) # Children and subtags, when both are the same. for numFoos, fooAt, foos in [ [html.numChildren, html.childAt, html.children], [html.numSubtags, html.subtagAt, html.subtags]]: self.assertEqual(numFoos(), 2) self.assertEqual(len(foos()), 2) self.assertEqual(fooAt(0).name(), 'head') self.assertEqual(fooAt(1).name(), 'body') # Children and subtags when they're different body = html.subtagAt(1) p = body.subtagAt(0) self.assertEqual(p.name(), 'p') self.assertEqual(p.numChildren(), 3) self.assertEqual(p.numSubtags(), 1)
def checkMatchingAttr(self): html = HTMLReader().readString(self._html) assert html.tagWithMatchingAttr('color', 'white').name() == 'body' assert html.tagWithMatchingAttr('id', 'dataTable').name() == 'table' assert html.tagWithId('dataTable').name() == 'table'