Bug 1700051: part 29) Refactor `TextNodeContainsDOMWordSeparator` to `FindOffsetOfLastDOMWordSeparatorSequence`. r=smaug

More descriptive and separates input- from output-arguments.

Differential Revision: https://phabricator.services.mozilla.com/D112180
This commit is contained in:
Mirko Brodesser 2021-04-16 08:10:11 +00:00
Родитель cd7fa085fa
Коммит 70371eb59b
1 изменённых файлов: 19 добавлений и 29 удалений

Просмотреть файл

@ -666,21 +666,16 @@ static inline bool IsBRElement(nsINode* aNode) {
}
/**
* Given a TextNode, checks to see if there's a DOM word separator before
* aBeforeOffset within it. This function does not modify aSeparatorOffset when
* it returns false.
* Given a TextNode, finds the last sequence of DOM word separators before
* aBeforeOffset and returns the offset to its first element.
*
* @param aContent the TextNode to check.
* @param aBeforeOffset the offset in the TextNode before which we will search
* for the DOM separator. You can pass INT32_MAX to search the entire
* length of the string.
* @param aSeparatorOffset will be set to the offset of the first separator it
* encounters. Will not be written to if no separator is found.
* @returns True if it found a separator.
*/
static bool TextNodeContainsDOMWordSeparator(nsIContent* aContent,
int32_t aBeforeOffset,
int32_t* aSeparatorOffset) {
static Maybe<int32_t> FindOffsetOfLastDOMWordSeparatorSequence(
nsIContent* aContent, int32_t aBeforeOffset) {
const nsTextFragment* textFragment = aContent->GetText();
NS_ASSERTION(textFragment, "Where is our text?");
int32_t end = std::min(aBeforeOffset, int32_t(textFragment->GetLength()));
@ -688,26 +683,12 @@ static bool TextNodeContainsDOMWordSeparator(nsIContent* aContent,
if (textFragment->Is2b()) {
nsDependentSubstring targetText(textFragment->Get2b(), end);
WordSplitState<nsDependentSubstring> state(targetText);
const Maybe<int32_t> separatorOffset =
state.FindOffsetOfLastDOMWordSeparatorSequence(end);
if (separatorOffset) {
*aSeparatorOffset = *separatorOffset;
return true;
}
return false;
return state.FindOffsetOfLastDOMWordSeparatorSequence(end);
}
nsDependentCSubstring targetText(textFragment->Get1b(), end);
WordSplitState<nsDependentCSubstring> state(targetText);
const Maybe<int32_t> separatorOffset =
state.FindOffsetOfLastDOMWordSeparatorSequence(end);
if (separatorOffset) {
*aSeparatorOffset = *separatorOffset;
return true;
}
return false;
return state.FindOffsetOfLastDOMWordSeparatorSequence(end);
}
/**
@ -727,8 +708,15 @@ static bool ContainsDOMWordSeparator(nsINode* aNode, int32_t aBeforeOffset,
if (!IsSpellCheckingTextNode(aNode)) return false;
return TextNodeContainsDOMWordSeparator(aNode->AsContent(), aBeforeOffset,
aSeparatorOffset);
const Maybe<int32_t> separatorOffset =
FindOffsetOfLastDOMWordSeparatorSequence(aNode->AsContent(),
aBeforeOffset);
if (separatorOffset) {
*aSeparatorOffset = *separatorOffset;
return true;
}
return false;
}
static bool IsBreakElement(nsINode* aNode) {
@ -808,8 +796,10 @@ void mozInlineSpellWordUtil::AdjustSoftBeginAndBuildSoftText() {
nsIContent* prevNode = node->GetPreviousSibling();
while (prevNode && IsSpellCheckingTextNode(prevNode)) {
mSoftBegin.mNode = prevNode;
if (TextNodeContainsDOMWordSeparator(prevNode, INT32_MAX,
&newOffset)) {
const Maybe<int32_t> separatorOffset =
FindOffsetOfLastDOMWordSeparatorSequence(prevNode, INT32_MAX);
if (separatorOffset) {
newOffset = *separatorOffset;
break;
}
prevNode = prevNode->GetPreviousSibling();