frac = frac.getElement(frac.getElementIndex(offset));
frac = frac.getElement(frac.getElementIndex(pos));
    private void insertFracture(ElementSpec tag)    {      // insert the fracture at offset.      BranchElement parent = (BranchElement) elementStack.peek();      int parentIndex = parent.getElementIndex(offset);      AttributeSet parentAtts = parent.getAttributes();      Element toFracture = parent.getElement(parentIndex);      int parSize = parent.getElementCount();      Edit edit = getEditForParagraphAndIndex(parent, parentIndex);      Element frac = toFracture;      int leftIns = 0;      int indexOfFrac = toFracture.getElementIndex(offset);      int size = toFracture.getElementCount();      // gets the leaf that falls along the fracture      frac = toFracture.getElement(indexOfFrac);      while (!frac.isLeaf())        frac = frac.getElement(frac.getElementIndex(offset));      AttributeSet atts = frac.getAttributes();      int fracStart = frac.getStartOffset();      int fracEnd = frac.getEndOffset();      if (offset > fracStart && offset < fracEnd)        {          // recreate left-side of branch and all its children before offset          // add the fractured leaves to the right branch          BranchElement rightBranch =            (BranchElement) createBranchElement(parent, parentAtts);                    // Check if left branch has already been edited. If so, we only          // need to create the right branch.          BranchElement leftBranch = null;          Element[] added = null;          if (edit.added.size() > 0 || edit.removed.size() > 0)            {              added = new Element[] { rightBranch };                            // don't try to remove left part of tree              parentIndex++;            }          else            {              leftBranch =                (BranchElement) createBranchElement(parent, parentAtts);              added = new Element[] { leftBranch, rightBranch };              // add fracture to leftBranch              Element leftFracturedLeaf = createLeafElement(leftBranch, atts,                                                            fracStart, offset);              leftBranch.replace(leftIns, 0, new Element[] { leftFracturedLeaf });            }                    if (!toFracture.isLeaf())            {              // add all non-fracture elements to the branches              if (indexOfFrac > 0 && leftBranch != null)                {                  Element[] add = new Element[indexOfFrac];                  for (int i = 0; i < indexOfFrac; i++)                    add[i] = toFracture.getElement(i);                  leftIns = add.length;                  leftBranch.replace(0, 0, add);                }              int count = size - indexOfFrac - 1;              if (count > 0)                {                  Element[] add = new Element[count];                  int j = 0;                  int i = indexOfFrac + 1;                  while (j < count)                    add[j++] = toFracture.getElement(i++);                  rightBranch.replace(0, 0, add);                }            }                    // add to fracture to rightBranch                    // Check if we can join the right frac leaf with the next leaf          int rm = 0;          int end = fracEnd;          Element next = rightBranch.getElement(0);          if (next != null && next.isLeaf()              && next.getAttributes().isEqual(atts))            {              end = next.getEndOffset();              rm = 1;            }          Element rightFracturedLeaf = createLeafElement(rightBranch, atts,                                                         offset, end);          rightBranch.replace(0, rm, new Element[] { rightFracturedLeaf });          // recreate those elements after parentIndex and add/remove all          // new/old elements to parent          int remove = parSize - parentIndex;          Element[] removed = new Element[0];          Element[] added2 = new Element[0];          if (remove > 0)            {              removed = new Element[remove];              int s = 0;              for (int j = parentIndex; j < parSize; j++)                removed[s++] = parent.getElement(j);          edit.addRemovedElements(removed);              added2 = recreateAfterFracture(removed, parent, 1,                                            rightBranch.getEndOffset());            }                    edit.addAddedElements(added);          edit.addAddedElements(added2);          elementStack.push(rightBranch);          lastFractured = rightFracturedLeaf;        }      else        fracNotCreated = true;    }