Properly handle special case, when list depth decreases and depth is misaligned
[sixth.git] / src / main / java / eu / svjatoslav / sixth / core / document / Heading.java
index db9f9df..f4a9513 100644 (file)
@@ -73,6 +73,7 @@ public class Heading {
         FormattedText title = FormattedText.fromOrg(parseFullListTitle(getPartialTitle(listSections), tm.getTokenizer(), indent));
 
         if (indent > currentListElement.indent){
+            // list dept increases
             ListElement newElement = new ListElement(title, indent, currentListElement, type);
             currentListElement.addContent(newElement);
             currentListElement = newElement;
@@ -80,16 +81,28 @@ public class Heading {
         }
 
         if (indent == currentListElement.indent){
+            // list depth is the same
             ListElement newElement = new ListElement(title, indent, currentListElement.parent, type);
             currentListElement.parent.addContent(newElement);
             currentListElement = newElement;
             return;
         }
 
+        // list dept decreases
         while (true){
             if (currentListElement.parent.indent <= indent){
-                ListElement newElement = new ListElement(title, indent, currentListElement.parent, type);
-                currentListElement.parent.addContent(newElement);
+                if (currentListElement.parent.indent < 0){
+                    // reached first depth level, cannot go any deeper.
+                    // This special situation arisesbb only when lint indents are not properly aligned.
+                    // That is, document structure is incorrect.
+                    ListElement newElement = new ListElement(title, indent, currentListElement.parent, type);
+                    currentListElement.parent.addContent(newElement);
+                    currentListElement = newElement;
+                    return;
+                }
+
+                ListElement newElement = new ListElement(title, indent, currentListElement.parent.parent, type);
+                currentListElement.parent.parent.addContent(newElement);
                 currentListElement = newElement;
                 return;
             }