Skip to content

Commit

Permalink
Bug 1718924 - part 5: Add `OffsetEntry::OffsetInTextNodeIsInRangeOrEn…
Browse files Browse the repository at this point in the history
…dOffset()` r=m_kato

Depends on D119151

Differential Revision: https://phabricator.services.mozilla.com/D119152
  • Loading branch information
masayuki-nakano committed Jul 13, 2021
1 parent 84f8568 commit d1c80af
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions editor/spellchecker/TextServicesDocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ class OffsetEntry final {
mIsValid(true) {}

uint32_t EndOffsetInTextNode() const { return mOffsetInTextNode + mLength; }
bool OffsetInTextNodeIsInRangeOrEndOffset(uint32_t aOffsetInTextNode) const {
return aOffsetInTextNode >= mOffsetInTextNode &&
aOffsetInTextNode <= EndOffsetInTextNode();
}

OwningNonNull<Text> mTextNode;
uint32_t mOffsetInTextNode;
Expand Down Expand Up @@ -1792,8 +1796,7 @@ nsresult TextServicesDocument::GetCollapsedSelection(
NS_ENSURE_TRUE(entry, NS_ERROR_FAILURE);

if (entry->mTextNode == parent->AsText() &&
entry->mOffsetInTextNode <= offset &&
offset <= entry->EndOffsetInTextNode()) {
entry->OffsetInTextNodeIsInRangeOrEndOffset(offset)) {
*aSelStatus = BlockSelectionStatus::eBlockContains;
*aSelOffset = entry->mStrOffset + (offset - entry->mOffsetInTextNode);
*aSelLength = 0;
Expand Down Expand Up @@ -1901,8 +1904,8 @@ nsresult TextServicesDocument::GetCollapsedSelection(
const OffsetEntry* const entry = mOffsetTable[i];
NS_ENSURE_TRUE(entry, NS_ERROR_FAILURE);

if (entry->mTextNode == textNode && entry->mOffsetInTextNode <= offset &&
offset <= entry->EndOffsetInTextNode()) {
if (entry->mTextNode == textNode &&
entry->OffsetInTextNodeIsInRangeOrEndOffset(offset)) {
*aSelStatus = BlockSelectionStatus::eBlockContains;
*aSelOffset = entry->mStrOffset + (offset - entry->mOffsetInTextNode);
*aSelLength = 0;
Expand Down Expand Up @@ -2113,11 +2116,10 @@ nsresult TextServicesDocument::GetUncollapsedSelection(
entry = mOffsetTable[i];
NS_ENSURE_TRUE(entry, NS_ERROR_FAILURE);
if (!found) {
if (entry->mTextNode == p1.get() && entry->mOffsetInTextNode <= o1 &&
o1 <= entry->EndOffsetInTextNode()) {
if (entry->mTextNode == p1.get() &&
entry->OffsetInTextNodeIsInRangeOrEndOffset(o1)) {
*aSelOffset = entry->mStrOffset + (o1 - entry->mOffsetInTextNode);
if (p1 == p2 && entry->mOffsetInTextNode <= o2 &&
o2 <= entry->EndOffsetInTextNode()) {
if (p1 == p2 && entry->OffsetInTextNodeIsInRangeOrEndOffset(o2)) {
// The start and end of the range are in the same offset
// entry. Calculate the length of the range then we're done.
*aSelLength = o2 - o1;
Expand All @@ -2129,8 +2131,8 @@ nsresult TextServicesDocument::GetUncollapsedSelection(
found = true;
}
} else { // Found.
if (entry->mTextNode == p2.get() && entry->mOffsetInTextNode <= o2 &&
o2 <= entry->EndOffsetInTextNode()) {
if (entry->mTextNode == p2.get() &&
entry->OffsetInTextNodeIsInRangeOrEndOffset(o2)) {
// We found the end of the range. Calculate the length of the
// sub string that is before the end of the range, then we're done.
*aSelLength += o2 - entry->mOffsetInTextNode;
Expand Down

0 comments on commit d1c80af

Please sign in to comment.