Bug 1432944 part 2. Make nsIHTMLEditor.getSelectedElement return nsISupports. r=m_kato
authorBoris Zbarsky <bzbarsky@mit.edu>
Mon, 29 Jan 2018 23:27:59 -0500
changeset 401399 15f093275a0df22eca0c4692c1fbc02fa783d593
parent 401398 93c1d149d757d49da158cc64f6eb21fb3406da18
child 401400 84d651460f98f1e1a973393a2adf1d4cdbc4fa7b
push id33343
push userrgurzau@mozilla.com
push dateTue, 30 Jan 2018 10:03:41 +0000
treeherdermozilla-central@9746e0a0a81c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersm_kato
bugs1432944
milestone60.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 1432944 part 2. Make nsIHTMLEditor.getSelectedElement return nsISupports. r=m_kato The only C++ caller immediately QIs the result anyway, and the exact type returned doesn't matter for JS callers because the return values are DOM objects. This makes it simpler to rejigger the internals to work with nsINode and Element and whatnot. MozReview-Commit-ID: 863IDGECqYY
editor/libeditor/HTMLEditor.cpp
editor/libeditor/HTMLEditor.h
editor/nsIHTMLEditor.idl
--- a/editor/libeditor/HTMLEditor.cpp
+++ b/editor/libeditor/HTMLEditor.cpp
@@ -2329,17 +2329,17 @@ HTMLEditor::GetElementOrParentByTagName(
   }
 
   ret.forget(aReturn);
   return NS_OK;
 }
 
 NS_IMETHODIMP
 HTMLEditor::GetSelectedElement(const nsAString& aTagName,
-                               nsIDOMElement** aReturn)
+                               nsISupports** aReturn)
 {
   NS_ENSURE_TRUE(aReturn , NS_ERROR_NULL_POINTER);
 
   // default is null - no element found
   *aReturn = nullptr;
 
   // First look for a single element in selection
   RefPtr<Selection> selection = GetSelection();
@@ -2498,16 +2498,25 @@ HTMLEditor::GetSelectedElement(const nsA
   if (selectedElement) {
     // Getters must addref
     NS_ADDREF(*aReturn);
   }
   return NS_OK;
 }
 
 already_AddRefed<Element>
+HTMLEditor::GetSelectedElement(const nsAString& aTagName)
+{
+  nsCOMPtr<nsISupports> domElement;
+  GetSelectedElement(aTagName, getter_AddRefs(domElement));
+  nsCOMPtr<Element> element = do_QueryInterface(domElement);
+  return element.forget();
+}
+
+already_AddRefed<Element>
 HTMLEditor::CreateElementWithDefaults(const nsAString& aTagName)
 {
   MOZ_ASSERT(!aTagName.IsEmpty());
 
   nsAutoString tagName(aTagName);
   ToLowerCase(tagName);
   nsAutoString realTagName;
 
@@ -4453,23 +4462,18 @@ HTMLEditor::GetSelectionContainer()
       RefPtr<nsRange> range = selection->GetRangeAt(0);
 
       nsCOMPtr<nsINode> startContainer = range->GetStartContainer();
       int32_t startOffset = range->StartOffset();
       nsCOMPtr<nsINode> endContainer = range->GetEndContainer();
       int32_t endOffset = range->EndOffset();
 
       if (startContainer == endContainer && startOffset + 1 == endOffset) {
-        nsCOMPtr<nsIDOMElement> focusElement;
-        nsresult rv = GetSelectedElement(EmptyString(),
-                                         getter_AddRefs(focusElement));
-        NS_ENSURE_SUCCESS(rv, nullptr);
-        if (focusElement) {
-          focusNode = do_QueryInterface(focusElement);
-        }
+        MOZ_ASSERT(!focusNode, "How did it get set already?");
+        focusNode = GetSelectedElement(EmptyString());
       }
       if (!focusNode) {
         focusNode = range->GetCommonAncestor();
       }
     } else {
       for (int32_t i = 0; i < rangeCount; i++) {
         RefPtr<nsRange> range = selection->GetRangeAt(i);
 
--- a/editor/libeditor/HTMLEditor.h
+++ b/editor/libeditor/HTMLEditor.h
@@ -988,16 +988,22 @@ protected:
    *                    return value and can use the
    *                    AutoSelectionSetterAfterTableEdit stack-based object to
    *                    insure we reset the caret in a table-editing method.
    */
   void SetSelectionAfterTableEdit(nsIDOMElement* aTable,
                                   int32_t aRow, int32_t aCol,
                                   int32_t aDirection, bool aSelected);
 
+  /**
+   * A more C++-friendly version of nsIHTMLEditor::GetSelectedElement
+   * that just returns null on errors.
+   */
+  already_AddRefed<dom::Element> GetSelectedElement(const nsAString& aTagName);
+
 protected:
   RefPtr<TypeInState> mTypeInState;
   RefPtr<ComposerCommandsUpdater> mComposerCommandsUpdater;
 
   bool mCRInParagraphCreatesParagraph;
 
   bool mCSSAware;
   UniquePtr<CSSEditUtils> mCSSEditUtils;
--- a/editor/nsIHTMLEditor.idl
+++ b/editor/nsIHTMLEditor.idl
@@ -356,34 +356,37 @@ interface nsIHTMLEditor : nsISupports
    *     If it is null, the anchor node of the current selection is used.
    * @return         NS_EDITOR_ELEMENT_NOT_FOUND if an element is not found
    *                 (passes NS_SUCCEEDED macro)
    */
   nsIDOMElement getElementOrParentByTagName(in AString aTagName,
                                             in nsIDOMNode aNode);
 
   /**
-   * Return an element only if it is the only node selected,
-   *    such as an image, horizontal rule, etc.
+   * Return an Element only if it is the only node selected,
+   *    such as an image, horizontal rule, etc.  The return type is
+   *    nsISupports for implementation convenience; the returned object,
+   *    if not null, is always a DOM Element.
+   *
    * The exception is a link, which is more like a text attribute:
    *    The Anchor tag is returned if the selection is within the textnode(s)
    *    that are children of the "A" node.
    *    This could be a collapsed selection, i.e., a caret
    *    within the link text.
    *
    * @param aTagName  The HTML tagname or and empty string
    *       to get any element (but only if it is the only element selected)
    *    Special input values for Links and Named anchors:
    *    Use "href" to get a link node
    *      (an "A" tag with the "href" attribute set)
    *    Use "anchor" or "namedanchor" to get a named anchor node
    *      (an "A" tag with the "name" attribute set)
    * @return the element as described above
    */
-  nsIDOMElement getSelectedElement(in AString aTagName);
+  nsISupports getSelectedElement(in AString aTagName);
 
   /**
    * Replace all children of <HEAD> with string of HTML source
    */
   void replaceHeadContentsWithHTML(in AString aSourceToInsert);
 
   /**
    * Return a new element with default attribute values