def test_disallowed_attribute_entity_replacement_character(self): with self.subTest("Disallow <"): entity = MockEntity("entity", expansion_text="<") element = Element("<Element attr='blah&entity;blah'/>") with self.assertRaises(XMLError): element.parse_to_end({"entity": entity}) with self.subTest("Allow <"): lt = MockEntity("lt", "<") element = Element("<Element attr='blah<blah'/>") element.parse_to_end({"lt": lt})
def test_respects_ignore_flags(self): with self.subTest("General"): entity = MockEntity("entity", expansion_text="SOMEVALUE") text = Helpers.parse_string_literal(f"Some text and an entity: &entity;", general_entities={"entity": entity}) self.assertEqual("Some text and an entity: SOMEVALUE", text) with self.subTest("Parameter"): entity = MockEntity("entity", expansion_text="SOMEVALUE") text = Helpers.parse_string_literal(f"Some text and an entity: %entity;", parameter_entities={"entity": entity}) self.assertEqual("Some text and an entity: SOMEVALUE", text)
def test_no_recursion(self): with self.subTest("Direct recursion"): entity = MockEntity("entity", expansion_text="Some Text &entity;") with self.assertRaises(XMLError): Helpers.parse_string_literal("Text &entity;", general_entities={"entity": entity}) with self.subTest("Indirect recursion"): entity1 = MockEntity("entity1", expansion_text="Some Text &entity2;") entity2 = MockEntity("entity2", expansion_text="Some Text &entity1;") with self.assertRaises(XMLError): Helpers.parse_string_literal("Text &entity;", general_entities={"entity1": entity1, "entity2": entity2})
def test_ampersand_expansion(self): with self.subTest("&"): amp = MockEntity('amp', expansion_text="&") text = Helpers.parse_string_literal("Text & an entity", general_entities={"amp": amp}) self.assertEqual("Text & an entity", text) with self.subTest("Entity containing &"): entity = MockEntity('entity', expansion_text="Some text &") with self.assertRaises(XMLError): Helpers.parse_string_literal("Text &entity; an entity", general_entities={"entity": entity}) with self.subTest("Entity containing &"): amp = MockEntity('amp', expansion_text="&") entity = MockEntity('entity', expansion_text="and Some text &") text = Helpers.parse_string_literal("Text &entity; an entity", general_entities={"amp": amp, "entity": entity}) self.assertEqual("Text and Some text & an entity", text)
def test_ignores_general_entities(self): entity = MockEntity("entity", expansion_text="SOMEVALUE") pi = ProcessingInstruction( "<?Target Some data with &entity;?> and some text<end/>") pi.parse_to_end({"entity": entity}) self.assertEqual("Some data with &entity;", pi.data)
def test_ignores_cdata_entity_references(self): with self.subTest("General entity references"): entity = MockEntity("entity", expansion_text="SOMEVALUE") text = Text() text.add_text( "Some text and an<![CDATA[ &entity; which should not be parsed ]]></end>" ) text.check_wellformedness() self.assertEqual( "Some text and an &entity; which should not be parsed ", text.text) with self.subTest("Character references"): text = Text() text.add_text( "Some text and a<![CDATA[ char reference m which should not be parsed ]]></end>" ) text.check_wellformedness() self.assertEqual( "Some text and a char reference m which should not be parsed ", text.text)
def test_parameter_entity(self): entity = MockEntity("entity", expansion_text="SOMEVALUE", entity_type=MockEntity.Type.PARAMETER) expansion_text = Helpers.parse_reference(f"%entity;", parameter_entities={"entity": entity}) self.assertEqual("SOMEVALUE", expansion_text)
def test_general_entity(self): entity = MockEntity("entity", expansion_text="SOMEVALUE") expansion_text = Helpers.parse_reference(f"&entity;", general_entities={"entity": entity}) self.assertEqual("SOMEVALUE", expansion_text)
def test_attribute_general_entity_replacement(self): entity = MockEntity("entity", expansion_text="ENTITY TEXT") element = Element("<Element attr='Entity &entity; text'/>") element.parse_to_end({"entity": entity}) self.assertEqual("Entity ENTITY TEXT text", element.attributes["attr"])