Skip to content
This repository was archived by the owner on Jul 9, 2025. It is now read-only.

Commit 825170c

Browse files
Bug 1734771 - part 8: Make ConvertToXPOffset() take const Text& instead of nsIContent* r=m_kato
Differential Revision: https://phabricator.services.mozilla.com/D128145
1 parent 83ee827 commit 825170c

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

dom/events/ContentEventHandler.cpp

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -542,20 +542,20 @@ static uint32_t CountNewlinesInXPLength(const Text& aTextNode,
542542
return newlines;
543543
}
544544

545-
static uint32_t CountNewlinesInNativeLength(Text* aText,
545+
static uint32_t CountNewlinesInNativeLength(const Text& aTextNode,
546546
uint32_t aNativeLength) {
547-
const nsTextFragment* text = &aText->TextFragment();
547+
const nsTextFragment& textFragment = aTextNode.TextFragment();
548548
// For automated tests, we should abort on debug build.
549-
MOZ_ASSERT(
550-
(aNativeLength == UINT32_MAX || aNativeLength <= text->GetLength() * 2),
551-
"aNativeLength is unexpected value");
552-
const uint32_t xpLength = text->GetLength();
549+
MOZ_ASSERT((aNativeLength == UINT32_MAX ||
550+
aNativeLength <= textFragment.GetLength() * 2),
551+
"aNativeLength is unexpected value");
552+
const uint32_t xpLength = textFragment.GetLength();
553553
uint32_t newlines = 0;
554554
for (uint32_t i = 0, nativeOffset = 0;
555555
i < xpLength && nativeOffset < aNativeLength; ++i, ++nativeOffset) {
556556
// For automated tests, we should abort on debug build.
557-
MOZ_ASSERT(i < text->GetLength(), "i is out-of-bounds");
558-
if (text->CharAt(i) == '\n') {
557+
MOZ_ASSERT(i < xpLength, "i is out-of-bounds");
558+
if (textFragment.CharAt(i) == '\n') {
559559
++newlines;
560560
++nativeOffset;
561561
}
@@ -615,14 +615,13 @@ uint32_t ContentEventHandler::GetTextLength(const Text& aTextNode,
615615
return length + textLengthDifference;
616616
}
617617

618-
static uint32_t ConvertToXPOffset(nsIContent* aContent,
618+
static uint32_t ConvertToXPOffset(const Text& aTextNode,
619619
uint32_t aNativeOffset) {
620620
#if defined(XP_WIN)
621621
// On Windows, the length of a native newline ("\r\n") is twice the length of
622622
// the XP newline ("\n"), so XP offset is equal to the length of the native
623623
// offset minus the number of newlines encountered in the string.
624-
return aNativeOffset -
625-
CountNewlinesInNativeLength(aContent->AsText(), aNativeOffset);
624+
return aNativeOffset - CountNewlinesInNativeLength(aTextNode, aNativeOffset);
626625
#else
627626
// On other platforms, the native and XP newlines are the same.
628627
return aNativeOffset;
@@ -1057,16 +1056,16 @@ nsresult ContentEventHandler::SetRawRangeFromFlatTextOffset(
10571056
if (!startSet && aOffset <= offset + textLength) {
10581057
nsINode* startNode = nullptr;
10591058
int32_t startNodeOffset = -1;
1060-
if (content->IsText()) {
1059+
if (Text* textNode = Text::FromNode(content)) {
10611060
// Rule #1.1: [textNode or text[Node or textNode[
10621061
uint32_t xpOffset = aOffset - offset;
10631062
if (aLineBreakType == LINE_BREAK_TYPE_NATIVE) {
1064-
xpOffset = ConvertToXPOffset(content, xpOffset);
1063+
xpOffset = ConvertToXPOffset(*textNode, xpOffset);
10651064
}
10661065

10671066
if (aExpandToClusterBoundaries) {
10681067
uint32_t oldXPOffset = xpOffset;
1069-
rv = ExpandToClusterBoundary(content, false, &xpOffset);
1068+
nsresult rv = ExpandToClusterBoundary(textNode, false, &xpOffset);
10701069
if (NS_WARN_IF(NS_FAILED(rv))) {
10711070
return rv;
10721071
}
@@ -1075,7 +1074,7 @@ nsresult ContentEventHandler::SetRawRangeFromFlatTextOffset(
10751074
*aNewOffset -= (oldXPOffset - xpOffset);
10761075
}
10771076
}
1078-
startNode = content;
1077+
startNode = textNode;
10791078
startNodeOffset = static_cast<int32_t>(xpOffset);
10801079
} else if (aOffset < offset + textLength) {
10811080
// Rule #1.2 [<element>
@@ -1128,14 +1127,14 @@ nsresult ContentEventHandler::SetRawRangeFromFlatTextOffset(
11281127
// range.
11291128
if (endOffset <= offset + textLength) {
11301129
MOZ_ASSERT(startSet, "The start of the range should've been set already");
1131-
if (content->IsText()) {
1130+
if (Text* textNode = Text::FromNode(content)) {
11321131
// Rule #2.1: ]textNode or text]Node or textNode]
11331132
uint32_t xpOffset = endOffset - offset;
11341133
if (aLineBreakType == LINE_BREAK_TYPE_NATIVE) {
1135-
uint32_t xpOffsetCurrent = ConvertToXPOffset(content, xpOffset);
1134+
uint32_t xpOffsetCurrent = ConvertToXPOffset(*textNode, xpOffset);
11361135
if (xpOffset && GetBRLength(aLineBreakType) > 1) {
11371136
MOZ_ASSERT(GetBRLength(aLineBreakType) == 2);
1138-
uint32_t xpOffsetPre = ConvertToXPOffset(content, xpOffset - 1);
1137+
uint32_t xpOffsetPre = ConvertToXPOffset(*textNode, xpOffset - 1);
11391138
// If previous character's XP offset is same as current character's,
11401139
// it means that the end offset is between \r and \n. So, the
11411140
// range end should be after the \n.
@@ -1147,13 +1146,13 @@ nsresult ContentEventHandler::SetRawRangeFromFlatTextOffset(
11471146
}
11481147
}
11491148
if (aExpandToClusterBoundaries) {
1150-
rv = ExpandToClusterBoundary(content, true, &xpOffset);
1149+
nsresult rv = ExpandToClusterBoundary(textNode, true, &xpOffset);
11511150
if (NS_WARN_IF(NS_FAILED(rv))) {
11521151
return rv;
11531152
}
11541153
}
11551154
NS_ASSERTION(xpOffset <= INT32_MAX, "The end node offset is too large");
1156-
rv = aRawRange->SetEnd(content, xpOffset);
1155+
nsresult rv = aRawRange->SetEnd(textNode, xpOffset);
11571156
if (NS_WARN_IF(NS_FAILED(rv))) {
11581157
return rv;
11591158
}

0 commit comments

Comments
 (0)