Bug 1397577 - Avoid using GetChildAt() in HTMLEditor::GetSelectedOrParentTableElement(); r=smaug
authorEhsan Akhgari <ehsan@mozilla.com>
Wed, 06 Sep 2017 23:07:43 -0400
changeset 429054 98982ca120620852927ac84d8ff70d4b2edb3b4e
parent 429053 90c6c33b5604580e21d1f69116a537d3e63028a1
child 429055 52433c05f30628771e15d72f87f51c9383f8372f
push id7761
push userjlund@mozilla.com
push dateFri, 15 Sep 2017 00:19:52 +0000
treeherdermozilla-beta@c38455951db4 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmaug
bugs1397577
milestone57.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1397577 - Avoid using GetChildAt() in HTMLEditor::GetSelectedOrParentTableElement(); r=smaug
dom/base/Selection.cpp
dom/base/Selection.h
editor/libeditor/HTMLTableEditor.cpp
--- a/dom/base/Selection.cpp
+++ b/dom/base/Selection.cpp
@@ -918,16 +918,29 @@ Selection::FocusOffset()
 
   if (GetDirection() == eDirNext){
     return mAnchorFocusRange->EndOffset();
   }
 
   return mAnchorFocusRange->StartOffset();
 }
 
+nsIContent*
+Selection::GetChildAtAnchorOffset()
+{
+  if (!mAnchorFocusRange)
+    return nullptr;
+
+  if (GetDirection() == eDirNext) {
+    return mAnchorFocusRange->GetChildAtStartOffset();
+  }
+
+  return mAnchorFocusRange->GetChildAtEndOffset();
+}
+
 static nsresult
 CompareToRangeStart(nsINode* aCompareNode, int32_t aCompareOffset,
                     nsRange* aRange, int32_t* aCmp)
 {
   nsINode* start = aRange->GetStartContainer();
   NS_ENSURE_STATE(aCompareNode && start);
   // If the nodes that we're comparing are not in the same document,
   // assume that aCompareNode will fall at the end of the ranges.
--- a/dom/base/Selection.h
+++ b/dom/base/Selection.h
@@ -166,16 +166,18 @@ public:
   JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
 
   // WebIDL methods
   nsINode*     GetAnchorNode();
   uint32_t     AnchorOffset();
   nsINode*     GetFocusNode();
   uint32_t     FocusOffset();
 
+  nsIContent*  GetChildAtAnchorOffset();
+
   /*
    * IsCollapsed -- is the whole selection just one point, or unset?
    */
   bool IsCollapsed() const
   {
     uint32_t cnt = mRanges.Length();
     if (cnt == 0) {
       return true;
--- a/editor/libeditor/HTMLTableEditor.cpp
+++ b/editor/libeditor/HTMLTableEditor.cpp
@@ -3196,18 +3196,17 @@ HTMLEditor::GetSelectedOrParentTableElem
   } else {
     nsCOMPtr<nsINode> anchorNode = selection->GetAnchorNode();
     if (NS_WARN_IF(!anchorNode)) {
       return NS_ERROR_FAILURE;
     }
 
     // Get child of anchor node, if exists
     if (anchorNode->HasChildNodes()) {
-      int32_t anchorOffset = selection->AnchorOffset();
-      nsINode* selectedNode = anchorNode->GetChildAt(anchorOffset);
+      nsINode* selectedNode = selection->GetChildAtAnchorOffset();
       if (!selectedNode) {
         selectedNode = anchorNode;
         // If anchor doesn't have a child, we can't be selecting a table element,
         //  so don't do the following:
       } else {
         if (selectedNode->IsHTMLElement(nsGkAtoms::td)) {
           tableOrCellElement = do_QueryInterface(selectedNode);
           aTagName = tdName;