question: 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 ; }
target:  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.