Program: 178442
Project: 178442
Buggy source:
/** 
 * The caret has to be deterministically positioned (within a paragraph), it is always located as follows: <ul> <li>at the last position of the previous non-empty text-node within the same paragraph if such a previous text-node exists.</li> <li>if not, at the first position of the next non-empty text-node relative to the locator within the same paragraph.</li> <li>if there are no non-empty text nodes (all text nodes are empty) then the caret moves to the first empty text node either at left or at right </ul>
 * @param oldCaretPosition the old caret position
 * @return the new caret position, as described above
 */
public static Range normalizeCaretPosition(Range oldCaretPosition){
  Range newCaretPos=null;
  if (oldCaretPosition.isCollapsed()) {
    newCaretPos=oldCaretPosition.cloneRange();
    if (isNonEmptyTextNode(oldCaretPosition.getStartContainer()) && oldCaretPosition.getStartOffset() > 0) {
      return oldCaretPosition;
    }
 else {
      Map<String,List<Text>> leftTextNodesInSameP=getLeftTextNodesInSameP(oldCaretPosition);
      List<Text> leftNonEmpty=leftTextNodesInSameP.get(NON__EMPTY);
      int leftSize=leftNonEmpty.size();
      if (leftSize > 0) {
        Text firstLeftTextNode=leftNonEmpty.get(leftSize - 1);
        newCaretPos.setStart(firstLeftTextNode,firstLeftTextNode.getLength());
      }
 else {
        Map<String,List<Text>> rightTextNodesInSameP=getRightTextNodesInSameP(oldCaretPosition);
        List<Text> rightNonEmpty=rightTextNodesInSameP.get(NON__EMPTY);
        if (rightNonEmpty.size() > 0) {
          Text firstRightTextNode=rightNonEmpty.get(0);
          newCaretPos.setStart(firstRightTextNode,0);
        }
 else {
          List<Text> emptyTextNodes=leftTextNodesInSameP.get(EMPTY);
          Text emptyText=null;
          if (emptyTextNodes.size() > 0) {
            emptyText=emptyTextNodes.get(0);
          }
 else {
            emptyTextNodes=rightTextNodesInSameP.get(EMPTY);
            if (emptyTextNodes.size() > 0) {
              emptyText=emptyTextNodes.get(0);
            }
          }
          if (emptyText != null) {
            newCaretPos.setStart(emptyText,0);
          }
        }
      }
    }
    newCaretPos.collapse(true);
  }
  return newCaretPos;
}

Fixed version (ground truth):
/** 
 * The caret has to be deterministically positioned (within a paragraph), it is always located as follows: <ul> <li>at the last position of the previous non-empty text-node within the same paragraph if such a previous text-node exists.</li> <li>if not, at the first position of the next non-empty text-node relative to the locator within the same paragraph.</li> <li>if there are no non-empty text nodes (all text nodes are empty) then the caret moves to the first empty text node either at left or at right </ul>
 * @param oldCaretPosition the old caret position
 * @return the new caret position, as described above
 */
public static Range normalizeCaretPosition(Range oldCaretPosition){
  Range newCaretPos=oldCaretPosition.cloneRange();
  if (oldCaretPosition.isCollapsed()) {
    if (isNonEmptyTextNode(oldCaretPosition.getStartContainer()) && oldCaretPosition.getStartOffset() > 0) {
      return oldCaretPosition;
    }
 else {
      Map<String,List<Text>> leftTextNodesInSameP=getLeftTextNodesInSameP(oldCaretPosition);
      List<Text> leftNonEmpty=leftTextNodesInSameP.get(NON__EMPTY);
      int leftSize=leftNonEmpty.size();
      if (leftSize > 0) {
        Text firstLeftTextNode=leftNonEmpty.get(leftSize - 1);
        newCaretPos.setStart(firstLeftTextNode,firstLeftTextNode.getLength());
      }
 else {
        Map<String,List<Text>> rightTextNodesInSameP=getRightTextNodesInSameP(oldCaretPosition);
        List<Text> rightNonEmpty=rightTextNodesInSameP.get(NON__EMPTY);
        if (rightNonEmpty.size() > 0) {
          Text firstRightTextNode=rightNonEmpty.get(0);
          newCaretPos.setStart(firstRightTextNode,0);
        }
 else {
          List<Text> emptyTextNodes=leftTextNodesInSameP.get(EMPTY);
          Text emptyText=null;
          if (emptyTextNodes.size() > 0) {
            emptyText=emptyTextNodes.get(0);
          }
 else {
            emptyTextNodes=rightTextNodesInSameP.get(EMPTY);
            if (emptyTextNodes.size() > 0) {
              emptyText=emptyTextNodes.get(0);
            }
          }
          if (emptyText != null) {
            newCaretPos.setStart(emptyText,0);
          }
        }
      }
    }
    newCaretPos.collapse(true);
  }
  return newCaretPos;
}