SF.net SVN: docutils:[9753] trunk/docutils

milde--- via Docutils-checkins <[email protected]>
Newsgroups gmane.text.docutils.cvs
Message-ID <[email protected]>
Revision: 9753
          http://sourceforge.net/p/docutils/code/9753
Author:   milde
Date:     2024-06-14 11:04:53 +0000 (Fri, 14 Jun 2024)
Log Message:
-----------
xml-parser: Handle case that the input has no XML element.

If there is no XML element in the input, the XMLPullParser does
not yield "events".

To handle this, ``element2node(None)`` now returns a
`paragraph` with a `problematic` node reporting the problem.

Modified Paths:
--------------
    trunk/docutils/docutils/parsers/docutils_xml.py
    trunk/docutils/test/test_parsers/test_docutils_xml/test_parse.py
    trunk/docutils/test/test_parsers/test_docutils_xml/test_parse_element.py

Modified: trunk/docutils/docutils/parsers/docutils_xml.py
===================================================================
--- trunk/docutils/docutils/parsers/docutils_xml.py	2024-06-12 15:11:09 UTC (rev 9752)
+++ trunk/docutils/docutils/parsers/docutils_xml.py	2024-06-14 11:04:53 UTC (rev 9753)
@@ -116,7 +116,9 @@
         document = utils.new_document('xml input',
                                       frontend.get_default_settings(Parser))
         document.source == 'xml input'
-
+    if element is None:
+        problem = nodes.problematic('', 'No XML element found.')
+        return nodes.paragraph('', '', problem)
     # Get the corresponding `nodes.Element` instance:
     try:
         nodeclass = getattr(nodes, element.tag)

Modified: trunk/docutils/test/test_parsers/test_docutils_xml/test_parse.py
===================================================================
--- trunk/docutils/test/test_parsers/test_docutils_xml/test_parse.py	2024-06-12 15:11:09 UTC (rev 9752)
+++ trunk/docutils/test/test_parsers/test_docutils_xml/test_parse.py	2024-06-14 11:04:53 UTC (rev 9753)
@@ -204,6 +204,15 @@
         A paragraph.
     spurious tailing text
 """],
+["""\
+just spurious text
+""",
+"""\
+<document source="test data">
+    <paragraph>
+        <problematic>
+            No XML element found.
+"""],
 ]
 
 

Modified: trunk/docutils/test/test_parsers/test_docutils_xml/test_parse_element.py
===================================================================
--- trunk/docutils/test/test_parsers/test_docutils_xml/test_parse_element.py	2024-06-12 15:11:09 UTC (rev 9752)
+++ trunk/docutils/test/test_parsers/test_docutils_xml/test_parse_element.py	2024-06-14 11:04:53 UTC (rev 9753)
@@ -50,6 +50,11 @@
         node = docutils_xml.parse_element(xml, self.document)
         self.assertEqual('<strong>text</strong>', str(node))
 
+    def test_nothing_but_junk_text(self):
+        xml = 'just text'
+        node = docutils_xml.parse_element(xml, self.document)
+        self.assertEqual(node.astext(), 'No XML element found.')
+
     def test_nonexistent_element_type(self):
         xml = '<tip><p>some text</p></tip>'
         node = docutils_xml.parse_element(xml, self.document)

This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.