Program: 178459
Project: 178459
Buggy source:
@Override public void onKeyDown(KeyDownEvent event){
  final int keyCode=event.getNativeKeyCode();
  Selection selection=getTextArea().getDocument().getSelection();
  if (selection.getRangeCount() > 0) {
    Range range=selection.getRangeAt(0);
    logRange(range);
    int pos=-1;
    Node startContainer=range.getStartContainer();
    Node endContainer=range.getEndContainer();
    List<Integer> path=TreeHelper.getLocator(range.getStartContainer());
    TreeOperation op=null;
switch (keyCode) {
case KeyCodes.KEY_BACKSPACE:
{
        pos=range.getStartOffset();
        Node n;
        Element rightParagraph;
        Node leftParagraph;
        if (Node.TEXT_NODE == startContainer.getNodeType()) {
          n=getAncestorBelowParagraph(startContainer);
          rightParagraph=n.getParentElement();
          leftParagraph=rightParagraph.getPreviousSibling();
          Text textNode=Text.as(startContainer);
          if (pos == 0) {
            if (leftParagraph != null) {
              boolean merge=false;
              if (textNode != n && textNode == n.getFirstChild()) {
                merge=true;
              }
              if (textNode == n && n == rightParagraph.getFirstChild()) {
                merge=true;
              }
              if (merge) {
                int brCount=rightParagraph.getElementsByTagName(BR).getLength();
                op=new TreeMergeParagraph(clientJupiter.getSiteId(),path.get(0),leftParagraph.getChildCount(),rightParagraph.getChildCount() - brCount);
                op.setPath(TreeHelper.toIntArray(path));
              }
 else {
                op=skipBackspaceOnEmptyTexts(textNode,path,rightParagraph,leftParagraph);
              }
            }
 else {
              log.fine("String_Node_Str");
              event.preventDefault();
            }
          }
 else {
            pos=pos - 1;
            op=new TreeDeleteText(clientJupiter.getSiteId(),pos,TreeHelper.toIntArray(path));
          }
        }
 else         if (Node.ELEMENT_NODE == startContainer.getNodeType()) {
          Element element=Element.as(startContainer);
          boolean merge=false;
          if ("String_Node_Str".equalsIgnoreCase(element.getNodeName())) {
            rightParagraph=element;
            leftParagraph=rightParagraph.getPreviousSibling();
          }
 else {
            n=getAncestorBelowParagraph(startContainer);
            rightParagraph=n.getParentElement();
            leftParagraph=rightParagraph.getPreviousSibling();
            if (pos == 0 && "String_Node_Str".equalsIgnoreCase(element.getNodeName())) {
              merge=(element == n && n == rightParagraph.getFirstChild());
            }
          }
          merge=merge && (leftParagraph != null);
          if (merge) {
            int brCount=rightParagraph.getElementsByTagName(BR).getLength();
            op=new TreeMergeParagraph(clientJupiter.getSiteId(),path.get(0),leftParagraph.getChildCount(),rightParagraph.getChildCount() - brCount);
            op.setPath(TreeHelper.toIntArray(path));
          }
 else {
            log.severe("String_Node_Str" + element.getTagName() + "String_Node_Str");
            event.preventDefault();
          }
        }
      }
    break;
case KeyCodes.KEY_DELETE:
{
    Element leftParagraph;
    Element rightParagraph;
    if (Node.TEXT_NODE == startContainer.getNodeType()) {
      Text textNode=Text.as(startContainer);
      leftParagraph=getAncestorBelowParagraph(startContainer).getParentElement();
      rightParagraph=leftParagraph.getNextSiblingElement();
      pos=range.getStartOffset();
      if (textNode.getLength() == pos) {
        if ((rightParagraph != null) && (!rightParagraph.getClassName().toLowerCase().contains("String_Node_Str"))) {
          int brCount=rightParagraph.getElementsByTagName(BR).getLength();
          path.set(0,path.get(0) + 1);
          op=new TreeMergeParagraph(clientJupiter.getSiteId(),path.get(0),leftParagraph.getChildCount(),rightParagraph.getChildCount() - brCount);
          op.setPath(TreeHelper.toIntArray(path));
        }
 else {
          log.fine("String_Node_Str");
          event.preventDefault();
        }
      }
 else {
        op=new TreeDeleteText(clientJupiter.getSiteId(),pos,TreeHelper.toIntArray(path));
      }
    }
 else     if (Node.ELEMENT_NODE == startContainer.getNodeType()) {
      Element element=Element.as(startContainer);
      if ("String_Node_Str".equalsIgnoreCase(element.getNodeName())) {
        leftParagraph=element;
      }
 else {
        leftParagraph=getAncestorBelowParagraph(element).getParentElement();
      }
      rightParagraph=leftParagraph.getNextSiblingElement();
      if (rightParagraph != null) {
        int brCount=rightParagraph.getElementsByTagName(BR).getLength();
        path.set(0,path.get(0) + 1);
        op=new TreeMergeParagraph(clientJupiter.getSiteId(),path.get(0),leftParagraph.getChildCount(),rightParagraph.getChildCount() - brCount);
        op.setPath(TreeHelper.toIntArray(path));
      }
 else {
        log.fine("String_Node_Str");
        event.preventDefault();
      }
    }
  }
break;
case KeyCodes.KEY_ENTER:
{
path=TreeHelper.getLocator(range.getEndContainer());
pos=range.getEndOffset();
if (Node.TEXT_NODE == endContainer.getNodeType()) {
  Text textNode=Text.as(endContainer);
  boolean isNewParagraph=false;
  Node n=getAncestorBelowParagraph(textNode);
  if (n.getPreviousSibling() == null && 0 == pos) {
    isNewParagraph=true;
    pos=path.get(0);
  }
  if ((n.getNextSibling() == null || BR.equalsIgnoreCase(n.getNextSibling().getNodeName())) && textNode.getLength() == pos) {
    isNewParagraph=true;
    pos=path.get(0) + 1;
  }
  if (isNewParagraph) {
    op=new TreeNewParagraph(clientJupiter.getSiteId(),pos);
    op.setPath(TreeHelper.toIntArray(path));
  }
 else {
    op=new TreeInsertParagraph(clientJupiter.getSiteId(),pos,TreeHelper.toIntArray(path));
  }
}
 else if (Node.ELEMENT_NODE == endContainer.getNodeType()) {
  Element element=Element.as(endContainer);
  if (element.getPreviousSibling() == null && 0 == pos) {
    op=new TreeNewParagraph(clientJupiter.getSiteId(),path.get(0));
    op.setPath(TreeHelper.toIntArray(path));
  }
 else {
    int brCount=element.getElementsByTagName(BR).getLength();
    int childCount=element.getChildCount();
    boolean isBeforeLastBrTag=((pos == (childCount - brCount)) && (BR.equalsIgnoreCase(element.getLastChild().getNodeName())));
    boolean isAfterLastTag=(pos == childCount);
    if (isBeforeLastBrTag || isAfterLastTag) {
      pos=path.get(0) + 1;
      op=new TreeNewParagraph(clientJupiter.getSiteId(),pos);
      op.setPath(TreeHelper.toIntArray(path));
    }
 else {
      pos=range.getEndOffset();
      op=new TreeInsertParagraph(clientJupiter.getSiteId(),pos,TreeHelper.toIntArray(path));
    }
  }
}
}
break;
default :
break;
}
if (op != null) {
if (!(op instanceof TreeInsertText || op instanceof TreeDeleteText)) {
event.preventDefault();
}
clientJupiter.generate(op);
}
}
}

