Bug 757771 part 4 - Create new nsContentUtils::GetSelectionInTextControl method; r=ehsan

This commit is contained in:
Aryeh Gregor
2012-06-07 18:00:58 +03:00
parent aa34bbffa1
commit b3ad97272c
11 changed files with 157 additions and 228 deletions

View File

@@ -817,6 +817,15 @@ nsTextControlFrame::ScrollSelectionIntoView()
return NS_ERROR_FAILURE;
}
mozilla::dom::Element*
nsTextControlFrame::GetRootNodeAndInitializeEditor()
{
nsCOMPtr<nsIDOMElement> root;
GetRootNodeAndInitializeEditor(getter_AddRefs(root));
nsCOMPtr<mozilla::dom::Element> rootElem = do_QueryInterface(root);
return rootElem;
}
nsresult
nsTextControlFrame::GetRootNodeAndInitializeEditor(nsIDOMElement **aRootElement)
{
@@ -964,58 +973,6 @@ nsTextControlFrame::SetSelectionEnd(PRInt32 aSelectionEnd)
return SetSelectionEndPoints(selStart, selEnd);
}
nsresult
nsTextControlFrame::DOMPointToOffset(nsIDOMNode* aNode,
PRInt32 aNodeOffset,
PRInt32* aResult)
{
NS_ENSURE_ARG_POINTER(aNode && aResult);
*aResult = 0;
nsCOMPtr<nsIDOMElement> rootElement;
nsresult rv = GetRootNodeAndInitializeEditor(getter_AddRefs(rootElement));
nsCOMPtr<nsIDOMNode> rootNode(do_QueryInterface(rootElement));
NS_ENSURE_TRUE(rootNode, NS_ERROR_FAILURE);
nsCOMPtr<nsIDOMNodeList> nodeList;
rv = rootNode->GetChildNodes(getter_AddRefs(nodeList));
NS_ENSURE_SUCCESS(rv, rv);
NS_ENSURE_TRUE(nodeList, NS_ERROR_FAILURE);
PRUint32 length = 0;
rv = nodeList->GetLength(&length);
NS_ENSURE_SUCCESS(rv, rv);
if (!length || aNodeOffset < 0)
return NS_OK;
NS_ASSERTION(length <= 2, "We should have one text node and one mozBR at most");
nsCOMPtr<nsIDOMNode> firstNode;
rv = nodeList->Item(0, getter_AddRefs(firstNode));
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIDOMText> textNode = do_QueryInterface(firstNode);
nsCOMPtr<nsIDOMText> nodeAsText = do_QueryInterface(aNode);
if (nodeAsText || (aNode == rootNode && aNodeOffset == 0)) {
// Selection is somewhere inside the text node; the offset is aNodeOffset
*aResult = aNodeOffset;
} else {
// Selection is on the mozBR node, so offset should be set to the length
// of the text node.
if (textNode) {
rv = textNode->GetLength(&length);
NS_ENSURE_SUCCESS(rv, rv);
*aResult = PRInt32(length);
}
}
return NS_OK;
}
nsresult
nsTextControlFrame::OffsetToDOMPoint(PRInt32 aOffset,
nsIDOMNode** aResult,
@@ -1102,26 +1059,24 @@ nsTextControlFrame::GetSelectionRange(PRInt32* aSelectionStart,
rv = selCon->GetSelection(nsISelectionController::SELECTION_NORMAL, getter_AddRefs(selection));
NS_ENSURE_SUCCESS(rv, rv);
NS_ENSURE_TRUE(selection, NS_ERROR_FAILURE);
PRInt32 numRanges = 0;
selection->GetRangeCount(&numRanges);
if (numRanges < 1)
return NS_OK;
// We only operate on the first range in the selection!
nsCOMPtr<nsISelectionPrivate> selPriv = do_QueryInterface(selection);
NS_ENSURE_TRUE(selPriv, NS_ERROR_FAILURE);
nsRefPtr<nsFrameSelection> frameSel;
rv = selPriv->GetFrameSelection(getter_AddRefs(frameSel));
NS_ENSURE_SUCCESS(rv, rv);
NS_ENSURE_TRUE(frameSel, NS_ERROR_FAILURE);
nsRefPtr<nsTypedSelection> typedSel =
frameSel->GetSelection(nsISelectionController::SELECTION_NORMAL);
NS_ENSURE_TRUE(typedSel, NS_ERROR_FAILURE);
if (aDirection) {
nsCOMPtr<nsISelectionPrivate> selPriv = do_QueryInterface(selection);
if (selPriv) {
nsDirection direction = selPriv->GetSelectionDirection();
if (direction == eDirNext) {
*aDirection = eForward;
} else if (direction == eDirPrevious) {
*aDirection = eBackward;
} else {
NS_NOTREACHED("Invalid nsDirection enum value");
}
nsDirection direction = typedSel->GetSelectionDirection();
if (direction == eDirNext) {
*aDirection = eForward;
} else if (direction == eDirPrevious) {
*aDirection = eBackward;
} else {
NS_NOTREACHED("Invalid nsDirection enum value");
}
}
@@ -1129,40 +1084,10 @@ nsTextControlFrame::GetSelectionRange(PRInt32* aSelectionStart,
return NS_OK;
}
nsCOMPtr<nsIDOMRange> firstRange;
rv = selection->GetRangeAt(0, getter_AddRefs(firstRange));
NS_ENSURE_SUCCESS(rv, rv);
NS_ENSURE_TRUE(firstRange, NS_ERROR_FAILURE);
nsContentUtils::GetSelectionInTextControl(typedSel,
GetRootNodeAndInitializeEditor(), *aSelectionStart, *aSelectionEnd);
nsCOMPtr<nsIDOMNode> startNode, endNode;
PRInt32 startOffset = 0, endOffset = 0;
// Get the start point of the range.
rv = firstRange->GetStartContainer(getter_AddRefs(startNode));
NS_ENSURE_SUCCESS(rv, rv);
NS_ENSURE_TRUE(startNode, NS_ERROR_FAILURE);
rv = firstRange->GetStartOffset(&startOffset);
NS_ENSURE_SUCCESS(rv, rv);
// Get the end point of the range.
rv = firstRange->GetEndContainer(getter_AddRefs(endNode));
NS_ENSURE_SUCCESS(rv, rv);
NS_ENSURE_TRUE(endNode, NS_ERROR_FAILURE);
rv = firstRange->GetEndOffset(&endOffset);
NS_ENSURE_SUCCESS(rv, rv);
// Convert the start point to a selection offset.
rv = DOMPointToOffset(startNode, startOffset, aSelectionStart);
NS_ENSURE_SUCCESS(rv, rv);
// Convert the end point to a selection offset.
return DOMPointToOffset(endNode, endOffset, aSelectionEnd);
return NS_OK;
}
/////END INTERFACE IMPLEMENTATIONS