|
From: <mi...@us...> - 2022-10-11 19:03:03
|
Revision: 9126
http://sourceforge.net/p/docutils/code/9126
Author: milde
Date: 2022-10-11 19:03:00 +0000 (Tue, 11 Oct 2022)
Log Message:
-----------
Fix previous_sibling() method.
Move from nodes.Node to nodes.Element
(requires features only present in the sub-class).
Return `None` if called from first child (cf. [patches:#195]).
Tests.
Modified Paths:
--------------
trunk/docutils/docutils/nodes.py
trunk/docutils/test/test_nodes.py
trunk/docutils/test/test_writers/test_html5_polyglot_parts.py
Modified: trunk/docutils/docutils/nodes.py
===================================================================
--- trunk/docutils/docutils/nodes.py 2022-10-07 09:35:20 UTC (rev 9125)
+++ trunk/docutils/docutils/nodes.py 2022-10-11 19:03:00 UTC (rev 9126)
@@ -325,14 +325,7 @@
except StopIteration:
return None
- def previous_sibling(self):
- """Return preceding sibling node or ``None``."""
- try:
- return self.parent[self.parent.index(self)-1]
- except (AttributeError, IndexError):
- return None
-
class reprunicode(str):
"""
Deprecated backwards compatibility stub. Use the standard `str` instead.
@@ -740,6 +733,14 @@
def index(self, item, start=0, stop=sys.maxsize):
return self.children.index(item, start, stop)
+ def previous_sibling(self):
+ """Return preceding sibling node or ``None``."""
+ try:
+ i = self.parent.index(self)
+ except (AttributeError):
+ return None
+ return self.parent[i-1] if i > 0 else None
+
def is_not_default(self, key):
if self[key] == [] and key in self.list_attributes:
return 0
Modified: trunk/docutils/test/test_nodes.py
===================================================================
--- trunk/docutils/test/test_nodes.py 2022-10-07 09:35:20 UTC (rev 9125)
+++ trunk/docutils/test/test_nodes.py 2022-10-11 19:03:00 UTC (rev 9126)
@@ -140,6 +140,16 @@
self.assertEqual(e.index(e[4]), 1)
self.assertEqual(e.index(e[4], start=2), 4)
+ def test_previous_sibling(self):
+ e = nodes.Element()
+ c1 = nodes.Element()
+ c2 = nodes.Element()
+ e += [c1, c2]
+ # print(c1 == c2)
+ self.assertEqual(e.previous_sibling(), None)
+ self.assertEqual(c1.previous_sibling(), None)
+ self.assertEqual(c2.previous_sibling(), c1)
+
def test_clear(self):
element = nodes.Element()
element += nodes.Element()
Modified: trunk/docutils/test/test_writers/test_html5_polyglot_parts.py
===================================================================
--- trunk/docutils/test/test_writers/test_html5_polyglot_parts.py 2022-10-07 09:35:20 UTC (rev 9125)
+++ trunk/docutils/test/test_writers/test_html5_polyglot_parts.py 2022-10-11 19:03:00 UTC (rev 9126)
@@ -52,8 +52,8 @@
totest = {}
-totest['Title promotion'] = ({'stylesheet_path': '',
- 'embed_stylesheet': 0}, [
+totest['standard'] = ({'stylesheet_path': '',
+ 'embed_stylesheet': 0}, [
["""\
Simple String
""",
@@ -125,6 +125,24 @@
</main>\\n''',
'html_head': '''...<title><string></title>\\n'''}
"""],
+[""".. [CIT2022] A citation.""",
+"""\
+{'fragment': '''<div role="list" class="citation-list">
+<div class="citation" id="cit2022" role="doc-biblioentry">
+<span class="label"><span class="fn-bracket">[</span>CIT2022<span class="fn-bracket">]</span></span>
+<p>A citation.</p>
+</div>
+</div>\\n''',
+ 'html_body': '''<main>
+<div role="list" class="citation-list">
+<div class="citation" id="cit2022" role="doc-biblioentry">
+<span class="label"><span class="fn-bracket">[</span>CIT2022<span class="fn-bracket">]</span></span>
+<p>A citation.</p>
+</div>
+</div>
+</main>\\n''',
+ 'html_head': '''...<title><string></title>\\n'''}
+"""],
["""\
+++++
Title
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|