private boolean canModifyPrev(Node node){
  boolean textLastChild=false;
  Node prev=node.getPreviousSibling();
  while (prev != null) {
    short type=prev.getNodeType();
    if (type == Node.ENTITY_REFERENCE_NODE) {
      Node lastChild=prev.getLastChild();
      if (lastChild == null) {
        return false;
      }
      while (lastChild != null) {
        short lType=lastChild.getNodeType();
        if (lType == Node.TEXT_NODE || lType == Node.CDATA_SECTION_NODE) {
          textLastChild=true;
        }
 else         if (lType == Node.ENTITY_REFERENCE_NODE) {
          if (!canModifyPrev(lastChild)) {
            return false;
          }
 else {
            textLastChild=true;
          }
        }
 else {
          return !textLastChild;
        }
        lastChild=lastChild.getPreviousSibling();
      }
    }
 else     if (type == Node.TEXT_NODE || type == Node.CDATA_SECTION_NODE) {
    }
 else {
      return true;
    }
    prev=prev.getPreviousSibling();
  }
  return true;
}

private boolean canModifyPrev ( Node node ) { boolean textLastChild = false ; Node prev = node . getPreviousSibling ( ) ; while ( prev != null ) { short type = prev . getNodeType ( ) ; if ( type == Node . ENTITY_REFERENCE_NODE ) { Node lastChild = prev . getLastChild ( ) ; if ( lastChild == null ) { return false ; } while ( lastChild != null ) { short lType = lastChild . getNodeType ( ) ; if ( lType == Node . TEXT_NODE || lType == Node . CDATA_SECTION_NODE ) { textLastChild = true ; } else if ( lType == Node . ENTITY_REFERENCE_NODE ) { if ( ! canModifyPrev ( lastChild ) ) { return false ; } else { textLastChild = true ; } } else { return ! textLastChild ; } lastChild = lastChild . getPreviousSibling ( ) ; } } else if ( type == Node . TEXT_NODE || type == Node . CDATA_SECTION_NODE ) { } else { return true ; } prev = prev . getPreviousSibling ( ) ; } return true ; }
if any entityreference to be removed has descendants that are not entityreference , text , or cdatasection nodes , the replacewholetext method must fail before performing any modification of the document , raising a domexception with the code no_modification_allowed_err . traverse previous siblings of the node to be replaced . if a previous sibling is an entityreference node , get it ' s last child . if the last child was a text or cdatasection node and its previous siblings are neither a replaceable entityreference or text or cdatasection nodes , return false . if the last child was neither text nor cdatasection nor a replaceable entityreference node , then return true . if the last child was a text or cdatasection node any its previous sibling was not or was an entityreference that did not contain only text or cdatasection nodes , return false . check this recursively for entityreference nodes .
