Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIA can't get compareendpoints to work between text selection and documentrange in internet explorer

Main problem: Can't get CompareEndpoints to give any value other than "1" when comparing the textrange of the selected text with the documentrange on the current site (displayed in IE).

//Initialize range variables
IUIAutomationTextRange* documentRange = NULL;
IUIAutomationTextRange* selectionRange = NULL;
IUIAutomationTextRangeArray* selectionRangeArray = NULL;

//Get entire text document range
m_pTextPattern->get_DocumentRange(&documentRange);

//Get selection range
m_pTextPattern->GetSelection(&selectionRangeArray);
selectionRangeArray->GetElement(0, &selectionRange);

The ranges are valid, and the selected text is inside the document range. When we try to get the number of moves/characters the selected text is from the start of the document/site-start, then we only get return value of 1.

selectionRange->CompareEndpoints(
   TextPatternRangeEndpoint::TextPatternRangeEndpoint_Start, 
   documentRange, 
   TextPatternRangeEndpoint::TextPatternRangeEndpoint_Start,
   &rv);

Ex. the site: http://www.cplusplus.com/reference/string/string/

We retrieve the textpattern from the node with name "string - C++ Reference". Then we get the document range of the entire document "documentRange" and select some text with the mouse and saves that range to selectionRange ex. "objects that represent" (selection of text from site... line 3 under std::string).

We have tried the same for a notepad window, where compareendpoints returned a valid/correct distance between the points textranges.

Example:

if (SUCCEEDED(hr))
    {
        IUIAutomationTextRange* documentRange = NULL;
        IUIAutomationTextRangeArray* selectionRangeArray = NULL;
        IUIAutomationTextRange* selectionRange = NULL;
        hr = E_FAIL;

        hr = m_pTextPattern->get_DocumentRange(&documentRange);
        if (SUCCEEDED(hr) && documentRange != NULL)
        {
            hr = m_pTextPattern->GetSelection(&selectionRangeArray);
            if (SUCCEEDED(hr) && selectionRangeArray != NULL)
            {
                int length;
                hr = selectionRangeArray->get_Length(&length);
                if (SUCCEEDED(hr) && length > 0)
                {
                    hr = selectionRangeArray->GetElement(0, &selectionRange);
                    if (SUCCEEDED(hr) && selectionRange != NULL)
                    {
                        hr =  selectionRange->CompareEndpoints(TextPatternRangeEndpoint::TextPatternRangeEndpoint_Start, 
                            documentRange, TextPatternRangeEndpoint::TextPatternRangeEndpoint_Start, &rv);
                        wprintf(L"getSelectionStart rv: %d\n", rv);         
                    }
                }
            }
        }
        if (documentRange != NULL)
        {
            documentRange->Release();
            documentRange = NULL;
        }
        if (selectionRangeArray != NULL)
        {
            selectionRangeArray->Release();
            selectionRangeArray = NULL;
        }
        if (selectionRange != NULL)
        {
            selectionRange->Release();
            selectionRange = NULL;
        }

    }
}
like image 220
Jaaxe Avatar asked Jun 15 '16 13:06

Jaaxe


1 Answers

The docs state that a negative, positive, or zero value is returned. It does not return a distance necessarily.

like image 52
Kevin Smyth Avatar answered Sep 25 '22 05:09

Kevin Smyth