예제 #1
0
 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;"):
         lt = MockEntity("lt", "&#60;")
         element = Element("<Element attr='blah&lt;blah'/>")
         element.parse_to_end({"lt": lt})
예제 #2
0
 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)
예제 #3
0
 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})
예제 #4
0
 def test_ampersand_expansion(self):
     with self.subTest("&amp;"):
         amp = MockEntity('amp', expansion_text="&#38;")
         text = Helpers.parse_string_literal("Text &amp; 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;"):
         amp = MockEntity('amp', expansion_text="&#38;")
         entity = MockEntity('entity', expansion_text="and Some text &amp;")
         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)
예제 #6
0
    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 &#109; which should not be parsed ]]></end>"
            )
            text.check_wellformedness()
            self.assertEqual(
                "Some text and a char reference &#109; which should not be parsed ",
                text.text)
예제 #7
0
    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)
예제 #8
0
    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)
예제 #9
0
 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"])