Bug 1343275 part 2. Use slightly faster APIs for getting the information we want out of a selection when converting it to a text-control selection range. r=ehsan

MozReview-Commit-ID: 6NRoWNqb1pC
This commit is contained in:
Boris Zbarsky 2017-03-09 14:44:17 -05:00
Родитель de5686a6c3
Коммит 9ea0de6975
1 изменённых файлов: 24 добавлений и 20 удалений

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

@ -7063,7 +7063,11 @@ nsContentUtils::GetSelectionInTextControl(Selection* aSelection,
{
MOZ_ASSERT(aSelection && aRoot);
if (!aSelection->RangeCount()) {
// We don't care which end of this selection is anchor and which is focus. In
// fact, we explicitly want to know which is the _start_ and which is the
// _end_, not anchor vs focus.
const nsRange* range = aSelection->GetAnchorFocusRange();
if (!range) {
// Nothing selected
aOutStartOffset = aOutEndOffset = 0;
return;
@ -7071,10 +7075,10 @@ nsContentUtils::GetSelectionInTextControl(Selection* aSelection,
// All the node pointers here are raw pointers for performance. We shouldn't
// be doing anything in this function that invalidates the node tree.
nsINode* anchorNode = aSelection->GetAnchorNode();
uint32_t anchorOffset = aSelection->AnchorOffset();
nsINode* focusNode = aSelection->GetFocusNode();
uint32_t focusOffset = aSelection->FocusOffset();
nsINode* startParent = range->GetStartParent();
uint32_t startOffset = range->StartOffset();
nsINode* endParent = range->GetEndParent();
uint32_t endOffset = range->EndOffset();
// We have at most two children, consisting of an optional text node followed
// by an optional <br>.
@ -7082,10 +7086,10 @@ nsContentUtils::GetSelectionInTextControl(Selection* aSelection,
nsIContent* firstChild = aRoot->GetFirstChild();
#ifdef DEBUG
nsCOMPtr<nsIContent> lastChild = aRoot->GetLastChild();
NS_ASSERTION(anchorNode == aRoot || anchorNode == firstChild ||
anchorNode == lastChild, "Unexpected anchorNode");
NS_ASSERTION(focusNode == aRoot || focusNode == firstChild ||
focusNode == lastChild, "Unexpected focusNode");
NS_ASSERTION(startParent == aRoot || startParent == firstChild ||
startParent == lastChild, "Unexpected startParent");
NS_ASSERTION(endParent == aRoot || endParent == firstChild ||
endParent == lastChild, "Unexpected endParent");
// firstChild is either text or a <br> (hence an element).
MOZ_ASSERT_IF(firstChild,
firstChild->IsNodeOfType(nsINode::eTEXT) || firstChild->IsElement());
@ -7094,25 +7098,25 @@ nsContentUtils::GetSelectionInTextControl(Selection* aSelection,
// non-virtual.
if (!firstChild || firstChild->IsElement()) {
// No text node, so everything is 0
anchorOffset = focusOffset = 0;
startOffset = endOffset = 0;
} else {
// First child is text. If the anchor/focus is already in the text node,
// First child is text. If the start/end is already in the text node,
// or the start of the root node, no change needed. If it's in the root
// node but not the start, or in the trailing <br>, we need to set the
// offset to the end.
if ((anchorNode == aRoot && anchorOffset != 0) ||
(anchorNode != aRoot && anchorNode != firstChild)) {
anchorOffset = firstChild->Length();
if ((startParent == aRoot && startOffset != 0) ||
(startParent != aRoot && startParent != firstChild)) {
startOffset = firstChild->Length();
}
if ((focusNode == aRoot && focusOffset != 0) ||
(focusNode != aRoot && focusNode != firstChild)) {
focusOffset = firstChild->Length();
if ((endParent == aRoot && endOffset != 0) ||
(endParent != aRoot && endParent != firstChild)) {
endOffset = firstChild->Length();
}
}
// Make sure aOutStartOffset <= aOutEndOffset.
aOutStartOffset = std::min(anchorOffset, focusOffset);
aOutEndOffset = std::max(anchorOffset, focusOffset);
MOZ_ASSERT(startOffset <= endOffset);
aOutStartOffset = startOffset;
aOutEndOffset = endOffset;
}