if (target.isLeaf() && paragraph.getElementCount() > (index + 1))
Edit edit = getEditForParagraphAndIndex(paragraph, index);  if (paragraph.getStartOffset() > pos) { Element first = paragraph.getElement(0); Element newEl = createLeafElement(paragraph, first.getAttributes(), pos, first.getEndOffset()); edit.addAddedElement(newEl); edit.addRemovedElement(first); } else if (paragraph.getElementCount() > (index + 1) && (pos == target.getStartOffset() && !target.equals(lastFractured)))
    private void insertContentTag(ElementSpec tag)    {      int len = tag.getLength();      int dir = tag.getDirection();      AttributeSet tagAtts = tag.getAttributes();      if (dir == ElementSpec.JoinPreviousDirection)        {          // The mauve tests to this class show that a JoinPrevious insertion          // does not add any edits to the document event. To me this means          // that nothing is done here. The previous element naturally should          // expand so that it covers the new characters.        }      else if (dir == ElementSpec.JoinNextDirection)        {          // FIXME:          // Have to handle JoinNext differently depending on whether          // or not it comes after a fracture.  If comes after a fracture,           // the insertFracture method takes care of everything and nothing          // needs to be done here.  Otherwise, we need to adjust the          // Element structure.  For now, I check if the elementStack's           // top Element is the immediate parent of the LeafElement at          // offset - if so, we did not come immediately after a           // fracture.  This seems awkward and should probably be improved.          // We may be doing too much in insertFracture because we are           // adjusting the offsets, the correct thing to do may be to           // create a new branch element and push it on to element stack          // and then this method here can be more general.          BranchElement paragraph = (BranchElement) elementStack.peek();          int index = paragraph.getElementIndex(offset);          Element target = paragraph.getElement(index);          if (target.isLeaf() && paragraph.getElementCount() > (index + 1))            {              Element next = paragraph.getElement(index + 1);          Element newEl1 = createLeafElement(paragraph,                                                 target.getAttributes(),                                                 target.getStartOffset(),                                             offset);          Element newEl2 = createLeafElement(paragraph,                                                 next.getAttributes(), offset,                                           next.getEndOffset());              Edit edit = getEditForParagraphAndIndex(paragraph, index);              edit.addRemovedElement(target);              edit.addRemovedElement(next);              edit.addAddedElement (newEl1);              edit.addAddedElement (newEl2);        }        }      else if (dir == ElementSpec.OriginateDirection)        {          BranchElement paragraph = (BranchElement) elementStack.peek();          int index = paragraph.getElementIndex(offset);          Element current = paragraph.getElement(index);          Element[] added;          Element[] removed = new Element[] {current};          Element[] splitRes = split(current, offset, length);          if (splitRes[0] == null)            {              added = new Element[2];              added[0] = createLeafElement(paragraph, tagAtts,                                           offset, endOffset);              added[1] = splitRes[1];              removed = new Element[0];              index++;            }          else if (current.getStartOffset() == offset)            {              // This is if the new insertion happens immediately before               // the <code>current</code> Element.  In this case there are 2               // resulting Elements.                            added = new Element[2];              added[0] = createLeafElement(paragraph, tagAtts, offset,                                           endOffset);              added[1] = splitRes[1];            }          else if (current.getEndOffset() == endOffset)            {              // This is if the new insertion happens right at the end of               // the <code>current</code> Element.  In this case there are               // 2 resulting Elements.              added = new Element[2];              added[0] = splitRes[0];              added[1] = createLeafElement(paragraph, tagAtts, offset,                                           endOffset);            }          else            {              // This is if the new insertion is in the middle of the               // <code>current</code> Element.  In this case               // there will be 3 resulting Elements.              added = new Element[3];              added[0] = splitRes[0];              added[1] = createLeafElement(paragraph, tagAtts, offset,                                           endOffset);              added[2] = splitRes[1];            }          Edit edit = getEditForParagraphAndIndex(paragraph, index);          edit.addRemovedElements(removed);          edit.addAddedElements(added);        }      offset += len;    }