Fix nested lists DOM generation and add tests.

This commit is contained in:
Sergio Martínez Portela 2023-05-01 20:40:02 +02:00
parent 790ef57598
commit 36a0a00113
3 changed files with 52 additions and 2 deletions

View File

@ -464,8 +464,9 @@ class Headline:
if isinstance(c, dom.ListItem) if isinstance(c, dom.ListItem)
] ]
if ((len(list_children) > 0) if (len(list_children) == 0):
and (len(list_children[-1].orig.indentation) break
if ((len(list_children[-1].orig.indentation)
<= len(line.indentation))): <= len(line.indentation))):
# No more breaking out of lists, it's indentation # No more breaking out of lists, it's indentation
# is less than ours # is less than ours

16
tests/11-nested-lists.org Normal file
View File

@ -0,0 +1,16 @@
#+TITLE: 11-Nested lists
#+DESCRIPTION: Simple org file to test nested lists
#+TODO: TODO(t) PAUSED(p) | DONE(d)
* Nested lists
:PROPERTIES:
:ID: 11-nested-lists
:CREATED: [2020-01-01 Wed 01:01]
:END:
- 1
- 1.1
- 1.2
- 2
- 2.1
- 2.2
- 3

View File

@ -724,6 +724,39 @@ class TestSerde(unittest.TestCase):
self.assertTrue(len(non_props) == 1, self.assertTrue(len(non_props) == 1,
'Expected <List>, with only (1) element') 'Expected <List>, with only (1) element')
def test_nested_lists_html_file_11(self):
with open(os.path.join(DIR, "11-nested-lists.org")) as f:
doc = load(f)
hl = doc.getTopHeadlines()[0]
tree = hl.as_dom()
non_props = [
item
for item in tree
if not isinstance(item, dom.PropertyDrawerNode)
]
print_tree(non_props)
self.assertTrue((len(non_props) == 1) and (isinstance(non_props[0], dom.ListGroupNode)),
'Expected only <List> as top level')
dom_list = non_props[0]
children = dom_list.children
self.assertTrue(len(children) == 5, 'Expected 5 items inside <List>, 3 texts and 2 sublists')
# Assert texts
self.assertEqual(children[0].content, ['1'])
self.assertEqual(children[2].content, ['2'])
self.assertEqual(children[4].content[0], '3') # Might be ['3', '\n'] but shouldn't be a breaking change
# Assert lists
self.assertTrue(isinstance(children[1], dom.ListGroupNode), 'Expected sublist inside "1"')
self.assertEqual(children[1].children[0].content, ['1.1'])
self.assertEqual(children[1].children[1].content, ['1.2'])
self.assertTrue(isinstance(children[3], dom.ListGroupNode), 'Expected sublist inside "2"')
self.assertEqual(children[3].children[0].content, ['2.1'])
self.assertEqual(children[3].children[1].content, ['2.2'])
def print_tree(tree, indentation=0, headline=None): def print_tree(tree, indentation=0, headline=None):
for element in tree: for element in tree: