def test_external_parameter_entity_definition(self): with self.subTest("SYSTEM"): document = Document("") document._Document__parse_doctype_declaration( "<!DOCTYPE root [<!ENTITY % entity SYSTEM 'uri'>]>") self.assertEqual("uri", document.parameter_entities["entity"].system_URI) self.assertEqual(Entity.Type.PARAMETER, document.parameter_entities["entity"].type) self.assertEqual(True, document.parameter_entities["entity"].external) self.assertEqual(True, document.parameter_entities["entity"].parsed) with self.subTest("PUBLIC"): document = Document("") document._Document__parse_doctype_declaration( "<!DOCTYPE root [<!ENTITY % entity PUBLIC 'uri1' 'uri2'>]>") self.assertEqual("uri1", document.parameter_entities["entity"].public_URI) self.assertEqual("uri2", document.parameter_entities["entity"].system_URI) self.assertEqual(Entity.Type.PARAMETER, document.parameter_entities["entity"].type) self.assertEqual(True, document.parameter_entities["entity"].external) self.assertEqual(True, document.parameter_entities["entity"].parsed)
def test_no_text_in_internal_subset(self): document = Document("") with self.assertRaises(XMLError): document._Document__parse_doctype_declaration("""<!DOCTYPE root [ <!ENTITY % entity 'blah, blahblah'>' some disallowed text ]>""")
def test_unparsed_entity_definition(self): with self.subTest("SYSTEM"): document = Document("") document._Document__parse_doctype_declaration( "<!DOCTYPE root [<!ENTITY entity SYSTEM 'uri' NDATA notation>]>" ) self.assertEqual("uri", document.general_entities["entity"].system_URI) self.assertEqual(Entity.Type.GENERAL, document.general_entities["entity"].type) self.assertEqual(True, document.general_entities["entity"].external) self.assertEqual(False, document.general_entities["entity"].parsed) self.assertEqual("notation", document.general_entities["entity"].notation) with self.subTest("PUBLIC"): document = Document("") document._Document__parse_doctype_declaration( "<!DOCTYPE root [<!ENTITY entity PUBLIC 'uri1' 'uri2' NDATA notation>]>" ) self.assertEqual("uri1", document.general_entities["entity"].public_URI) self.assertEqual("uri2", document.general_entities["entity"].system_URI) self.assertEqual(Entity.Type.GENERAL, document.general_entities["entity"].type) self.assertEqual(True, document.general_entities["entity"].external) self.assertEqual(False, document.general_entities["entity"].parsed) self.assertEqual("notation", document.general_entities["entity"].notation)
def test_forbidden_name_characters(self): for char in ["\u00d7", "\u00f7", "\u037e", "\u2030"]: with self.subTest(char): document = Document("") with self.assertRaises(XMLError): document._Document__parse_doctype_declaration( f"<!DOCTYPE root{char}>")
def test_forbidden_name_start_characters(self): for char in ["-", ".", "0", "7", "\u0305"]: with self.subTest(char): document = Document("") with self.assertRaises(XMLError): document._Document__parse_doctype_declaration( f"<!DOCTYPE {char}root>")
def test_internal_parameter_entity_definition(self): document = Document("") document._Document__parse_doctype_declaration( "<!DOCTYPE root [<!ENTITY % entity 'blah'>]>") self.assertEqual("blah", document.parameter_entities["entity"].expansion_text) self.assertEqual(Entity.Type.PARAMETER, document.parameter_entities["entity"].type) self.assertEqual(False, document.parameter_entities["entity"].external)
def test_root_name_parsing(self): with self.subTest("With no subset"): document = Document("") document._Document__parse_doctype_declaration("<!DOCTYPE root>") self.assertEqual("root", document.dtd_name) with self.subTest("With empty internal subset"): document = Document("") document._Document__parse_doctype_declaration("<!DOCTYPE root []>") self.assertEqual("root", document.dtd_name)
def test_parameter_reference_within_markup(self): with self.subTest("ELEMENT declaration"): document = Document("") with self.assertRaises(XMLError): document._Document__parse_doctype_declaration( """<!DOCTYPE root [ <!ENTITY % entity 'blah, blahblah'>' <!ELEMENT root (%entity;)> ]>""") with self.subTest("NOTATION declaration"): document = Document("") with self.assertRaises(XMLError): document._Document__parse_doctype_declaration( """<!DOCTYPE root [ <!ENTITY % entity 'SYSTEM \"blahblah\"'>' <!NOTATION blah %entity;> ]>""") with self.subTest("ATTLIST declaration"): document = Document("") with self.assertRaises(XMLError): document._Document__parse_doctype_declaration( """<!DOCTYPE root [ <!ENTITY % entity 'CDATA #REQUIRED'>' <!ATTLIST root %entity;> ]>""")
def test_name_collisions(self): with self.subTest( "General and parameter entities occupy different namespaces"): document = Document("") document._Document__parse_doctype_declaration("""<!DOCTYPE root [ <!ENTITY % entity 'SOME TEXT'> <!ENTITY entity 'SOME OTHER TEXT'> ]>""") self.assertEqual( "SOME TEXT", document.parameter_entities["entity"].expansion_text) self.assertEqual( "SOME OTHER TEXT", document.general_entities["entity"].expansion_text) with self.subTest("General entities"): document = Document("") document._Document__parse_doctype_declaration("""<!DOCTYPE root [ <!ENTITY entity 'SOME TEXT'> <!ENTITY entity 'SOME OTHER TEXT'> ]>""") self.assertEqual( "SOME TEXT", document.general_entities["entity"].expansion_text) with self.subTest("Parameter entities"): document = Document("") document._Document__parse_doctype_declaration("""<!DOCTYPE root [ <!ENTITY % entity 'SOME TEXT'> <!ENTITY % entity 'SOME OTHER TEXT'> ]>""") self.assertEqual( "SOME TEXT", document.parameter_entities["entity"].expansion_text)
def test_parameter_entity_expansion(self): with self.subTest("In DTD"): document = Document("") document._Document__parse_doctype_declaration("""<!DOCTYPE root [ <!ENTITY % entity '<!ENTITY entity2 "SOME TEXT">'> %entity; ]>""") self.assertEqual( "SOME TEXT", document.general_entities["entity2"].expansion_text) with self.subTest("In general entity"): document = Document("") document._Document__parse_doctype_declaration("""<!DOCTYPE root [ <!ENTITY % entity 'SOME TEXT'> <!ENTITY entity2 'SOME %entity; OTHER TEXT'> ]>""") self.assertEqual( "SOME TEXT", document.parameter_entities["entity"].expansion_text) self.assertEqual( "SOME SOME TEXT OTHER TEXT", document.general_entities["entity2"].expansion_text)
def test_external_declaration_parsing(self): with self.subTest("SYSTEM"): document = Document("") document._Document__parse_doctype_declaration( "<!DOCTYPE root SYSTEM 'uri'>") self.assertEqual("root", document.dtd_name) self.assertEqual("uri", document.external_system_uri) with self.subTest("PUBLIC"): document = Document("") document._Document__parse_doctype_declaration( "<!DOCTYPE root PUBLIC 'uri1' 'uri2'>") self.assertEqual("root", document.dtd_name) self.assertEqual("uri1", document.external_public_uri) self.assertEqual("uri2", document.external_system_uri) with self.subTest("SYSTEM with internal subset"): document = Document("") document._Document__parse_doctype_declaration( "<!DOCTYPE root SYSTEM 'uri' []>") self.assertEqual("root", document.dtd_name) self.assertEqual("uri", document.external_system_uri)