if __name__ == '__main__': # XXX Allow us to import the sibling module import os, sys os.chdir(os.path.split(os.path.abspath(__file__))[0]) sys.path.insert(0, os.path.abspath(os.path.join(os.pardir, "src"))) from xhtml5parser import * import unittest, re def sortattrs(match): """ sort all XML attributes by name """ name = match.group(1) attrs = re.findall('([-:\w]+)="([^"]*)"', match.group(2)) if not attrs: return "<%s%s%s>" % match.groups() attrs.sort() attrs = ' '.join(['%s="%s"' % (n,v) for n,v in attrs]) return "<%s %s%s>" % (name, attrs, match.group(3)) def ncr(match): """ replace Numeric Character References with their utf-8 equivalents """ return unichr(int(match.group(1))).encode('utf-8') xmlelem = re.compile(r'<(\w+)((?: [-:\w]+="[^"]*")+)(/?)>') class Xhtml5Test(unittest.TestCase): def assertXmlEquals(self, input, expected=None, parser=XMLParser): document = parser(tree=XTreeBuilder).parse(input) if not expected: # test to see that the input data wasn't modified in any way other # than re-arranging attribute order and replacing numeric character # references with their UTF-8 equivalents. expected = xmlelem.sub(sortattrs, input) expected = re.sub('(\d+);', ncr, expected) self.assertEquals(expected, xmlelem.sub(sortattrs, document.toxml())) else: self.assertEquals(expected, document.toxml()) def assertXhtmlEquals(self, input, expected=None, parser=XHTMLParser): self.assertXmlEquals(input, expected, parser) class BasicXhtml5Test(Xhtml5Test): def test_title_body_mismatched_close(self): self.assertXhtmlEquals( '