Bug 1700051: part 31.1) Factor `SoftText` out. r=smaug

The `mSoft*` members belong together and will be moved to `SoftText` in
the following reviews.

Depends on D112540

Differential Revision: https://phabricator.services.mozilla.com/D112541
This commit is contained in:
Mirko Brodesser 2021-04-20 07:49:10 +00:00
Родитель acff2bb77d
Коммит 1b13e1f445
2 изменённых файлов: 30 добавлений и 24 удалений

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

@ -357,7 +357,7 @@ bool mozInlineSpellWordUtil::GetNextWord(nsAString& aText,
MakeNodeOffsetRangeForWord(word, aNodeOffsetRange);
++mNextWordIndex;
*aSkipChecking = !word.mCheckableWord;
::NormalizeWord(mSoftText, word.mSoftTextOffset, word.mLength, aText);
::NormalizeWord(mSoftText.mValue, word.mSoftTextOffset, word.mLength, aText);
MOZ_LOG(sInlineSpellWordUtilLog, LogLevel::Debug,
("%s: returning: %s (skip=%d)", __FUNCTION__,
@ -831,7 +831,7 @@ void mozInlineSpellWordUtil::AdjustSoftBeginAndBuildSoftText() {
// Now build up the string moving forward through the DOM until we reach
// the soft end and *then* see a DOM word separator, a non-inline-element
// boundary, or the hard end node.
mSoftText.Truncate();
mSoftText.mValue.Truncate();
mSoftTextDOMMapping.Clear();
bool seenSoftEnd = false;
// Leave this outside the loop so large heap string allocations can be reused
@ -864,11 +864,12 @@ void mozInlineSpellWordUtil::AdjustSoftBeginAndBuildSoftText() {
if (firstOffsetInNode < lastOffsetInNode) {
int32_t len = lastOffsetInNode - firstOffsetInNode;
mSoftTextDOMMapping.AppendElement(DOMTextMapping(
NodeOffset(node, firstOffsetInNode), mSoftText.Length(), len));
mSoftTextDOMMapping.AppendElement(
DOMTextMapping(NodeOffset(node, firstOffsetInNode),
mSoftText.mValue.Length(), len));
bool ok = textFragment->AppendTo(mSoftText, firstOffsetInNode, len,
mozilla::fallible);
bool ok = textFragment->AppendTo(mSoftText.mValue, firstOffsetInNode,
len, mozilla::fallible);
if (!ok) {
// probably out of memory, remove from mSoftTextDOMMapping
mSoftTextDOMMapping.RemoveLastElement();
@ -888,25 +889,25 @@ void mozInlineSpellWordUtil::AdjustSoftBeginAndBuildSoftText() {
// stop now.
if (seenSoftEnd) break;
// Record the break
mSoftText.Append(' ');
mSoftText.mValue.Append(' ');
}
}
MOZ_LOG(sInlineSpellWordUtilLog, LogLevel::Debug,
("%s: got DOM string: %s", __FUNCTION__,
NS_ConvertUTF16toUTF8(mSoftText).get()));
NS_ConvertUTF16toUTF8(mSoftText.mValue).get()));
}
auto mozInlineSpellWordUtil::BuildRealWords() const
-> Result<RealWords, nsresult> {
// This is pretty simple. We just have to walk mSoftText, tokenizing it
// This is pretty simple. We just have to walk mSoftText.mValue, tokenizing it
// into "real words".
// We do an outer traversal of words delimited by IsDOMWordSeparator, calling
// SplitDOMWordAndAppendTo on each of those DOM words
int32_t wordStart = -1;
RealWords realWords;
for (int32_t i = 0; i < int32_t(mSoftText.Length()); ++i) {
if (IsDOMWordSeparator(mSoftText.CharAt(i))) {
for (int32_t i = 0; i < int32_t(mSoftText.mValue.Length()); ++i) {
if (IsDOMWordSeparator(mSoftText.mValue.CharAt(i))) {
if (wordStart >= 0) {
nsresult rv = SplitDOMWordAndAppendTo(wordStart, i, realWords);
if (NS_FAILED(rv)) {
@ -921,8 +922,8 @@ auto mozInlineSpellWordUtil::BuildRealWords() const
}
}
if (wordStart >= 0) {
nsresult rv =
SplitDOMWordAndAppendTo(wordStart, mSoftText.Length(), realWords);
nsresult rv = SplitDOMWordAndAppendTo(wordStart, mSoftText.mValue.Length(),
realWords);
if (NS_FAILED(rv)) {
return Err(rv);
}
@ -931,7 +932,7 @@ auto mozInlineSpellWordUtil::BuildRealWords() const
return realWords;
}
/*********** DOM/realwords<->mSoftText mapping functions ************/
/*********** DOM/realwords<->mSoftText.mValue mapping functions ************/
int32_t mozInlineSpellWordUtil::MapDOMPositionToSoftTextOffset(
NodeOffset aNodeOffset) const {
@ -1101,7 +1102,7 @@ int32_t mozInlineSpellWordUtil::FindRealWordContaining(
nsresult mozInlineSpellWordUtil::SplitDOMWordAndAppendTo(
int32_t aStart, int32_t aEnd, nsTArray<RealWord>& aRealWords) const {
nsDependentSubstring targetText(mSoftText, aStart, aEnd - aStart);
nsDependentSubstring targetText(mSoftText.mValue, aStart, aEnd - aStart);
WordSplitState<nsDependentSubstring> state(targetText);
state.mCurCharClass = state.ClassifyCharacter(0, true);

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

@ -120,6 +120,13 @@ class MOZ_STACK_CLASS mozInlineSpellWordUtil {
const nsINode* GetRootNode() const { return mRootNode; }
private:
struct SoftText {
// DOM text covering the soft range, with newlines added at block boundaries
nsString mValue;
};
SoftText mSoftText;
mozInlineSpellWordUtil(mozilla::dom::Document& aDocument,
bool aIsContentEditableOrDesignMode, nsINode& aRootNode
@ -141,8 +148,6 @@ class MOZ_STACK_CLASS mozInlineSpellWordUtil {
NodeOffset mSoftBegin;
NodeOffset mSoftEnd;
// DOM text covering the soft range, with newlines added at block boundaries
nsString mSoftText;
// A list of where we extracted text from, ordered by mSoftTextOffset. A given
// DOM node appears at most once in this list.
struct DOMTextMapping {
@ -158,7 +163,7 @@ class MOZ_STACK_CLASS mozInlineSpellWordUtil {
};
nsTArray<DOMTextMapping> mSoftTextDOMMapping;
// A list of the "real words" in mSoftText, ordered by mSoftTextOffset
// A list of the "real words" in mSoftText.mValue, ordered by mSoftTextOffset
struct RealWord {
int32_t mSoftTextOffset;
uint32_t mLength : 31;
@ -186,11 +191,11 @@ class MOZ_STACK_CLASS mozInlineSpellWordUtil {
nsresult EnsureWords();
int32_t MapDOMPositionToSoftTextOffset(NodeOffset aNodeOffset) const;
// Map an offset into mSoftText to a DOM position. Note that two DOM positions
// can map to the same mSoftText offset, e.g. given nodes A=aaaa and B=bbbb
// forming aaaabbbb, (A,4) and (B,0) give the same string offset. So,
// aHintBefore controls which position we return ... if aHint is eEnd
// then the position indicates the END of a range so we return (A,4).
// Map an offset into mSoftText.mValue to a DOM position. Note that two DOM
// positions can map to the same mSoftText.mValue offset, e.g. given nodes
// A=aaaa and B=bbbb forming aaaabbbb, (A,4) and (B,0) give the same string
// offset. So, aHintBefore controls which position we return ... if aHint is
// eEnd then the position indicates the END of a range so we return (A,4).
// Otherwise the position indicates the START of a range so we return (B,0).
enum DOMMapHint { HINT_BEGIN, HINT_END };
NodeOffset MapSoftTextOffsetToDOMPosition(int32_t aSoftTextOffset,
@ -208,7 +213,7 @@ class MOZ_STACK_CLASS mozInlineSpellWordUtil {
int32_t FindRealWordContaining(int32_t aSoftTextOffset, DOMMapHint aHint,
bool aSearchForward) const;
// build mSoftText and mSoftTextDOMMapping and adjust mSoftBegin.
// build mSoftText.mValue and mSoftTextDOMMapping and adjust mSoftBegin.
void AdjustSoftBeginAndBuildSoftText();
mozilla::Result<RealWords, nsresult> BuildRealWords() const;