Element nextLeaf = next.getElement(0); Edit e = getEditForParagraphAndIndex(next, 0); Element newEl2 = createLeafElement(next, atts, pos, nextLeaf.getEndOffset()); e.addAddedElement(newEl2); e.addRemovedElement(nextLeaf);
if (p != insertPath.length - 1) first = edit.e.getElement(edit.index); break; }
    private void insertContentTag(ElementSpec tag)    {      BranchElement paragraph = (BranchElement) elementStack.peek();      int len = tag.getLength();      int dir = tag.getDirection();      AttributeSet tagAtts = tag.getAttributes();            if (dir == ElementSpec.JoinNextDirection)        {          int index = paragraph.getElementIndex(pos);          Element target = paragraph.getElement(index);          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)))            {              Element next = paragraph.getElement(index + 1);              Element newEl = createLeafElement(paragraph,                                                next.getAttributes(), pos,                                                next.getEndOffset());              edit.addAddedElement(newEl);              edit.addRemovedElement(next);              edit.addRemovedElement(target);            }          else            {              BranchElement parent = (BranchElement) paragraph.getParentElement();              int i = parent.getElementIndex(pos);              BranchElement next = (BranchElement) parent.getElement(i + 1);              AttributeSet atts = tag.getAttributes();                    if (next != null)                {                  Element nextLeaf = next.getElement(0);                  Edit e = getEditForParagraphAndIndex(next, 0);                     Element newEl2 = createLeafElement(next, atts, pos, nextLeaf.getEndOffset());                  e.addAddedElement(newEl2);                  e.addRemovedElement(nextLeaf);                }            }        }      else         {          int end = pos + len;          Element leaf = createLeafElement(paragraph, tag.getAttributes(), pos, end);                // Check for overlap with other leaves/branches          if (paragraph.getElementCount() > 0)            {              int index = paragraph.getElementIndex(pos);              Element target = paragraph.getElement(index);              boolean onlyContent = target.isLeaf();                    BranchElement toRec = paragraph;              if (!onlyContent)                toRec = (BranchElement) target;                    // Check if we should place the leaf before or after target              if (pos > target.getStartOffset())                index++;              Edit edit = getEditForParagraphAndIndex(paragraph, index);              edit.addAddedElement(leaf);            }          else            paragraph.replace(0, 0, new Element[] { leaf });        }                                  pos += len;    }