// TODO(danilatos): Clarify and possibly reorganise this loop
// TODO(danilatos): Write more unit tests to thoroughly cover all scenarios
if (possibleOwnerNode == null) {
// Scenario (D)
throw new HtmlInserted(
Point.inElement(parentElement, (ContentNode) null),
Point.start(filteredHtml, parentNodelet)
);
}
ContentTextNode possibleOwner;
try {
possibleOwner = (ContentTextNode) possibleOwnerNode;
} catch (ClassCastException e) {
if (possibleOwnerNode.isImplAttached()) {
// Scenario (C)
throw new HtmlInserted(
Point.inElement(parentElement, possibleOwnerNode),
Point.inElementReverse(filteredHtml, parentNodelet, nodeletBeforeTextNodes)
);
} else {
// Scenario (A)
// Not minor, an element has gone missing
throw new HtmlMissing(possibleOwnerNode, parentNodelet);
}
}
ContentNode nextNode = renderedContent.getNextSibling(possibleOwner);
if (nextNode != null && !nextNode.isImplAttached()) {
// Scenario (E)
throw new HtmlMissing(nextNode, parentNodelet);
}
if (current != possibleOwner.getImplNodelet()) {
// Scenario (B)
if (attemptRepair) {
possibleOwner.setTextNodelet(current);
return nullifyIfWrongDocument(possibleOwner);
} else {
// TODO(danilatos): Ensure repairs handle nodes on either
// side, as this is kind of a "replace" error
throw new HtmlInserted(
Point.inElement(parentElement, possibleOwner),
Point.inElement(parentNodelet, current));
}
}