Fred Drake | 4e7cdb5 | 2001-10-04 20:05:10 +0000 | [diff] [blame] | 1 | import formatter |
| 2 | import htmllib |
| 3 | import unittest |
| 4 | |
Barry Warsaw | 04f357c | 2002-07-23 19:04:11 +0000 | [diff] [blame] | 5 | from test import test_support |
Fred Drake | 4e7cdb5 | 2001-10-04 20:05:10 +0000 | [diff] [blame] | 6 | |
| 7 | |
| 8 | class AnchorCollector(htmllib.HTMLParser): |
| 9 | def __init__(self, *args, **kw): |
| 10 | self.__anchors = [] |
| 11 | htmllib.HTMLParser.__init__(self, *args, **kw) |
| 12 | |
| 13 | def get_anchor_info(self): |
| 14 | return self.__anchors |
| 15 | |
| 16 | def anchor_bgn(self, *args): |
| 17 | self.__anchors.append(args) |
| 18 | |
| 19 | |
| 20 | class HTMLParserTestCase(unittest.TestCase): |
| 21 | def test_anchor_collection(self): |
| 22 | # See SF bug #467059. |
| 23 | parser = AnchorCollector(formatter.NullFormatter(), verbose=1) |
| 24 | parser.feed( |
| 25 | """<a href='http://foo.org/' name='splat'> </a> |
| 26 | <a href='http://www.python.org/'> </a> |
| 27 | <a name='frob'> </a> |
| 28 | """) |
| 29 | parser.close() |
| 30 | self.assertEquals(parser.get_anchor_info(), |
| 31 | [('http://foo.org/', 'splat', ''), |
| 32 | ('http://www.python.org/', '', ''), |
| 33 | ('', 'frob', ''), |
| 34 | ]) |
| 35 | |
| 36 | |
| 37 | def test_main(): |
| 38 | test_support.run_unittest(HTMLParserTestCase) |
| 39 | |
| 40 | |
| 41 | if __name__ == "__main__": |
| 42 | test_main() |