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

milde--- via Docutils-checkins <[email protected]>
Newsgroups gmane.text.docutils.cvs
Message-ID <[email protected]>
Revision: 9695
          http://sourceforge.net/p/docutils/code/9695
Author:   milde
Date:     2024-05-08 07:11:05 +0000 (Wed, 08 May 2024)
Log Message:
-----------
Doctree validation: Validate number and types of element children.

The "manpage" writer tests include a sample with invalid doctree
(`<citation>` without content, generated from an "rST" source without warning)
to ensure this case is handled gracefully -> ensure "validate" setting is off.

Modified Paths:
--------------
    trunk/docutils/docutils/nodes.py
    trunk/docutils/test/test_writers/test_manpage.py

Modified: trunk/docutils/docutils/nodes.py
===================================================================
--- trunk/docutils/docutils/nodes.py	2024-05-08 07:10:56 UTC (rev 9694)
+++ trunk/docutils/docutils/nodes.py	2024-05-08 07:11:05 UTC (rev 9695)
@@ -1146,8 +1146,16 @@
         except ValueError as e:
             messages.append(e.args[0])  # the message argument
         # TODO: check number of children
+        n_min, n_max = self.valid_len
+        if len(self.children) < n_min:
+            messages.append(f'Expects at least {n_min} children, '
+                            f'not {len(self.children)}.')
+        if n_max is not None and len(self.children) > n_max:
+            messages.append(f'Expects at most {n_max} children, '
+                            f'not {len(self.children)}.')
         for child in self.children:
-            # TODO: check whether child has allowed type
+            if not isinstance(child, self.valid_children):
+                messages.append(f'May not contain "{child.tagname}" elements.')
             child.validate()
         if messages:
             msg = f'Element <{self.tagname}> invalid:\n' + '\n'.join(messages)

Modified: trunk/docutils/test/test_writers/test_manpage.py
===================================================================
--- trunk/docutils/test/test_writers/test_manpage.py	2024-05-08 07:10:56 UTC (rev 9694)
+++ trunk/docutils/test/test_writers/test_manpage.py	2024-05-08 07:11:05 UTC (rev 9695)
@@ -34,6 +34,7 @@
                         settings_overrides={
                             '_disable_config': True,
                             'strict_visitor': True,
+                            'validate': False,  # allow testing invalid doctree
                         }).decode()
                     self.assertEqual(case_expected, output)
 

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.