def test_boolean_attribute(self): # ability to serialize boolean attribute by setting value to None form = html.Element('form') form.set('novalidate', None) self.assertEqual(html.tostring(form), _bytes('<form novalidate></form>')) form.set('custom') self.assertEqual(html.tostring(form), _bytes('<form novalidate custom></form>'))
def test_html5_doctype(self): # document type declaration with neither public if nor system url doc = html.Element('html').getroottree() doc.docinfo.public_id = None doc.docinfo.system_url = None self.assertEqual(doc.docinfo.doctype, '<!DOCTYPE html>') self.assertTrue(doc.docinfo.public_id is None) self.assertEqual(self.etree.tostring(doc), _bytes('<!DOCTYPE html>\n<html/>'))
def test_ietf_decl(self): # legacy declaration with public id, no system url doc = html.Element('html').getroottree() doc.docinfo.public_id = '-//IETF//DTD HTML//EN' doc.docinfo.system_url = None self.assertEqual(doc.docinfo.doctype, '<!DOCTYPE html PUBLIC "-//IETF//DTD HTML//EN">') self.assertEqual(self.etree.tostring(doc), _bytes('<!DOCTYPE html PUBLIC "-//IETF//DTD HTML//EN">\n<html/>'))
def test_html_decl(self): # Slightly different to one above: when we create an html element, # we do not start with a blank slate. doc = html.Element('html').getroottree() doc.docinfo.public_id = 'bar' doc.docinfo.system_url = 'baz' self.assertEqual(doc.docinfo.doctype, '<!DOCTYPE html PUBLIC "bar" "baz">') self.assertEqual(etree.tostring(doc), _bytes('<!DOCTYPE html PUBLIC "bar" "baz">\n<html/>'))
def test_set_decl_html(self): doc = html.Element('html').getroottree() doc.docinfo.public_id = "-//W3C//DTD XHTML 1.0 Strict//EN" doc.docinfo.system_url = \ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" self.assertEqual(doc.docinfo.doctype, '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">') self.assertEqual(self.etree.tostring(doc), _bytes('''<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"></html>'''))
def test_clean_doctype(self): doc = html.Element('html').getroottree() self.assertTrue(doc.docinfo.doctype != '') doc.docinfo.clear() self.assertTrue(doc.docinfo.doctype == '')