// Need to examine the children of the indivisible element to see
// if it contains any anti-xxx elements that have an open
// counterpart. If one is found we need to push the open
// counterpart into the indivisible element and promote and then
// remove the anti-element.
Node x = element.getHead();
while (x != null) {
boolean removed = false;
if (x instanceof Element) {
Element childElement = (Element) x;
String child = childElement.getName();
boolean pushedDown = false;
if (styleCfg.isAntiElement(child)) {
// Found an anti-element...push any open stylistic elements
// into this indivisible element.
Iterator stylisticElements = styleCfg.
getCounterpartElementNames(child);
while (stylisticElements.hasNext()) {
String stylisticElement =
(String) stylisticElements.next();
if (tracker.isOpen(stylisticElement)) {
// This child is an anti-element and its
// counterpart is open. We need to push the
// counterpart into the indivisible element.
pushCounterpartElementDown(element,
stylisticElement);
pushedDown = true;
tracker.closed(stylisticElement);
}
}
if (pushedDown) {
// Promote and then remove anti-element.
childElement.promote(true);
removeElement(childElement);
removed = true;
// I have had an anti-element that has been
// promoted. The dom tree may have changed
// so we should terminate the traversal here.
// terminateTraversal = true;
} else {
// anti-element may be removed (but take care
// since we still want to continue this
// iteration.
removeElement(childElement);
removed = true;
}
// Start the traversal from the beginning again
// because we have promoted and/or removed a child
// element. Doing this is guarenteed to be fail-safe.
x = element.getHead();
} else {
// It is not an anti-element but its children may
// be or their children may be so we need to ensure
// that they are also visited.
visit(childElement);
}
}
if (!removed && (x != null)) {
x = x.getNext();
}
}
}
element.forEachChild(this);