Bug 1347433 part.1 Separate TextRange offset and length adjustment to AdjustRange() r=m_kato

First of all, replacing native line breakers with XP line breakers needs to adjust offset and length of each TextRange.  Therefore, we cannot just duplicate the code into TextEventDispatcher::PendingComposition::Flush().

For creating a new method to replace the native line breakers in PendingComposition::mString, we should separate range adjustment code to a static method, AdjustRange(), because we cannot use for loop simply because we need to adjust both mClauses and mCaret.

MozReview-Commit-ID: 5ycsN8EAs45

--HG--
extra : rebase_source : 0ef667669c9027958a0a955f4b883f70be89cbb3
This commit is contained in:
Masayuki Nakano 2017-03-15 18:51:32 +09:00
parent a9a15a1807
commit aa1ef41139
2 changed files with 41 additions and 19 deletions

View File

@ -680,27 +680,9 @@ TextEventDispatcher::PendingComposition::Set(const nsAString& aString,
}
// Adjust offsets in the ranges for XP linefeed character (only \n).
// XXX Following code is the safest approach. However, it wastes performance.
// For ensuring the clauses do not overlap each other, we should redesign
// TextRange later.
for (uint32_t i = 0; i < aRanges->Length(); ++i) {
TextRange range = aRanges->ElementAt(i);
TextRange nativeRange = range;
if (nativeRange.mStartOffset > 0) {
nsAutoString preText(Substring(aString, 0, nativeRange.mStartOffset));
preText.ReplaceSubstring(NS_LITERAL_STRING("\r\n"),
NS_LITERAL_STRING("\n"));
range.mStartOffset = preText.Length();
}
if (nativeRange.Length() == 0) {
range.mEndOffset = range.mStartOffset;
} else {
nsAutoString clause(
Substring(aString, nativeRange.mStartOffset, nativeRange.Length()));
clause.ReplaceSubstring(NS_LITERAL_STRING("\r\n"),
NS_LITERAL_STRING("\n"));
range.mEndOffset = range.mStartOffset + clause.Length();
}
AdjustRange(range, aString);
if (range.mRangeType == TextRangeType::eCaret) {
mCaret = range;
} else {
@ -711,6 +693,36 @@ TextEventDispatcher::PendingComposition::Set(const nsAString& aString,
return NS_OK;
}
// static
void
TextEventDispatcher::PendingComposition::AdjustRange(
TextRange& aRange,
const nsAString& aNativeString)
{
TextRange nativeRange = aRange;
// XXX Following code wastes runtime cost because this causes computing
// mStartOffset for each clause from the start of composition string.
// If we'd make TextRange have only its length, we don't need to do
// this. However, this must not be so serious problem because
// composition string is usually short and separated as a few clauses.
if (nativeRange.mStartOffset > 0) {
nsAutoString preText(
Substring(aNativeString, 0, nativeRange.mStartOffset));
preText.ReplaceSubstring(NS_LITERAL_STRING("\r\n"),
NS_LITERAL_STRING("\n"));
aRange.mStartOffset = preText.Length();
}
if (nativeRange.Length() == 0) {
aRange.mEndOffset = aRange.mStartOffset;
} else {
nsAutoString clause(
Substring(aNativeString, nativeRange.mStartOffset, nativeRange.Length()));
clause.ReplaceSubstring(NS_LITERAL_STRING("\r\n"),
NS_LITERAL_STRING("\n"));
aRange.mEndOffset = aRange.mStartOffset + clause.Length();
}
}
nsresult
TextEventDispatcher::PendingComposition::Flush(
TextEventDispatcher* aDispatcher,

View File

@ -332,6 +332,16 @@ private:
TextRange mCaret;
void EnsureClauseArray();
/**
* AdjustRange() adjusts aRange as in the string with XP line breakers.
*
* @param aRange The reference to a range in aNativeString.
* This will be modified.
* @param aNativeString The string with native line breakers.
* This may include "\r\n" and/or "\r".
*/
static void AdjustRange(TextRange& aRange, const nsAString& aNativeString);
};
PendingComposition mPendingComposition;