Fixed version (ground truth):
@Override public void onKeyDown(KeyDownEvent event){
  final int keyCode=event.getNativeKeyCode();
  Selection selection=getTextArea().getDocument().getSelection();
  if (selection.getRangeCount() > 0) {
    Range range=selection.getRangeAt(0);
    logRange(range);
    int pos=-1;
    Node startContainer=range.getStartContainer();
    Node endContainer=range.getEndContainer();
    List<Integer> path=TreeHelper.getLocator(range.getStartContainer());
    TreeOperation op=null;
switch (keyCode) {
case KeyCodes.KEY_BACKSPACE:
{
        pos=range.getStartOffset();
        Node n;
        Element rightParagraph;
        Node leftParagraph;
        if (Node.TEXT_NODE == startContainer.getNodeType()) {
          n=getAncestorBelowParagraph(startContainer);
          rightParagraph=n.getParentElement();
          leftParagraph=rightParagraph.getPreviousSibling();
          Text textNode=Text.as(startContainer);
          if (pos == 0) {
            if (leftParagraph != null) {
              boolean merge=false;
              if (textNode != n && textNode == n.getFirstChild()) {
                merge=true;
              }
              if (textNode == n && n == rightParagraph.getFirstChild()) {
                merge=true;
              }
              if (merge) {
                int brCount=rightParagraph.getElementsByTagName(BR).getLength();
                op=new TreeMergeParagraph(clientJupiter.getSiteId(),path.get(0),leftParagraph.getChildCount(),rightParagraph.getChildCount() - brCount);
                op.setPath(TreeHelper.toIntArray(path));
              }
 else {
                op=skipBackspaceOnEmptyTexts(textNode,path,rightParagraph,leftParagraph);
              }
            }
 else {
              log.fine("String_Node_Str");
              event.preventDefault();
            }
          }
 else {
            pos=pos - 1;
            op=new TreeDeleteText(clientJupiter.getSiteId(),pos,TreeHelper.toIntArray(path));
          }
        }
 else         if (Node.ELEMENT_NODE == startContainer.getNodeType()) {
          Element element=Element.as(startContainer);
          boolean merge=false;
          if ("String_Node_Str".equalsIgnoreCase(element.getNodeName())) {
            rightParagraph=element;
            merge=true;
          }
 else {
            n=getAncestorBelowParagraph(startContainer);
            rightParagraph=n.getParentElement();
            if (pos == 0 && "String_Node_Str".equalsIgnoreCase(element.getNodeName())) {
              merge=(element == n && n == rightParagraph.getFirstChild());
            }
          }
          leftParagraph=rightParagraph.getPreviousSibling();
          merge=merge && (leftParagraph != null);
          if (merge) {
            int brCount=rightParagraph.getElementsByTagName(BR).getLength();
            op=new TreeMergeParagraph(clientJupiter.getSiteId(),path.get(0),leftParagraph.getChildCount(),rightParagraph.getChildCount() - brCount);
            op.setPath(TreeHelper.toIntArray(path));
          }
 else {
            log.severe("String_Node_Str" + element.getTagName() + "String_Node_Str");
            event.preventDefault();
          }
        }
      }
    break;
case KeyCodes.KEY_DELETE:
{
    Element leftParagraph;
    Element rightParagraph;
    if (Node.TEXT_NODE == startContainer.getNodeType()) {
      Text textNode=Text.as(startContainer);
      leftParagraph=getAncestorBelowParagraph(startContainer).getParentElement();
      rightParagraph=leftParagraph.getNextSiblingElement();
      pos=range.getStartOffset();
      if (textNode.getLength() == pos) {
        if ((rightParagraph != null) && (!rightParagraph.getClassName().toLowerCase().contains("String_Node_Str"))) {
          int brCount=rightParagraph.getElementsByTagName(BR).getLength();
          path.set(0,path.get(0) + 1);
          op=new TreeMergeParagraph(clientJupiter.getSiteId(),path.get(0),leftParagraph.getChildCount(),rightParagraph.getChildCount() - brCount);
          op.setPath(TreeHelper.toIntArray(path));
        }
 else {
          log.fine("String_Node_Str");
          event.preventDefault();
        }
      }
 else {
        op=new TreeDeleteText(clientJupiter.getSiteId(),pos,TreeHelper.toIntArray(path));
      }
    }
 else     if (Node.ELEMENT_NODE == startContainer.getNodeType()) {
      Element element=Element.as(startContainer);
      if ("String_Node_Str".equalsIgnoreCase(element.getNodeName())) {
        leftParagraph=element;
      }
 else {
        leftParagraph=getAncestorBelowParagraph(element).getParentElement();
      }
      rightParagraph=leftParagraph.getNextSiblingElement();
      if (rightParagraph != null) {
        int brCount=rightParagraph.getElementsByTagName(BR).getLength();
        path.set(0,path.get(0) + 1);
        op=new TreeMergeParagraph(clientJupiter.getSiteId(),path.get(0),leftParagraph.getChildCount(),rightParagraph.getChildCount() - brCount);
        op.setPath(TreeHelper.toIntArray(path));
      }
 else {
        log.fine("String_Node_Str");
        event.preventDefault();
      }
    }
  }
break;
case KeyCodes.KEY_ENTER:
{
path=TreeHelper.getLocator(range.getEndContainer());
pos=range.getEndOffset();
if (Node.TEXT_NODE == endContainer.getNodeType()) {
  Text textNode=Text.as(endContainer);
  boolean isNewParagraph=false;
  Node n=getAncestorBelowParagraph(textNode);
  if (n.getPreviousSibling() == null && 0 == pos) {
    isNewParagraph=true;
    pos=path.get(0);
  }
  if ((n.getNextSibling() == null || BR.equalsIgnoreCase(n.getNextSibling().getNodeName())) && textNode.getLength() == pos) {
    isNewParagraph=true;
    pos=path.get(0) + 1;
  }
  if (isNewParagraph) {
    op=new TreeNewParagraph(clientJupiter.getSiteId(),pos);
    op.setPath(TreeHelper.toIntArray(path));
  }
 else {
    op=new TreeInsertParagraph(clientJupiter.getSiteId(),pos,TreeHelper.toIntArray(path));
  }
}
 else if (Node.ELEMENT_NODE == endContainer.getNodeType()) {
  Element element=Element.as(endContainer);
  if (element.getPreviousSibling() == null && 0 == pos) {
    op=new TreeNewParagraph(clientJupiter.getSiteId(),path.get(0));
    op.setPath(TreeHelper.toIntArray(path));
  }
 else {
    int brCount=element.getElementsByTagName(BR).getLength();
    int childCount=element.getChildCount();
    boolean isBeforeLastBrTag=((pos == (childCount - brCount)) && (BR.equalsIgnoreCase(element.getLastChild().getNodeName())));
    boolean isAfterLastTag=(pos == childCount);
    if (isBeforeLastBrTag || isAfterLastTag) {
      pos=path.get(0) + 1;
      op=new TreeNewParagraph(clientJupiter.getSiteId(),pos);
      op.setPath(TreeHelper.toIntArray(path));
    }
 else {
      pos=range.getEndOffset();
      op=new TreeInsertParagraph(clientJupiter.getSiteId(),pos,TreeHelper.toIntArray(path));
    }
  }
}
}
break;
default :
break;
}
if (op != null) {
if (!(op instanceof TreeInsertText || op instanceof TreeDeleteText)) {
event.preventDefault();
}
clientJupiter.generate(op);
}
}
}