List<Node> deletedNodes = oldComp.getBodyNode().getMinimalDeletedSet(
deletedID);
// Set prevLeaf to the leaf after which the old HTML needs to be
// inserted
Node prevLeaf = null;
if (before > 0)
prevLeaf = getTextNode(before - 1);
// Set nextLeaf to the leaf before which the old HTML needs to be
// inserted
Node nextLeaf = null;
if (before < getRangeCount())
nextLeaf = getTextNode(before);
while (deletedNodes.size() > 0) {
LastCommonParentResult prevResult, nextResult;
if (prevLeaf != null) {
prevResult = prevLeaf.getLastCommonParent(deletedNodes.get(0));
} else {
prevResult = new LastCommonParentResult();
prevResult.setLastCommonParent(getBodyNode());
prevResult.setIndexInLastCommonParent(-1);
}
if (nextLeaf != null) {
nextResult = nextLeaf.getLastCommonParent(deletedNodes
.get(deletedNodes.size() - 1));
} else {
nextResult = new LastCommonParentResult();
nextResult.setLastCommonParent(getBodyNode());
nextResult.setIndexInLastCommonParent(getBodyNode()
.getNbChildren());
}
if (prevResult.getLastCommonParentDepth() == nextResult
.getLastCommonParentDepth()) {
// We need some metric to choose which way to add...
if (deletedNodes.get(0).getParent() == deletedNodes.get(
deletedNodes.size() - 1).getParent()
&& prevResult.getLastCommonParent() == nextResult
.getLastCommonParent()) {
// The difference is not in the parent
prevResult.setLastCommonParentDepth(prevResult
.getLastCommonParentDepth() + 1);
} else {
// The difference is in the parent, so compare them
// now THIS is tricky
double distancePrev = deletedNodes.get(0).getParent()
.getMatchRatio(prevResult.getLastCommonParent());
double distanceNext = deletedNodes.get(
deletedNodes.size() - 1).getParent().getMatchRatio(
nextResult.getLastCommonParent());
if (distancePrev <= distanceNext) {
// insert after the previous node
prevResult.setLastCommonParentDepth(prevResult
.getLastCommonParentDepth() + 1);
} else {
// insert before the next node
nextResult.setLastCommonParentDepth(nextResult
.getLastCommonParentDepth() + 1);
}
}
}
if (prevResult.getLastCommonParentDepth() > nextResult
.getLastCommonParentDepth()) {
// Inserting at the front
if(inlineResult) {
if (prevResult.isSplittingNeeded()) {
prevLeaf.getParent().splitUntill(
prevResult.getLastCommonParent(), prevLeaf, true);
}
prevLeaf = deletedNodes.remove(0).copyTree();
prevLeaf.setParent(prevResult.getLastCommonParent());
prevResult.getLastCommonParent().addChild(
prevResult.getIndexInLastCommonParent() + 1,
prevLeaf);
} else {
deletedNodes.remove(0);
}
} else if (prevResult.getLastCommonParentDepth() < nextResult
.getLastCommonParentDepth()) {
// Inserting at the back
if (nextResult.isSplittingNeeded()) {
boolean splitOccured = nextLeaf.getParent().splitUntill(
nextResult.getLastCommonParent(), nextLeaf, false);
if (splitOccured) {
// The place where to insert is shifted one place to the
// right
nextResult.setIndexInLastCommonParent(nextResult
.getIndexInLastCommonParent() + 1);
}
}
if(inlineResult) {
nextLeaf = deletedNodes.remove(deletedNodes.size() - 1)
.copyTree();
nextLeaf.setParent(nextResult.getLastCommonParent());
nextResult.getLastCommonParent().addChild(
nextResult.getIndexInLastCommonParent(), nextLeaf);
} else {
deletedNodes.remove(deletedNodes.size() - 1);
}