From 83778753b6b50057ca49f7836000b60f632b8703 Mon Sep 17 00:00:00 2001 From: Makoto Kato Date: Mon, 28 Aug 2017 17:54:34 +0900 Subject: [PATCH] Bug 1391978 - Part 6. Replace nsISelection::Extend with Selection::Extend. r=masayuki Except to HTMLEditRules::NormalizeSelection, I replace nsISelection::Extend with Selection::Extend. MozReview-Commit-ID: H83zpvAo5Xa --HG-- extra : rebase_source : b217dd1d506229d848126b9c2494d1b4f9dc3a35 extra : histedit_source : 8999bc5b1b1edfe7e48a432d52640b8181490624 --- editor/libeditor/HTMLEditor.cpp | 21 +++++++------ editor/libeditor/TextEditor.cpp | 6 ++-- editor/libeditor/TextEditorTest.cpp | 30 +++++++++--------- editor/txtsvc/nsTextServicesDocument.cpp | 40 +++++++++++++----------- 4 files changed, 49 insertions(+), 48 deletions(-) diff --git a/editor/libeditor/HTMLEditor.cpp b/editor/libeditor/HTMLEditor.cpp index 2e2cab9f7ef6..8e036a2dc378 100644 --- a/editor/libeditor/HTMLEditor.cpp +++ b/editor/libeditor/HTMLEditor.cpp @@ -1683,17 +1683,18 @@ HTMLEditor::SelectElement(nsIDOMElement* aElement) RefPtr selection = GetSelection(); NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER); - nsCOMPtrparent; - nsresult rv = aElement->GetParentNode(getter_AddRefs(parent)); - if (NS_SUCCEEDED(rv) && parent) { - int32_t offsetInParent = GetChildOffset(aElement, parent); + nsINode* parent = element->GetParentNode(); + if (NS_WARN_IF(!parent)) { + return NS_ERROR_FAILURE; + } - // Collapse selection to just before desired element, - rv = selection->Collapse(parent, offsetInParent); - if (NS_SUCCEEDED(rv)) { - // then extend it to just after - rv = selection->Extend(parent, offsetInParent + 1); - } + int32_t offsetInParent = parent->IndexOf(element); + + // Collapse selection to just before desired element, + nsresult rv = selection->Collapse(parent, offsetInParent); + if (NS_SUCCEEDED(rv)) { + // then extend it to just after + rv = selection->Extend(parent, offsetInParent + 1); } return rv; } diff --git a/editor/libeditor/TextEditor.cpp b/editor/libeditor/TextEditor.cpp index d58f610a1a9d..976f36df2a3b 100644 --- a/editor/libeditor/TextEditor.cpp +++ b/editor/libeditor/TextEditor.cpp @@ -1640,15 +1640,15 @@ TextEditor::SelectEntireDocument(Selection* aSelection) // Don't select the trailing BR node if we have one int32_t selOffset; - nsCOMPtr selNode; + nsCOMPtr selNode; rv = GetEndNodeAndOffset(aSelection, getter_AddRefs(selNode), &selOffset); NS_ENSURE_SUCCESS(rv, rv); - nsCOMPtr childNode = GetChildAt(selNode, selOffset - 1); + nsINode* childNode = selNode->GetChildAt(selOffset - 1); if (childNode && TextEditUtils::IsMozBR(childNode)) { int32_t parentOffset; - nsCOMPtr parentNode = GetNodeLocation(childNode, &parentOffset); + nsINode* parentNode = GetNodeLocation(childNode, &parentOffset); return aSelection->Extend(parentNode, parentOffset); } diff --git a/editor/libeditor/TextEditorTest.cpp b/editor/libeditor/TextEditorTest.cpp index 0da00bbe97db..af36fcf1f77d 100644 --- a/editor/libeditor/TextEditorTest.cpp +++ b/editor/libeditor/TextEditorTest.cpp @@ -144,10 +144,10 @@ nsresult TextEditorTest::TestTextProperties() uint32_t count; nodeList->GetLength(&count); NS_ASSERTION(0!=count, "there are no text nodes in the document!"); - nsCOMPtrtextNode; - rv = nodeList->Item(count - 1, getter_AddRefs(textNode)); + nsCOMPtr domTextNode; + rv = nodeList->Item(count - 1, getter_AddRefs(domTextNode)); TEST_RESULT(rv); - TEST_POINTER(textNode.get()); + TEST_POINTER(domTextNode.get()); // set the whole text node to bold printf("set the whole first text node to bold\n"); @@ -155,12 +155,10 @@ nsresult TextEditorTest::TestTextProperties() rv = mEditor->GetSelection(getter_AddRefs(selection)); TEST_RESULT(rv); TEST_POINTER(selection.get()); - nsCOMPtrtextData; - textData = do_QueryInterface(textNode); - uint32_t length; - textData->GetLength(&length); - selection->Collapse(textNode, 0); - selection->Extend(textNode, length); + nsCOMPtr textNode = do_QueryInterface(domTextNode); + uint32_t length = textNode->Length(); + selection->AsSelection()->Collapse(textNode, 0); + selection->AsSelection()->Extend(textNode, length); nsCOMPtr htmlEditor (do_QueryInterface(mTextEditor)); NS_ENSURE_TRUE(htmlEditor, NS_ERROR_FAILURE); @@ -202,8 +200,8 @@ nsresult TextEditorTest::TestTextProperties() // set all but the first and last character to bold printf("set the first text node (1, length-1) to bold and italic, and (2, length-1) to underline.\n"); - selection->Collapse(textNode, 1); - selection->Extend(textNode, length-1); + selection->AsSelection()->Collapse(textNode, 1); + selection->AsSelection()->Extend(textNode, length-1); rv = htmlEditor->SetInlineProperty(b, empty, empty); TEST_RESULT(rv); rv = htmlEditor->GetInlineProperty(b, empty, empty, &first, &any, &all); @@ -233,14 +231,14 @@ nsresult TextEditorTest::TestTextProperties() TEST_POINTER(nodeList.get()); nodeList->GetLength(&count); NS_ASSERTION(0!=count, "there are no text nodes in the document!"); - rv = nodeList->Item(count-2, getter_AddRefs(textNode)); + rv = nodeList->Item(count-2, getter_AddRefs(domTextNode)); TEST_RESULT(rv); TEST_POINTER(textNode.get()); - textData = do_QueryInterface(textNode); - textData->GetLength(&length); + textNode = do_QueryInterface(domTextNode); + length = textNode->Length(); NS_ASSERTION(length==915, "wrong text node"); - selection->Collapse(textNode, 1); - selection->Extend(textNode, length-2); + selection->AsSelection()->Collapse(textNode, 1); + selection->AsSelection()->Extend(textNode, length-2); rv = htmlEditor->SetInlineProperty(u, empty, empty); TEST_RESULT(rv); rv = htmlEditor->GetInlineProperty(u, empty, empty, &first, &any, &all); diff --git a/editor/txtsvc/nsTextServicesDocument.cpp b/editor/txtsvc/nsTextServicesDocument.cpp index f8fe3e3af956..7555106c7ac1 100644 --- a/editor/txtsvc/nsTextServicesDocument.cpp +++ b/editor/txtsvc/nsTextServicesDocument.cpp @@ -2151,13 +2151,13 @@ nsTextServicesDocument::SetSelectionInternal(int32_t aOffset, int32_t aLength, b { NS_ENSURE_TRUE(mSelCon && aOffset >= 0 && aLength >= 0, NS_ERROR_FAILURE); - nsIDOMNode *sNode = 0, *eNode = 0; - int32_t sOffset = 0, eOffset = 0; + nsCOMPtr startNode; + int32_t startNodeOffset = 0; OffsetEntry *entry; // Find start of selection in node offset terms: - for (size_t i = 0; !sNode && i < mOffsetTable.Length(); i++) { + for (size_t i = 0; !startNode && i < mOffsetTable.Length(); i++) { entry = mOffsetTable[i]; if (entry->mIsValid) { if (entry->mIsInsertedText) { @@ -2166,8 +2166,8 @@ nsTextServicesDocument::SetSelectionInternal(int32_t aOffset, int32_t aLength, b // match exactly! if (entry->mStrOffset == aOffset) { - sNode = entry->mNode; - sOffset = entry->mNodeOffset + entry->mLength; + startNode = do_QueryInterface(entry->mNode); + startNodeOffset = entry->mNodeOffset + entry->mLength; } } else if (aOffset >= entry->mStrOffset) { bool foundEntry = false; @@ -2193,19 +2193,19 @@ nsTextServicesDocument::SetSelectionInternal(int32_t aOffset, int32_t aLength, b } if (foundEntry) { - sNode = entry->mNode; - sOffset = entry->mNodeOffset + aOffset - entry->mStrOffset; + startNode = do_QueryInterface(entry->mNode); + startNodeOffset = entry->mNodeOffset + aOffset - entry->mStrOffset; } } - if (sNode) { + if (startNode) { mSelStartIndex = static_cast(i); mSelStartOffset = aOffset; } } } - NS_ENSURE_TRUE(sNode, NS_ERROR_FAILURE); + NS_ENSURE_TRUE(startNode, NS_ERROR_FAILURE); // XXX: If we ever get a SetSelection() method in nsIEditor, we should // use it. @@ -2219,7 +2219,7 @@ nsTextServicesDocument::SetSelectionInternal(int32_t aOffset, int32_t aLength, b NS_ENSURE_SUCCESS(rv, rv); - rv = selection->Collapse(sNode, sOffset); + rv = selection->AsSelection()->Collapse(startNode, startNodeOffset); NS_ENSURE_SUCCESS(rv, rv); } @@ -2238,34 +2238,36 @@ nsTextServicesDocument::SetSelectionInternal(int32_t aOffset, int32_t aLength, b } // Find the end of the selection in node offset terms: + nsCOMPtr endNode; + int32_t endNodeOffset = 0; int32_t endOffset = aOffset + aLength; - for (int32_t i = mOffsetTable.Length() - 1; !eNode && i >= 0; i--) { + for (int32_t i = mOffsetTable.Length() - 1; !endNode && i >= 0; i--) { entry = mOffsetTable[i]; if (entry->mIsValid) { if (entry->mIsInsertedText) { - if (entry->mStrOffset == eOffset) { + if (entry->mStrOffset == endNodeOffset) { // If the selection ends on an inserted text offset entry, // the selection includes the entire entry! - eNode = entry->mNode; - eOffset = entry->mNodeOffset + entry->mLength; + endNode = do_QueryInterface(entry->mNode); + endNodeOffset = entry->mNodeOffset + entry->mLength; } } else if (endOffset >= entry->mStrOffset && endOffset <= entry->mStrOffset + entry->mLength) { - eNode = entry->mNode; - eOffset = entry->mNodeOffset + endOffset - entry->mStrOffset; + endNode = do_QueryInterface(entry->mNode); + endNodeOffset = entry->mNodeOffset + endOffset - entry->mStrOffset; } - if (eNode) { + if (endNode) { mSelEndIndex = i; mSelEndOffset = endOffset; } } } - if (aDoUpdate && eNode) { - nsresult rv = selection->Extend(eNode, eOffset); + if (aDoUpdate && endNode) { + nsresult rv = selection->AsSelection()->Extend(endNode, endNodeOffset); NS_ENSURE_SUCCESS(rv, rv); }