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