if ((parent.getHead() != table) || (parent.getTail() != table)) {
removeWhitespaceChildren(parent);
}
if ((parent.getHead() != table) || (parent.getTail() != table)) {
Node child = parent.getHead();
boolean previousNotTable = false;
StylingFactory factory = StylingFactory.getDefaultInstance();
Styles parentStyles = parent.getStyles();
Styles tableStyles;
if (parentStyles == null) {
// This may occur if we are dealing with custom markup
tableStyles = factory.createStyles (null);
if (logger.isDebugEnabled ()) {
logger.debug("Encountered null styles on parent element "
+ parent.getName ());
}
} else {
tableStyles = factory.createInheritedStyles(parentStyles,
DisplayKeywords.TABLE);
}
newTable = allocateElement();
newTable.setName(elementHelper.getTable());
newTable.setStyles(tableStyles);
newTable.setAttribute(OptimizationConstants.OPTIMIZATION_ATTRIBUTE,
OptimizationConstants.OPTIMIZE_ALWAYS);
while (child != null) {
// Allocate a new row/cell within the new table to receive
// the current td's first available child
Styles rowStyles = factory.createInheritedStyles(tableStyles,
DisplayKeywords.TABLE_ROW);
Element newRow = allocateElement();
newRow.setName(elementHelper.getRow());
newRow.setStyles(rowStyles);
Styles cellStyles = factory.createInheritedStyles(rowStyles,
DisplayKeywords.TABLE_CELL);
Element newCell = allocateElement();
newCell.setName(elementHelper.getCell());
newCell.setStyles(cellStyles);
newTable.addTail(newRow);
newRow.addTail(newCell);
// Remove the child from its old parent then add it into the
// new cell
child.remove();
newCell.addTail(child);
previousNotTable = !elementHelper.isTable(child);
child = parent.getHead();
while ((child != null) &&
previousNotTable &&
!elementHelper.isTable(child)) {
// Concatenate all sequential non-table children together
// within the same row/cell
child.remove();
newCell.addTail(child);
child = parent.getHead();
}
}