diff options
author | Stefan Behnel <stefan_ml@behnel.de> | 2019-07-24 18:08:02 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-07-24 18:08:02 (GMT) |
commit | c6cb4cdd21c0c3a09b0617dbfaa7053d3bfa6def (patch) | |
tree | 6594bbede68651f6be2fdcb4b8db1e537a6bc4f2 /Lib/test/test_xml_etree.py | |
parent | 151b91dfd21a100ecb1eba9e293c0a8695bf3bf5 (diff) | |
download | cpython-c6cb4cdd21c0c3a09b0617dbfaa7053d3bfa6def.zip cpython-c6cb4cdd21c0c3a09b0617dbfaa7053d3bfa6def.tar.gz cpython-c6cb4cdd21c0c3a09b0617dbfaa7053d3bfa6def.tar.bz2 |
bpo-37399: Correctly attach tail text to the last element/comment/pi (GH-14856)
* bpo-37399: Correctly attach tail text to the last element/comment/pi, even when comments or pis are discarded.
Also fixes the insertion of PIs when "insert_pis=True" is configured for a TreeBuilder.
Diffstat (limited to 'Lib/test/test_xml_etree.py')
-rw-r--r-- | Lib/test/test_xml_etree.py | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/Lib/test/test_xml_etree.py b/Lib/test/test_xml_etree.py index 6173749..b2492cd 100644 --- a/Lib/test/test_xml_etree.py +++ b/Lib/test/test_xml_etree.py @@ -2954,6 +2954,66 @@ class TreeBuilderTest(unittest.TestCase): self.assertEqual(b.pi('target'), (len('target'), None)) self.assertEqual(b.pi('pitarget', ' text '), (len('pitarget'), ' text ')) + def test_late_tail(self): + # Issue #37399: The tail of an ignored comment could overwrite the text before it. + class TreeBuilderSubclass(ET.TreeBuilder): + pass + + xml = "<a>text<!-- comment -->tail</a>" + a = ET.fromstring(xml) + self.assertEqual(a.text, "texttail") + + parser = ET.XMLParser(target=TreeBuilderSubclass()) + parser.feed(xml) + a = parser.close() + self.assertEqual(a.text, "texttail") + + xml = "<a>text<?pi data?>tail</a>" + a = ET.fromstring(xml) + self.assertEqual(a.text, "texttail") + + xml = "<a>text<?pi data?>tail</a>" + parser = ET.XMLParser(target=TreeBuilderSubclass()) + parser.feed(xml) + a = parser.close() + self.assertEqual(a.text, "texttail") + + def test_late_tail_mix_pi_comments(self): + # Issue #37399: The tail of an ignored comment could overwrite the text before it. + # Test appending tails to comments/pis. + class TreeBuilderSubclass(ET.TreeBuilder): + pass + + xml = "<a>text<?pi1?> <!-- comment -->\n<?pi2?>tail</a>" + parser = ET.XMLParser(target=ET.TreeBuilder(insert_comments=True)) + parser.feed(xml) + a = parser.close() + self.assertEqual(a[0].text, ' comment ') + self.assertEqual(a[0].tail, '\ntail') + self.assertEqual(a.text, "text ") + + parser = ET.XMLParser(target=TreeBuilderSubclass(insert_comments=True)) + parser.feed(xml) + a = parser.close() + self.assertEqual(a[0].text, ' comment ') + self.assertEqual(a[0].tail, '\ntail') + self.assertEqual(a.text, "text ") + + xml = "<a>text<!-- comment -->\n<?pi data?>tail</a>" + parser = ET.XMLParser(target=ET.TreeBuilder(insert_pis=True)) + parser.feed(xml) + a = parser.close() + self.assertEqual(a[0].text, 'pi data') + self.assertEqual(a[0].tail, 'tail') + self.assertEqual(a.text, "text\n") + + parser = ET.XMLParser(target=TreeBuilderSubclass(insert_pis=True)) + parser.feed(xml) + a = parser.close() + self.assertEqual(a[0].text, 'pi data') + self.assertEqual(a[0].tail, 'tail') + self.assertEqual(a.text, "text\n") + def test_treebuilder_elementfactory_none(self): parser = ET.XMLParser(target=ET.TreeBuilder(element_factory=None)) parser.feed(self.sample1) |