if (parentElement != null) {
OdfElement copyParentNode = (OdfElement) parentElement.cloneNode(true);
if (ownerDoc != parentElement.getOwnerDocument()) {
copyParentNode = (OdfElement) ownerDoc.adoptNode(copyParentNode);
}
OdfTextSpan textSpan = new OdfTextSpan(ownerDoc);
int sIndex = mIndexInContainer;
int eIndex = sIndex + mMatchedText.length();
// delete the content except the selection string
// delete from the end to start, so that the postion will not be
// impact by delete action
delete(eIndex, TextExtractor.getText(copyParentNode).length() - eIndex, copyParentNode);
delete(0, sIndex, copyParentNode);
optimize(copyParentNode);
Node childNode = copyParentNode.getFirstChild();
while (childNode != null) {
textSpan.appendChild(childNode.cloneNode(true));
childNode = childNode.getNextSibling();
}
// apply text style for the textSpan
if (copyParentNode instanceof OdfStylableElement) {
applyTextStyleProperties(getTextStylePropertiesDeep((OdfStylableElement) copyParentNode), textSpan);