Bug 1404106 - Avoid using nsINode::GetChildAt() in HTMLEditor::GetSelectedElement(); r=masayuki
authorEhsan Akhgari <ehsan@mozilla.com>
Thu, 28 Sep 2017 16:19:49 -0400
changeset 426358 0a4fe014097f7dd59af9d042f0f7ea3acf712a7c
parent 426357 7abc24e252c022176c3219e65991b38a21301d2e
child 426359 2542bb01e23dfeec26d6ed32ec527b81b6b5eae4
push id97
push userfmarier@mozilla.com
push dateSat, 14 Oct 2017 01:12:59 +0000
reviewersmasayuki
bugs1404106
milestone58.0a1
Bug 1404106 - Avoid using nsINode::GetChildAt() in HTMLEditor::GetSelectedElement(); r=masayuki
editor/libeditor/HTMLEditor.cpp
--- a/editor/libeditor/HTMLEditor.cpp
+++ b/editor/libeditor/HTMLEditor.cpp
@@ -2344,26 +2344,25 @@ HTMLEditor::GetSelectedElement(const nsA
   bool isLinkTag = IsLinkTag(TagName);
   bool isNamedAnchorTag = IsNamedAnchorTag(TagName);
 
   nsCOMPtr<nsIDOMElement> selectedElement;
   RefPtr<nsRange> range = selection->GetRangeAt(0);
   NS_ENSURE_STATE(range);
 
   nsCOMPtr<nsINode> startContainer = range->GetStartContainer();
-  uint32_t startOffset = range->StartOffset();
+  nsIContent* startNode = range->GetChildAtStartOffset();
 
   nsCOMPtr<nsINode> endContainer = range->GetEndContainer();
-  uint32_t endOffset = range->EndOffset();
+  nsIContent* endNode = range->GetChildAtEndOffset();
 
   // Optimization for a single selected element
   if (startContainer && startContainer == endContainer &&
-      endOffset - startOffset == 1) {
-    nsCOMPtr<nsINode> selectedNode =
-      startContainer->GetChildAt(static_cast<int32_t>(startOffset));
+      startNode && endNode && startNode->GetNextSibling() == endNode) {
+    nsCOMPtr<nsINode> selectedNode = startNode;
     if (selectedNode) {
       selectedNode->AsDOMNode()->GetNodeName(domTagName);
       ToLowerCase(domTagName);
 
       // Test for appropriate node type requested
       if (anyTag || (TagName == domTagName) ||
           (isLinkTag && HTMLEditUtils::IsLink(selectedNode)) ||
           (isNamedAnchorTag && HTMLEditUtils::IsNamedAnchor(selectedNode))) {