日期:2014-05-16 浏览次数:20978 次
/*程序中的源码*/
HRESULT CContentBehavior::InternalFindTextImpl(CComBSTR bstrText, DWORD dwFlag, BOOL bSelected, DWORD *dwRet, BOOL bReplaceFind)
{
if( dwRet)
*dwRet =0;
CStringW strText(bstrText);
BOOL bBlankExists= strText.Find(L' ') >=0;
FXE::CXMLPointerUtil tools(this);
CComPtr<IMarkupPointer> pStart, pEnd, pEndConst;
tools.GetSelectedPointer(&pStart, &pEnd, NULL);
if ( (pStart == NULL) || (pEnd == NULL))
return E_FAIL;
BOOL bAreEqual = FALSE;
pStart->IsEqualTo(pEnd, &bAreEqual);
CComPtr<IHTMLElement> pElem;
GetContentElement(&pElem);
ASSERT(pElem);
long nStep = 1L;
MARKUP_CONTEXT_TYPE type = CONTEXT_TYPE_Text;
long nCount = bstrText.Length();
DWORD dwFlagTmp = dwFlag;
if( dwFlag & FR_DOWN)
{
pEnd->MoveAdjacentToElement(pElem, ELEM_ADJ_BeforeEnd );
if( dwFlag & FR_LOOPFIND)
pStart->MoveAdjacentToElement(pElem, ELEM_ADJ_AfterBegin);
if((!bAreEqual)&&(!bReplaceFind))
pStart->Right(TRUE, &type, NULL, &nCount, NULL);
}
else
{ //替换的时候,调用查找函数,是永远不会进入该else的。
pStart->MoveAdjacentToElement(pElem, ELEM_ADJ_AfterBegin);
if( dwFlag & FR_LOOPFIND)
pEnd->MoveAdjacentToElement(pElem, ELEM_ADJ_BeforeEnd );
if((!bAreEqual)&&(!bReplaceFind))
pEnd->Left(TRUE, &type, NULL, &nCount, NULL);
nCount = -nCount;
}
dwFlag &= (~FR_DOWN);
dwFlag &= (~FR_LOOPFIND);
CComPtr<IHTMLDocument2> spDoc;
GetHTMLDocument(&spDoc);
if( spDoc == NULL)
return E_FAIL;
CComPtr<IHTMLElement> spBodyElement;
spDoc->get_body(&spBodyElement);
ASSERT(spBodyElement);
CComQIPtr<IHTMLBodyElement> spBody(spBodyElement);
CComPtr<IHTMLTxtRange> spRange;
spBody->createTextRange(&spRange);
ASSERT(spRange);
//选中范围
CComQIPtr<IMarkupServices> pServices(spDoc);
pServices->CreateMarkupPointer(&pEndConst);
pEndConst->MoveToPointer(pEnd);
pServices->MoveRangeToPointers(pStart,pEnd,spRange);
ASSERT(spRange);
{
VARIANT_BOOL bRet = VARIANT_FALSE;
HRESULT hr = spRange->findText( bstrText, nCount, dwFlag, &bRet); //没有查找到并不代表spRange中真的没有bstrText这个字符串
CComBSTR bstrTextR;
spRange->get_text(&bstrTextR);
CStringW strTextR(bstrTextR);
CStringW strSearch(bstrText);
strTextR.Replace(L"\r\n",L"\r");//避免换行导致的误差
int n = -1;
if (!(dwFlag & FR_MATCHCASE))
{
strTextR.MakeLower();
strSearch.MakeLower();
}
if (dwFlagTmp & FR_DOWN)
{
n = strTextR.Find(strSearch);
}
else
{
std::wstring stdstrTextR = strTextR.GetBuffer(0);
n = stdstrTextR.rfind(strSearch.GetBuffer(0));<