Program: 178465
Project: 178465
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();
        if (Node.TEXT_NODE == startContainer.getNodeType()) {
          Text textNode=Text.as(startContainer);
          if (pos == 0) {
            if (textNode.getParentElement().getPreviousSibling() != null) {
              op=new TreeMergeParagraph(clientJupiter.getSiteId(),path.get(0),1,1);
              op.setPath(TreeHelper.toIntArray(path));
            }
 else {
            }
          }
 else {
            pos=pos - 1;
            op=new TreeDeleteText(clientJupiter.getSiteId(),pos,TreeHelper.toIntArray(path));
          }
        }
 else         if (Node.ELEMENT_NODE == startContainer.getNodeType()) {
          if (pos == 0) {
            if (startContainer.getPreviousSibling() != null) {
              log.severe("String_Node_Str");
            }
 else {
              event.preventDefault();
            }
          }
        }
      }
    break;
case KeyCodes.KEY_DELETE:
{
    if (Node.TEXT_NODE == startContainer.getNodeType()) {
      Text textNode=Text.as(startContainer);
      pos=range.getStartOffset();
      if (textNode.getLength() == pos) {
        Element sibling=textNode.getParentElement().getNextSiblingElement();
        if ((sibling != null) && (!sibling.getClassName().toLowerCase().contains("String_Node_Str"))) {
          path.set(0,path.get(0) + 1);
          op=new TreeMergeParagraph(clientJupiter.getSiteId(),path.get(0),1,1);
          op.setPath(TreeHelper.toIntArray(path));
        }
      }
 else {
        op=new TreeDeleteText(clientJupiter.getSiteId(),pos,TreeHelper.toIntArray(path));
      }
    }
 else     if (Node.ELEMENT_NODE == startContainer.getNodeType()) {
      if (startContainer.getNextSibling() != null) {
        path.set(0,path.get(0) + 1);
        op=new TreeMergeParagraph(clientJupiter.getSiteId(),path.get(0),1,1);
        op.setPath(TreeHelper.toIntArray(path));
      }
    }
  }
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=textNode;
  while (!"String_Node_Str".equalsIgnoreCase(n.getParentNode().getNodeName())) {
    n=n.getParentNode();
  }
  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();
        if (Node.TEXT_NODE == startContainer.getNodeType()) {
          Text textNode=Text.as(startContainer);
          Node n=getAncestorBelowParagraph(textNode);
          Node rightParagraph=n.getParentElement();
          Node leftParagraph=rightParagraph.getPreviousSibling();
          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) {
                op=new TreeMergeParagraph(clientJupiter.getSiteId(),path.get(0),leftParagraph.getChildCount(),rightParagraph.getChildCount());
                op.setPath(TreeHelper.toIntArray(path));
              }
 else {
                log.severe("String_Node_Str");
              }
            }
 else {
              log.fine("String_Node_Str");
            }
          }
 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);
          if (pos == 0) {
            if (startContainer.getPreviousSibling() != null) {
              log.severe("String_Node_Str");
            }
 else {
              event.preventDefault();
            }
          }
        }
      }
    break;
case KeyCodes.KEY_DELETE:
{
    if (Node.TEXT_NODE == startContainer.getNodeType()) {
      Text textNode=Text.as(startContainer);
      pos=range.getStartOffset();
      if (textNode.getLength() == pos) {
        Element sibling=textNode.getParentElement().getNextSiblingElement();
        if ((sibling != null) && (!sibling.getClassName().toLowerCase().contains("String_Node_Str"))) {
          path.set(0,path.get(0) + 1);
          op=new TreeMergeParagraph(clientJupiter.getSiteId(),path.get(0),1,1);
          op.setPath(TreeHelper.toIntArray(path));
        }
      }
 else {
        op=new TreeDeleteText(clientJupiter.getSiteId(),pos,TreeHelper.toIntArray(path));
      }
    }
 else     if (Node.ELEMENT_NODE == startContainer.getNodeType()) {
      if (startContainer.getNextSibling() != null) {
        path.set(0,path.get(0) + 1);
        op=new TreeMergeParagraph(clientJupiter.getSiteId(),path.get(0),1,1);
        op.setPath(TreeHelper.toIntArray(path));
      }
    }
  }
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);
}
}
}