# adapted from http://www.iamcal.com/publish/articles/php/processing_html_part_2/ # and from http://feedparser.org/tests/wellformed/sanitize/ # by Aaron Swartz, 2006, public domain import unittest, new from planet import sanitize class SanitizeTest(unittest.TestCase): pass # each call to HTML adds a test case to SanitizeTest testcases = 0 def HTML(a, b): global testcases testcases += 1 func = lambda self: self.assertEqual(sanitize.HTML(a), b) method = new.instancemethod(func, None, SanitizeTest) setattr(SanitizeTest, "test_%d" % testcases, method) ## basics HTML("","") HTML("hello","hello") ## balancing tags HTML("hello","hello") HTML("hello","hello") HTML("hello","hello") HTML("hello","hello") HTML("hello","hello") HTML("","") ## trailing slashes HTML('','') HTML('','') HTML('','') ## balancing angle brakets HTML('','b>') HTML('','>') HTML('foofoo','b>foo') HTML('>') HTML('b><','b>') HTML('>','>') ## attributes HTML('','') HTML('','') HTML('','') ## dangerous tags (a small sample) sHTML = lambda x: HTML(x, 'safe description') sHTML('safe description') sHTML('safe description') sHTML('safe description') sHTML('safe description') sHTML('safe description') sHTML('safe') sHTML('safe description') sHTML('safe description') sHTML('safe description') sHTML('safe description') for x in ['onabort', 'onblur', 'onchange', 'onclick', 'ondblclick', 'onerror', 'onfocus', 'onkeydown', 'onkeypress', 'onkeyup', 'onload', 'onmousedown', 'onmouseout', 'onmouseover', 'onmouseup', 'onreset', 'resize', 'onsubmit', 'onunload']: HTML('' % x, '') HTML('never trust your upstream platypus', 'never trust your upstream platypus') ## ignorables HTML('foo', 'foo') ## non-allowed tags HTML('','') HTML('\r\n\r\n\r\n\r\n\r\nfunction executeMe()\r\n{\r\n\r\n\r\n\r\n\r\n/*