From 70c8ff8e5af7d2661b64fb92a158f2860af7766c Mon Sep 17 00:00:00 2001 From: "Matt A. Tobin" Date: Fri, 17 Apr 2020 06:10:23 -0400 Subject: Bug 1377978 - Make nsRange use uint32_t to offset Tag #1375 --- editor/libeditor/EditorBase.cpp | 6 +-- editor/libeditor/HTMLEditRules.cpp | 67 ++++++++++++++++++----------- editor/libeditor/HTMLEditor.cpp | 10 ++--- editor/libeditor/HTMLEditorDataTransfer.cpp | 7 ++- editor/libeditor/HTMLStyleEditor.cpp | 2 +- editor/libeditor/HTMLTableEditor.cpp | 15 +++---- editor/txtsvc/nsFilteredContentIterator.cpp | 8 ++-- editor/txtsvc/nsTextServicesDocument.cpp | 47 ++++++++++++-------- 8 files changed, 90 insertions(+), 72 deletions(-) (limited to 'editor') diff --git a/editor/libeditor/EditorBase.cpp b/editor/libeditor/EditorBase.cpp index 5df4ff2c4..27983df31 100644 --- a/editor/libeditor/EditorBase.cpp +++ b/editor/libeditor/EditorBase.cpp @@ -5275,7 +5275,7 @@ EditorBase::GetIMESelectionStartOffsetIn(nsINode* aTextNode) return -1; } - int32_t minOffset = INT32_MAX; + uint32_t minOffset = UINT32_MAX; static const SelectionType kIMESelectionTypes[] = { SelectionType::eIMERawClause, SelectionType::eIMESelectedRawClause, @@ -5295,15 +5295,11 @@ EditorBase::GetIMESelectionStartOffsetIn(nsINode* aTextNode) if (NS_WARN_IF(range->GetStartParent() != aTextNode)) { // ignore the start offset... } else { - MOZ_ASSERT(range->StartOffset() >= 0, - "start offset shouldn't be negative"); minOffset = std::min(minOffset, range->StartOffset()); } if (NS_WARN_IF(range->GetEndParent() != aTextNode)) { // ignore the end offset... } else { - MOZ_ASSERT(range->EndOffset() >= 0, - "start offset shouldn't be negative"); minOffset = std::min(minOffset, range->EndOffset()); } } diff --git a/editor/libeditor/HTMLEditRules.cpp b/editor/libeditor/HTMLEditRules.cpp index c97ebf27f..805092eb7 100644 --- a/editor/libeditor/HTMLEditRules.cpp +++ b/editor/libeditor/HTMLEditRules.cpp @@ -427,7 +427,7 @@ HTMLEditRules::AfterEditInner(EditAction action, NS_ENSURE_STATE(selection); nsCOMPtr rangeStartParent, rangeEndParent; - int32_t rangeStartOffset = 0, rangeEndOffset = 0; + uint32_t rangeStartOffset = 0, rangeEndOffset = 0; // do we have a real range to act on? bool bDamagedRange = false; if (mDocChangeRange) { @@ -535,8 +535,8 @@ HTMLEditRules::AfterEditInner(EditAction action, mHTMLEditor->HandleInlineSpellCheck(action, selection, GetAsDOMNode(mRangeItem->startNode), mRangeItem->startOffset, - rangeStartParent, rangeStartOffset, - rangeEndParent, rangeEndOffset); + rangeStartParent, static_cast(rangeStartOffset), + rangeEndParent, static_cast(rangeEndOffset)); NS_ENSURE_SUCCESS(rv, rv); // detect empty doc @@ -5151,9 +5151,8 @@ HTMLEditRules::NormalizeSelection(Selection* inSelection) RefPtr range = inSelection->GetRangeAt(0); NS_ENSURE_TRUE(range, NS_ERROR_NULL_POINTER); nsCOMPtr startNode, endNode; - int32_t startOffset, endOffset; + uint32_t startOffset, endOffset; nsCOMPtr newStartNode, newEndNode; - int32_t newStartOffset, newEndOffset; rv = range->GetStartContainer(getter_AddRefs(startNode)); NS_ENSURE_SUCCESS(rv, rv); @@ -5166,22 +5165,22 @@ HTMLEditRules::NormalizeSelection(Selection* inSelection) // adjusted values default to original values newStartNode = startNode; - newStartOffset = startOffset; + uint32_t newStartOffset = startOffset; newEndNode = endNode; - newEndOffset = endOffset; + uint32_t newEndOffset = endOffset; // some locals we need for whitespace code nsCOMPtr unused; - int32_t offset; + int32_t offset = -1; WSType wsType; // let the whitespace code do the heavy lifting - WSRunObject wsEndObj(mHTMLEditor, endNode, endOffset); + WSRunObject wsEndObj(mHTMLEditor, endNode, static_cast(endOffset)); // is there any intervening visible whitespace? if so we can't push selection past that, // it would visibly change maening of users selection nsCOMPtr endNode_(do_QueryInterface(endNode)); - wsEndObj.PriorVisibleNode(endNode_, endOffset, address_of(unused), - &offset, &wsType); + wsEndObj.PriorVisibleNode(endNode_, static_cast(endOffset), + address_of(unused), &offset, &wsType); if (wsType != WSType::text && wsType != WSType::normalWS) { // eThisBlock and eOtherBlock conveniently distinquish cases // of going "down" into a block and "up" out of a block. @@ -5191,36 +5190,44 @@ HTMLEditRules::NormalizeSelection(Selection* inSelection) GetAsDOMNode(mHTMLEditor->GetRightmostChild(wsEndObj.mStartReasonNode, true)); if (child) { - newEndNode = EditorBase::GetNodeLocation(child, &newEndOffset); - ++newEndOffset; // offset *after* child + int32_t offset = -1; + newEndNode = EditorBase::GetNodeLocation(child, &offset); + // offset *after* child + newEndOffset = static_cast(offset + 1); } // else block is empty - we can leave selection alone here, i think. } else if (wsEndObj.mStartReason == WSType::thisBlock) { // endpoint is just after start of this block nsCOMPtr child; NS_ENSURE_STATE(mHTMLEditor); - mHTMLEditor->GetPriorHTMLNode(endNode, endOffset, address_of(child)); + mHTMLEditor->GetPriorHTMLNode(endNode, static_cast(endOffset), + address_of(child)); if (child) { - newEndNode = EditorBase::GetNodeLocation(child, &newEndOffset); - ++newEndOffset; // offset *after* child + int32_t offset = -1; + newEndNode = EditorBase::GetNodeLocation(child, &offset); + // offset *after* child + newEndOffset = static_cast(offset + 1); } // else block is empty - we can leave selection alone here, i think. } else if (wsEndObj.mStartReason == WSType::br) { // endpoint is just after break. lets adjust it to before it. + int32_t offset = -1; newEndNode = EditorBase::GetNodeLocation(GetAsDOMNode(wsEndObj.mStartReasonNode), - &newEndOffset); + &offset); + newEndOffset = static_cast(offset);; } } // similar dealio for start of range - WSRunObject wsStartObj(mHTMLEditor, startNode, startOffset); + WSRunObject wsStartObj(mHTMLEditor, startNode, + static_cast(startOffset)); // is there any intervening visible whitespace? if so we can't push selection past that, // it would visibly change maening of users selection nsCOMPtr startNode_(do_QueryInterface(startNode)); - wsStartObj.NextVisibleNode(startNode_, startOffset, address_of(unused), - &offset, &wsType); + wsStartObj.NextVisibleNode(startNode_, static_cast(startOffset), + address_of(unused), &offset, &wsType); if (wsType != WSType::text && wsType != WSType::normalWS) { // eThisBlock and eOtherBlock conveniently distinquish cases // of going "down" into a block and "up" out of a block. @@ -5230,23 +5237,31 @@ HTMLEditRules::NormalizeSelection(Selection* inSelection) GetAsDOMNode(mHTMLEditor->GetLeftmostChild(wsStartObj.mEndReasonNode, true)); if (child) { - newStartNode = EditorBase::GetNodeLocation(child, &newStartOffset); + int32_t offset = -1; + newStartNode = EditorBase::GetNodeLocation(child, &offset); + newStartOffset = static_cast(offset); } // else block is empty - we can leave selection alone here, i think. } else if (wsStartObj.mEndReason == WSType::thisBlock) { // startpoint is just before end of this block nsCOMPtr child; NS_ENSURE_STATE(mHTMLEditor); - mHTMLEditor->GetNextHTMLNode(startNode, startOffset, address_of(child)); + mHTMLEditor->GetNextHTMLNode(startNode, static_cast(startOffset), + address_of(child)); if (child) { - newStartNode = EditorBase::GetNodeLocation(child, &newStartOffset); + int32_t offset = -1; + newStartNode = EditorBase::GetNodeLocation(child, &offset); + newStartOffset = static_cast(offset); } // else block is empty - we can leave selection alone here, i think. } else if (wsStartObj.mEndReason == WSType::br) { // startpoint is just before a break. lets adjust it to after it. + int32_t offset = -1; newStartNode = EditorBase::GetNodeLocation(GetAsDOMNode(wsStartObj.mEndReasonNode), - &newStartOffset); + &offset); + // offset *after* break + newStartOffset = static_cast(offset + 1); ++newStartOffset; // offset *after* break } } @@ -7974,7 +7989,7 @@ HTMLEditRules::UpdateDocChangeRange(nsRange* aRange) NS_ENSURE_SUCCESS(rv, rv); // Positive result means mDocChangeRange start is after aRange start. if (result > 0) { - int32_t startOffset; + uint32_t startOffset; rv = aRange->GetStartOffset(&startOffset); NS_ENSURE_SUCCESS(rv, rv); rv = mDocChangeRange->SetStart(startNode, startOffset); @@ -7988,9 +8003,9 @@ HTMLEditRules::UpdateDocChangeRange(nsRange* aRange) // Negative result means mDocChangeRange end is before aRange end. if (result < 0) { nsCOMPtr endNode; - int32_t endOffset; rv = aRange->GetEndContainer(getter_AddRefs(endNode)); NS_ENSURE_SUCCESS(rv, rv); + uint32_t endOffset; rv = aRange->GetEndOffset(&endOffset); NS_ENSURE_SUCCESS(rv, rv); rv = mDocChangeRange->SetEnd(endNode, endOffset); diff --git a/editor/libeditor/HTMLEditor.cpp b/editor/libeditor/HTMLEditor.cpp index fd11d1a45..766ab81cf 100644 --- a/editor/libeditor/HTMLEditor.cpp +++ b/editor/libeditor/HTMLEditor.cpp @@ -2405,21 +2405,19 @@ HTMLEditor::GetSelectedElement(const nsAString& aTagName, NS_ENSURE_STATE(range); nsCOMPtr startParent; - int32_t startOffset, endOffset; nsresult rv = range->GetStartContainer(getter_AddRefs(startParent)); NS_ENSURE_SUCCESS(rv, rv); - rv = range->GetStartOffset(&startOffset); - NS_ENSURE_SUCCESS(rv, rv); + uint32_t startOffset = range->StartOffset(); nsCOMPtr endParent; rv = range->GetEndContainer(getter_AddRefs(endParent)); NS_ENSURE_SUCCESS(rv, rv); - rv = range->GetEndOffset(&endOffset); - NS_ENSURE_SUCCESS(rv, rv); + uint32_t endOffset = range->EndOffset(); // Optimization for a single selected element if (startParent && startParent == endParent && endOffset - startOffset == 1) { - nsCOMPtr selectedNode = GetChildAt(startParent, startOffset); + nsCOMPtr selectedNode = + GetChildAt(startParent, static_cast(startOffset)); NS_ENSURE_SUCCESS(rv, NS_OK); if (selectedNode) { selectedNode->GetNodeName(domTagName); diff --git a/editor/libeditor/HTMLEditorDataTransfer.cpp b/editor/libeditor/HTMLEditorDataTransfer.cpp index c56fbead7..0c01bdd1c 100644 --- a/editor/libeditor/HTMLEditorDataTransfer.cpp +++ b/editor/libeditor/HTMLEditorDataTransfer.cpp @@ -145,14 +145,13 @@ HTMLEditor::LoadHTML(const nsAString& aInputString) rv = range->GetStartContainer(getter_AddRefs(parent)); NS_ENSURE_SUCCESS(rv, rv); NS_ENSURE_TRUE(parent, NS_ERROR_NULL_POINTER); - int32_t childOffset; - rv = range->GetStartOffset(&childOffset); - NS_ENSURE_SUCCESS(rv, rv); + uint32_t childOffset = range->StartOffset(); nsCOMPtr nodeToInsert; docfrag->GetFirstChild(getter_AddRefs(nodeToInsert)); while (nodeToInsert) { - rv = InsertNode(nodeToInsert, parent, childOffset++); + rv = InsertNode(nodeToInsert, parent, + static_cast(childOffset++)); NS_ENSURE_SUCCESS(rv, rv); docfrag->GetFirstChild(getter_AddRefs(nodeToInsert)); } diff --git a/editor/libeditor/HTMLStyleEditor.cpp b/editor/libeditor/HTMLStyleEditor.cpp index 7141cfd61..7d1217069 100644 --- a/editor/libeditor/HTMLStyleEditor.cpp +++ b/editor/libeditor/HTMLStyleEditor.cpp @@ -1078,7 +1078,7 @@ HTMLEditor::GetInlinePropertyBase(nsIAtom& aProperty, if (content->GetAsText()) { if (!isCollapsed && first && firstNodeInRange) { firstNodeInRange = false; - if (range->StartOffset() == (int32_t)content->Length()) { + if (range->StartOffset() == content->Length()) { continue; } } else if (content == endNode && !endOffset) { diff --git a/editor/libeditor/HTMLTableEditor.cpp b/editor/libeditor/HTMLTableEditor.cpp index 06d6cae26..b26466179 100644 --- a/editor/libeditor/HTMLTableEditor.cpp +++ b/editor/libeditor/HTMLTableEditor.cpp @@ -2932,11 +2932,10 @@ HTMLEditor::GetCellFromRange(nsRange* aRange, NS_ENSURE_SUCCESS(rv, rv); NS_ENSURE_TRUE(startParent, NS_ERROR_FAILURE); - int32_t startOffset; - rv = aRange->GetStartOffset(&startOffset); - NS_ENSURE_SUCCESS(rv, rv); + uint32_t startOffset = aRange->StartOffset(); - nsCOMPtr childNode = GetChildAt(startParent, startOffset); + nsCOMPtr childNode = + GetChildAt(startParent, static_cast(startOffset)); // This means selection is probably at a text node (or end of doc?) if (!childNode) { return NS_ERROR_FAILURE; @@ -2947,15 +2946,11 @@ HTMLEditor::GetCellFromRange(nsRange* aRange, NS_ENSURE_SUCCESS(rv, rv); NS_ENSURE_TRUE(startParent, NS_ERROR_FAILURE); - int32_t endOffset; - rv = aRange->GetEndOffset(&endOffset); - NS_ENSURE_SUCCESS(rv, rv); - // If a cell is deleted, the range is collapse - // (startOffset == endOffset) + // (startOffset == aRange->EndOffset()) // so tell caller the cell wasn't found if (startParent == endParent && - endOffset == startOffset+1 && + aRange->EndOffset() == startOffset+1 && HTMLEditUtils::IsTableCell(childNode)) { // Should we also test if frame is selected? (Use GetCellDataAt()) // (Let's not for now -- more efficient) diff --git a/editor/txtsvc/nsFilteredContentIterator.cpp b/editor/txtsvc/nsFilteredContentIterator.cpp index c8ea734c4..cccfb90ad 100644 --- a/editor/txtsvc/nsFilteredContentIterator.cpp +++ b/editor/txtsvc/nsFilteredContentIterator.cpp @@ -240,13 +240,15 @@ ContentIsInTraversalRange(nsRange* aRange, nsIDOMNode* aNextNode, bool aIsPreMod nsCOMPtr sNode; nsCOMPtr eNode; - int32_t sOffset; - int32_t eOffset; + uint32_t sOffset; + uint32_t eOffset; aRange->GetStartContainer(getter_AddRefs(sNode)); aRange->GetStartOffset(&sOffset); aRange->GetEndContainer(getter_AddRefs(eNode)); aRange->GetEndOffset(&eOffset); - return ContentIsInTraversalRange(content, aIsPreMode, sNode, sOffset, eNode, eOffset); + return ContentIsInTraversalRange(content, aIsPreMode, + sNode, static_cast(sOffset), + eNode, static_cast(eOffset)); } //------------------------------------------------------------ diff --git a/editor/txtsvc/nsTextServicesDocument.cpp b/editor/txtsvc/nsTextServicesDocument.cpp index ccf964d2c..23a1bec3f 100644 --- a/editor/txtsvc/nsTextServicesDocument.cpp +++ b/editor/txtsvc/nsTextServicesDocument.cpp @@ -510,7 +510,6 @@ nsTextServicesDocument::LastSelectedBlock(TSDBlockSelectionStatus *aSelStatus, nsCOMPtr iter; RefPtr range; nsCOMPtr parent; - int32_t rangeCount, offset; if (isCollapsed) { // We have a caret. Check if the caret is in a text node. @@ -537,6 +536,7 @@ nsTextServicesDocument::LastSelectedBlock(TSDBlockSelectionStatus *aSelStatus, return NS_ERROR_FAILURE; } + uint32_t offset; rv = range->GetStartOffset(&offset); if (NS_FAILED(rv)) { @@ -596,8 +596,9 @@ nsTextServicesDocument::LastSelectedBlock(TSDBlockSelectionStatus *aSelStatus, // position to the end of the document, then walk forwards // till you find a text node, then find the beginning of it's block. - rv = CreateDocumentContentRootToNodeOffsetRange(parent, offset, false, - getter_AddRefs(range)); + rv = CreateDocumentContentRootToNodeOffsetRange( + parent, static_cast(offset), false, + getter_AddRefs(range)); if (NS_FAILED(rv)) { UNLOCK_DOC(this); @@ -690,6 +691,7 @@ nsTextServicesDocument::LastSelectedBlock(TSDBlockSelectionStatus *aSelStatus, // beginning of its text block, and make it the current // block. + int32_t rangeCount; rv = selection->GetRangeCount(&rangeCount); if (NS_FAILED(rv)) { @@ -797,6 +799,7 @@ nsTextServicesDocument::LastSelectedBlock(TSDBlockSelectionStatus *aSelStatus, return NS_ERROR_FAILURE; } + uint32_t offset; rv = range->GetEndOffset(&offset); if (NS_FAILED(rv)) { @@ -804,8 +807,8 @@ nsTextServicesDocument::LastSelectedBlock(TSDBlockSelectionStatus *aSelStatus, return rv; } - rv = CreateDocumentContentRootToNodeOffsetRange(parent, offset, false, - getter_AddRefs(range)); + rv = CreateDocumentContentRootToNodeOffsetRange( + parent, static_cast(offset), false, getter_AddRefs(range)); if (NS_FAILED(rv)) { UNLOCK_DOC(this); @@ -2377,14 +2380,16 @@ nsTextServicesDocument::GetCollapsedSelection(nsITextServicesDocument::TSDBlockS nsCOMPtr parent = do_QueryInterface(domParent); MOZ_ASSERT(parent); - int32_t offset; + uint32_t offset; rv = range->GetStartOffset(&offset); NS_ENSURE_SUCCESS(rv, rv); int32_t e1s1 = nsContentUtils::ComparePoints(eStart->mNode, eStartOffset, - domParent, offset); + domParent, + static_cast(offset)); int32_t e2s1 = nsContentUtils::ComparePoints(eEnd->mNode, eEndOffset, - domParent, offset); + domParent, + static_cast(offset)); if (e1s1 > 0 || e2s1 < 0) { // We're done if the caret is outside the current text block. @@ -2401,8 +2406,8 @@ nsTextServicesDocument::GetCollapsedSelection(nsITextServicesDocument::TSDBlockS NS_ENSURE_TRUE(entry, NS_ERROR_FAILURE); if (entry->mNode == domParent.get() && - entry->mNodeOffset <= offset && - offset <= entry->mNodeOffset + entry->mLength) { + entry->mNodeOffset <= static_cast(offset) && + static_cast(offset) <= entry->mNodeOffset + entry->mLength) { *aSelStatus = nsITextServicesDocument::eBlockContains; *aSelOffset = entry->mStrOffset + (offset - entry->mNodeOffset); *aSelLength = 0; @@ -2440,7 +2445,7 @@ nsTextServicesDocument::GetCollapsedSelection(nsITextServicesDocument::TSDBlockS // If the parent has children, position the iterator // on the child that is to the left of the offset. - uint32_t childIndex = (uint32_t)offset; + uint32_t childIndex = offset; if (childIndex > 0) { uint32_t numChildren = parent->GetChildCount(); @@ -2526,8 +2531,8 @@ nsTextServicesDocument::GetCollapsedSelection(nsITextServicesDocument::TSDBlockS NS_ENSURE_TRUE(entry, NS_ERROR_FAILURE); if (entry->mNode == node->AsDOMNode() && - entry->mNodeOffset <= offset && - offset <= entry->mNodeOffset + entry->mLength) { + entry->mNodeOffset <= static_cast(offset) && + static_cast(offset) <= entry->mNodeOffset + entry->mLength) { *aSelStatus = nsITextServicesDocument::eBlockContains; *aSelOffset = entry->mStrOffset + (offset - entry->mNodeOffset); *aSelLength = 0; @@ -2817,9 +2822,12 @@ nsTextServicesDocument::GetRangeEndPoints(nsRange* aRange, NS_ENSURE_TRUE(aStartParent, NS_ERROR_FAILURE); - rv = aRange->GetStartOffset(aStartOffset); - - NS_ENSURE_SUCCESS(rv, rv); + uint32_t offset; + rv = aRange->GetStartOffset(&offset); + if (NS_WARN_IF(NS_FAILED(rv))) { + return rv; + } + *aStartOffset = static_cast(offset); rv = aRange->GetEndContainer(aEndParent); @@ -2827,7 +2835,12 @@ nsTextServicesDocument::GetRangeEndPoints(nsRange* aRange, NS_ENSURE_TRUE(aEndParent, NS_ERROR_FAILURE); - return aRange->GetEndOffset(aEndOffset); + rv = aRange->GetEndOffset(&offset); + if (NS_WARN_IF(NS_FAILED(rv))) { + return rv; + } + *aEndOffset = static_cast(offset); + return NS_OK; } nsresult -- cgit v1.2.3