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.Node;
import org.w3c.dom.NodeList; import org.w3c.dom.NodeList;
import pro.litvinovg.xml.Debug;
import w2phtml.office.OfficeReader; import w2phtml.office.OfficeReader;
public class ListItemSplitter extends BasicSplitter implements ISplitter { public class ListItemSplitter extends BasicSplitter implements ISplitter {
@ -20,6 +21,8 @@ public class ListItemSplitter extends BasicSplitter implements ISplitter {
public boolean Split(Node listItem) { public boolean Split(Node listItem) {
Node listItemFirstPart = listItem.cloneNode(false); Node listItemFirstPart = listItem.cloneNode(false);
Node parent = listItem.getParentNode(); Node parent = listItem.getParentNode();
//System.out.println("----Start Item------");
//Debug.prettyPrintXml(parent);
int i = 0; int i = 0;
boolean dataMoved = false; boolean dataMoved = false;
NodeList listItemNodes = listItem.getChildNodes(); NodeList listItemNodes = listItem.getChildNodes();
@ -33,10 +36,9 @@ public class ListItemSplitter extends BasicSplitter implements ISplitter {
if (nodeName.equals(TEXT_SOFT_PAGE_BREAK)) { if (nodeName.equals(TEXT_SOFT_PAGE_BREAK)) {
//Remove SPB.Return result //Remove SPB.Return result
listItem.removeChild(listItemChild); listItem.removeChild(listItemChild);
//System.out.println("LIST ITEM SPLITTER REMOVE SPB");
} else if (nodeName.equals(TEXT_LIST)) { } else if (nodeName.equals(TEXT_LIST)) {
if (factory.split(listItemChild)){ factory.split(listItemChild);
dataMoved=true;
}
} else if (nodeName.equals(TEXT_H) || nodeName.equals(TEXT_P)) { } else if (nodeName.equals(TEXT_H) || nodeName.equals(TEXT_P)) {
if (factory.split(listItemChild)){ if (factory.split(listItemChild)){
@ -63,6 +65,8 @@ public class ListItemSplitter extends BasicSplitter implements ISplitter {
if(dataMoved) { if(dataMoved) {
parent.insertBefore(listItemFirstPart, listItem); parent.insertBefore(listItemFirstPart, listItem);
} }
//System.out.println("-----FINISH Item-----");
//Debug.prettyPrintXml(parent);
return dataMoved; 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_CONTINUE_NUMBERING;
import static w2phtml.office.XMLString.TEXT_LIST_HEADER; import static w2phtml.office.XMLString.TEXT_LIST_HEADER;
import static w2phtml.office.XMLString.TEXT_LIST_ITEM; 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 static w2phtml.office.XMLString.TEXT_SOFT_PAGE_BREAK;
import org.apache.commons.lang3.StringUtils;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.w3c.dom.Element; import org.w3c.dom.Element;
import org.w3c.dom.Node; import org.w3c.dom.Node;
@ -22,11 +24,13 @@ public class ListSplitter extends BasicSplitter implements ISplitter {
@Override @Override
public boolean Split(Node list) { public boolean Split(Node list) {
Node parent = list.getParentNode(); Node parent = list.getParentNode();
// System.out.println("----Start ListSplitter------");
// Debug.prettyPrintXml(parent);
Node listFirstPart = list.cloneNode(false); Node listFirstPart = list.cloneNode(false);
NodeList listNodes = list.getChildNodes(); NodeList listNodes = list.getChildNodes();
Document document = list.getOwnerDocument(); Document document = list.getOwnerDocument();
Element softPageBreak = document.createElement(TEXT_SOFT_PAGE_BREAK); Element softPageBreak = document.createElement(TEXT_SOFT_PAGE_BREAK);
Node hangingItem = null; Node nextPageItemPart = null;
int i = 0; int i = 0;
boolean dataMoved = false; boolean dataMoved = false;
while (listNodes.getLength() > i) { while (listNodes.getLength() > i) {
@ -52,11 +56,11 @@ public class ListSplitter extends BasicSplitter implements ISplitter {
// Add first part of list item to previous list item // Add first part of list item to previous list item
listFirstPart.appendChild(child.getPreviousSibling()); listFirstPart.appendChild(child.getPreviousSibling());
if (child.hasChildNodes()) { if (child.hasChildNodes()) {
hangingItem = child; nextPageItemPart = child;
} else { } else {
list.removeChild(child); list.removeChild(child);
} }
} }
// Debug.printNode(child); // Debug.printNode(child);
break; break;
@ -75,20 +79,35 @@ public class ListSplitter extends BasicSplitter implements ISplitter {
((Element) list).setAttribute(TEXT_CONTINUE_NUMBERING, "true"); ((Element) list).setAttribute(TEXT_CONTINUE_NUMBERING, "true");
parent.insertBefore(listFirstPart, list); parent.insertBefore(listFirstPart, list);
parent.insertBefore(softPageBreak, list); parent.insertBefore(softPageBreak, list);
if (hangingItem != null) { // System.out.println("LIST FIRST PART");
i = 0; // Debug.printNode(listFirstPart);
NodeList freeNodes = hangingItem.getChildNodes(); // TODO: Create dummy list style to override list item to not teleport
while (freeNodes.getLength() > 0) { // second part but making it list item without list mark
Node freeNode = freeNodes.item(i);
parent.insertBefore(freeNode, list); if (nextPageItemPart != null) {
} Node nextPageFirstNode = nextPageItemPart.getFirstChild();
list.removeChild(hangingItem); i = 0;
} 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);
}
if (!nextPageItemPart.hasChildNodes()) {
list.removeChild(nextPageItemPart);
}
}
} else { } else {
//If SPB right before list items // If SPB right before list items
parent.insertBefore(softPageBreak, list); // parent.insertBefore(softPageBreak, list);
} }
// System.out.println("----Finish ListSplitter------");
// Debug.prettyPrintXml(parent);
return dataMoved; return dataMoved;
} }