Fixed nested lists splitting

This commit is contained in:
Georgy Litvinov 2020-11-27 22:42:11 +01:00
parent 2a7fbf81f6
commit 5cbcbbadd7
2 changed files with 40 additions and 17 deletions

View file

@ -8,6 +8,7 @@ import static w2phtml.office.XMLString.TEXT_SOFT_PAGE_BREAK;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import pro.litvinovg.xml.Debug;
import w2phtml.office.OfficeReader;
public class ListItemSplitter extends BasicSplitter implements ISplitter {
@ -20,6 +21,8 @@ public class ListItemSplitter extends BasicSplitter implements ISplitter {
public boolean Split(Node listItem) {
Node listItemFirstPart = listItem.cloneNode(false);
Node parent = listItem.getParentNode();
//System.out.println("----Start Item------");
//Debug.prettyPrintXml(parent);
int i = 0;
boolean dataMoved = false;
NodeList listItemNodes = listItem.getChildNodes();
@ -33,10 +36,9 @@ public class ListItemSplitter extends BasicSplitter implements ISplitter {
if (nodeName.equals(TEXT_SOFT_PAGE_BREAK)) {
//Remove SPB.Return result
listItem.removeChild(listItemChild);
//System.out.println("LIST ITEM SPLITTER REMOVE SPB");
} else if (nodeName.equals(TEXT_LIST)) {
if (factory.split(listItemChild)){
dataMoved=true;
}
factory.split(listItemChild);
} else if (nodeName.equals(TEXT_H) || nodeName.equals(TEXT_P)) {
if (factory.split(listItemChild)){
@ -63,6 +65,8 @@ public class ListItemSplitter extends BasicSplitter implements ISplitter {
if(dataMoved) {
parent.insertBefore(listItemFirstPart, listItem);
}
//System.out.println("-----FINISH Item-----");
//Debug.prettyPrintXml(parent);
return dataMoved;
}

View file

@ -3,8 +3,10 @@ package w2phtml.pageSplitters;
import static w2phtml.office.XMLString.TEXT_CONTINUE_NUMBERING;
import static w2phtml.office.XMLString.TEXT_LIST_HEADER;
import static w2phtml.office.XMLString.TEXT_LIST_ITEM;
import static w2phtml.office.XMLString.TEXT_LIST;
import static w2phtml.office.XMLString.TEXT_SOFT_PAGE_BREAK;
import org.apache.commons.lang3.StringUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
@ -22,11 +24,13 @@ public class ListSplitter extends BasicSplitter implements ISplitter {
@Override
public boolean Split(Node list) {
Node parent = list.getParentNode();
// System.out.println("----Start ListSplitter------");
// Debug.prettyPrintXml(parent);
Node listFirstPart = list.cloneNode(false);
NodeList listNodes = list.getChildNodes();
Document document = list.getOwnerDocument();
Element softPageBreak = document.createElement(TEXT_SOFT_PAGE_BREAK);
Node hangingItem = null;
Node nextPageItemPart = null;
int i = 0;
boolean dataMoved = false;
while (listNodes.getLength() > i) {
@ -52,7 +56,7 @@ public class ListSplitter extends BasicSplitter implements ISplitter {
// Add first part of list item to previous list item
listFirstPart.appendChild(child.getPreviousSibling());
if (child.hasChildNodes()) {
hangingItem = child;
nextPageItemPart = child;
} else {
list.removeChild(child);
}
@ -75,20 +79,35 @@ public class ListSplitter extends BasicSplitter implements ISplitter {
((Element) list).setAttribute(TEXT_CONTINUE_NUMBERING, "true");
parent.insertBefore(listFirstPart, list);
parent.insertBefore(softPageBreak, list);
if (hangingItem != null) {
// System.out.println("LIST FIRST PART");
// Debug.printNode(listFirstPart);
// TODO: Create dummy list style to override list item to not teleport
// second part but making it list item without list mark
if (nextPageItemPart != null) {
Node nextPageFirstNode = nextPageItemPart.getFirstChild();
i = 0;
NodeList freeNodes = hangingItem.getChildNodes();
NodeList freeNodes = nextPageItemPart.getChildNodes();
while (freeNodes.getLength() > 0) {
Node freeNode = freeNodes.item(i);
String freeNodeName = freeNode.getNodeName();
if (StringUtils.equals(TEXT_LIST, freeNodeName)) {
break;
}
parent.insertBefore(freeNode, list);
}
list.removeChild(hangingItem);
if (!nextPageItemPart.hasChildNodes()) {
list.removeChild(nextPageItemPart);
}
}
} else {
// If SPB right before list items
parent.insertBefore(softPageBreak, list);
// parent.insertBefore(softPageBreak, list);
}
// System.out.println("----Finish ListSplitter------");
// Debug.prettyPrintXml(parent);
return dataMoved;
}