Bug 1455674 part 3. Stop using nsIDOMElement in nsITableEditor. r=masayuki
authorBoris Zbarsky <bzbarsky@mit.edu>
Thu, 26 Apr 2018 23:35:18 -0400
changeset 472083 667c981474f823ad581668657a52dbc9d40b845b
parent 472082 52e2af8c5e62675514fd20ba6da493cf761ddfd9
child 472084 15071a2629d9add643a5122b74dbb58d9e490c20
push id1728
push userjlund@mozilla.com
push dateMon, 18 Jun 2018 21:12:27 +0000
treeherdermozilla-release@c296fde26f5f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmasayuki
bugs1455674
milestone61.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 1455674 part 3. Stop using nsIDOMElement in nsITableEditor. r=masayuki The patch is a bit large because all these methods except SswitchTableCellHeaderType call each other; doing it piecemeal would have required introducing, then removing, a bunch of QIs.
editor/libeditor/EditorBase.cpp
editor/libeditor/EditorBase.h
editor/libeditor/HTMLEditRules.cpp
editor/libeditor/HTMLEditor.cpp
editor/libeditor/HTMLEditor.h
editor/libeditor/HTMLEditorDataTransfer.cpp
editor/libeditor/HTMLTableEditor.cpp
editor/nsITableEditor.idl
--- a/editor/libeditor/EditorBase.cpp
+++ b/editor/libeditor/EditorBase.cpp
@@ -4486,35 +4486,34 @@ EditorBase::CreateTxnForDeleteRange(nsRa
   if (NS_WARN_IF(!deleteNodeTransaction)) {
     return nullptr;
   }
   selectedNode.forget(aRemovingNode);
   return deleteNodeTransaction.forget();
 }
 
 nsresult
-EditorBase::CreateRange(nsIDOMNode* aStartContainer,
+EditorBase::CreateRange(nsINode* aStartContainer,
                         int32_t aStartOffset,
-                        nsIDOMNode* aEndContainer,
+                        nsINode* aEndContainer,
                         int32_t aEndOffset,
                         nsRange** aRange)
 {
   return nsRange::CreateRange(aStartContainer, aStartOffset,
                               aEndContainer, aEndOffset, aRange);
 }
 
 nsresult
-EditorBase::AppendNodeToSelectionAsRange(nsIDOMNode* aNode)
+EditorBase::AppendNodeToSelectionAsRange(nsINode* aNode)
 {
   NS_ENSURE_TRUE(aNode, NS_ERROR_NULL_POINTER);
   RefPtr<Selection> selection = GetSelection();
   NS_ENSURE_TRUE(selection, NS_ERROR_FAILURE);
 
-  nsCOMPtr<nsINode> node = do_QueryInterface(aNode);
-  nsCOMPtr<nsIDOMNode> parentNode = do_QueryInterface(node->GetParentNode());
+  nsCOMPtr<nsINode> parentNode = aNode->GetParentNode();
   NS_ENSURE_TRUE(parentNode, NS_ERROR_NULL_POINTER);
 
   int32_t offset = GetChildOffset(aNode, parentNode);
 
   RefPtr<nsRange> range;
   nsresult rv = CreateRange(parentNode, offset, parentNode, offset + 1,
                             getter_AddRefs(range));
   NS_ENSURE_SUCCESS(rv, rv);
--- a/editor/libeditor/EditorBase.h
+++ b/editor/libeditor/EditorBase.h
@@ -1446,25 +1446,25 @@ public:
    * CollapseSelectionToEnd() collapses the selection to the end of the editor.
    */
   nsresult CollapseSelectionToEnd(Selection* aSelection);
 
   /**
    * Helpers to add a node to the selection.
    * Used by table cell selection methods.
    */
-  nsresult CreateRange(nsIDOMNode* aStartContainer, int32_t aStartOffset,
-                       nsIDOMNode* aEndContainer, int32_t aEndOffset,
+  nsresult CreateRange(nsINode* aStartContainer, int32_t aStartOffset,
+                       nsINode* aEndContainer, int32_t aEndOffset,
                        nsRange** aRange);
 
   /**
    * Creates a range with just the supplied node and appends that to the
    * selection.
    */
-  nsresult AppendNodeToSelectionAsRange(nsIDOMNode *aNode);
+  nsresult AppendNodeToSelectionAsRange(nsINode* aNode);
 
   /**
    * When you are using AppendNodeToSelectionAsRange(), call this first to
    * start a new selection.
    */
   nsresult ClearSelection();
 
   static bool IsPreformatted(nsINode* aNode);
--- a/editor/libeditor/HTMLEditRules.cpp
+++ b/editor/libeditor/HTMLEditRules.cpp
@@ -2206,17 +2206,17 @@ HTMLEditRules::WillDeleteSelection(Selec
 
   // If there is only bogus content, cancel the operation
   if (mBogusNode) {
     *aCancel = true;
     return NS_OK;
   }
 
   // First check for table selection mode.  If so, hand off to table editor.
-  nsCOMPtr<nsIDOMElement> cell;
+  RefPtr<Element> cell;
   NS_ENSURE_STATE(mHTMLEditor);
   nsresult rv =
     mHTMLEditor->GetFirstSelectedCell(nullptr, getter_AddRefs(cell));
   if (NS_SUCCEEDED(rv) && cell) {
     NS_ENSURE_STATE(mHTMLEditor);
     rv = mHTMLEditor->DeleteTableCellContents();
     *aHandled = true;
     return rv;
--- a/editor/libeditor/HTMLEditor.cpp
+++ b/editor/libeditor/HTMLEditor.cpp
@@ -1096,17 +1096,17 @@ HTMLEditor::TabInTable(bool inIsShift,
     // If we haven't handled it yet, then we must have run off the end of the
     // table.  Insert a new row.
     rv = InsertTableRow(1, true);
     NS_ENSURE_SUCCESS(rv, rv);
     *outHandled = true;
     // Put selection in right place.  Use table code to get selection and index
     // to new row...
     RefPtr<Selection> selection;
-    nsCOMPtr<nsIDOMElement> tblElement, cell;
+    RefPtr<Element> tblElement, cell;
     int32_t row;
     rv = GetCellContext(getter_AddRefs(selection),
                         getter_AddRefs(tblElement),
                         getter_AddRefs(cell),
                         nullptr, nullptr,
                         &row, nullptr);
     NS_ENSURE_SUCCESS(rv, rv);
     // ...so that we can ask for first cell in that row...
@@ -1873,26 +1873,24 @@ nsresult
 HTMLEditor::GetHTMLBackgroundColorState(bool* aMixed,
                                         nsAString& aOutColor)
 {
   //TODO: We don't handle "mixed" correctly!
   NS_ENSURE_TRUE(aMixed, NS_ERROR_NULL_POINTER);
   *aMixed = false;
   aOutColor.Truncate();
 
-  nsCOMPtr<nsIDOMElement> domElement;
+  RefPtr<Element> element;
   int32_t selectedCount;
   nsAutoString tagName;
   nsresult rv = GetSelectedOrParentTableElement(tagName,
                                                 &selectedCount,
-                                                getter_AddRefs(domElement));
+                                                getter_AddRefs(element));
   NS_ENSURE_SUCCESS(rv, rv);
 
-  nsCOMPtr<dom::Element> element = do_QueryInterface(domElement);
-
   while (element) {
     // We are in a cell or selected table
     element->GetAttr(kNameSpaceID_None, nsGkAtoms::bgcolor, aOutColor);
 
     // Done if we have a color explicitly set
     if (!aOutColor.IsEmpty()) {
       return NS_OK;
     }
@@ -2763,54 +2761,45 @@ HTMLEditor::InsertLinkAroundSelection(El
 }
 
 nsresult
 HTMLEditor::SetHTMLBackgroundColorWithTransaction(const nsAString& aColor)
 {
   MOZ_ASSERT(IsInitialized(), "The HTMLEditor hasn't been initialized yet");
 
   // Find a selected or enclosing table element to set background on
-  nsCOMPtr<nsIDOMElement> domElement;
+  RefPtr<Element> element;
   int32_t selectedCount;
   nsAutoString tagName;
   nsresult rv = GetSelectedOrParentTableElement(tagName, &selectedCount,
-                                                getter_AddRefs(domElement));
+                                                getter_AddRefs(element));
   NS_ENSURE_SUCCESS(rv, rv);
 
   bool setColor = !aColor.IsEmpty();
 
-  nsCOMPtr<Element> element = nullptr;
   RefPtr<nsAtom> bgColorAtom = NS_Atomize("bgcolor");
-  if (domElement) {
+  if (element) {
     if (selectedCount > 0) {
       // Traverse all selected cells
-      nsCOMPtr<nsIDOMElement> domCell;
-      rv = GetFirstSelectedCell(nullptr, getter_AddRefs(domCell));
-      if (NS_SUCCEEDED(rv) && domCell) {
-        while (domCell) {
-          nsCOMPtr<Element> cell = do_QueryInterface(domCell);
-          if (NS_WARN_IF(!cell)) {
-            return NS_ERROR_FAILURE;
-          }
+      RefPtr<Element> cell;
+      rv = GetFirstSelectedCell(nullptr, getter_AddRefs(cell));
+      if (NS_SUCCEEDED(rv) && cell) {
+        while (cell) {
           rv = setColor ?
                  SetAttributeWithTransaction(*cell, *bgColorAtom, aColor) :
                  RemoveAttributeWithTransaction(*cell, *bgColorAtom);
           if (NS_FAILED(rv)) {
             return rv;
           }
-          GetNextSelectedCell(nullptr, getter_AddRefs(domCell));
+          GetNextSelectedCell(nullptr, getter_AddRefs(cell));
         }
         return NS_OK;
       }
     }
     // If we failed to find a cell, fall through to use originally-found element
-    element = do_QueryInterface(domElement);
-    if (NS_WARN_IF(!element)) {
-      return NS_ERROR_FAILURE;
-    }
   } else {
     // No table element -- set the background color on the body tag
     element = GetRoot();
     if (NS_WARN_IF(!element)) {
       return NS_ERROR_FAILURE;
     }
   }
   // Use the editor method that goes through the transaction system
--- a/editor/libeditor/HTMLEditor.h
+++ b/editor/libeditor/HTMLEditor.h
@@ -169,20 +169,20 @@ public:
   NS_DECL_NSIEDITORSTYLESHEETS
 
   // nsIEditorMailSupport methods
   NS_DECL_NSIEDITORMAILSUPPORT
 
   // nsITableEditor methods
   NS_DECL_NSITABLEEDITOR
 
-  nsresult GetLastCellInRow(nsIDOMNode* aRowNode,
-                            nsIDOMNode** aCellNode);
+  nsresult GetLastCellInRow(nsINode* aRowNode,
+                            nsINode** aCellNode);
 
-  nsresult GetCellFromRange(nsRange* aRange, nsIDOMElement** aCell);
+  nsresult GetCellFromRange(nsRange* aRange, Element** aCell);
 
   // Miscellaneous
 
   /**
    * This sets background on the appropriate container element (table, cell,)
    * or calls into nsTextEditor to set the page background.
    */
   nsresult SetCSSBackgroundColorWithTransaction(const nsAString& aColor);
@@ -679,107 +679,104 @@ protected:
   // Table Editing (implemented in nsTableEditor.cpp)
 
   /**
    * Insert a new cell after or before supplied aCell.
    * Optional: If aNewCell supplied, returns the newly-created cell (addref'd,
    * of course)
    * This doesn't change or use the current selection.
    */
-  nsresult InsertCell(nsIDOMElement* aCell, int32_t aRowSpan,
+  nsresult InsertCell(Element* aCell, int32_t aRowSpan,
                       int32_t aColSpan, bool aAfter, bool aIsHeader,
-                      nsIDOMElement** aNewCell);
+                      Element** aNewCell);
 
   /**
    * Helpers that don't touch the selection or do batch transactions.
    */
-  nsresult DeleteRow(nsIDOMElement* aTable, int32_t aRowIndex);
-  nsresult DeleteColumn(nsIDOMElement* aTable, int32_t aColIndex);
-  nsresult DeleteCellContents(nsIDOMElement* aCell);
+  nsresult DeleteRow(Element* aTable, int32_t aRowIndex);
+  nsresult DeleteColumn(Element* aTable, int32_t aColIndex);
+  nsresult DeleteCellContents(Element* aCell);
 
   /**
    * Move all contents from aCellToMerge into aTargetCell (append at end).
    */
-  nsresult MergeCells(nsCOMPtr<nsIDOMElement> aTargetCell,
-                      nsCOMPtr<nsIDOMElement> aCellToMerge,
+  nsresult MergeCells(RefPtr<Element> aTargetCell,
+                      RefPtr<Element> aCellToMerge,
                       bool aDeleteCellToMerge);
 
-  nsresult DeleteTable2(nsIDOMElement* aTable, Selection* aSelection);
-  nsresult SetColSpan(nsIDOMElement* aCell, int32_t aColSpan);
-  nsresult SetRowSpan(nsIDOMElement* aCell, int32_t aRowSpan);
+  nsresult DeleteTable2(Element* aTable, Selection* aSelection);
+  nsresult SetColSpan(Element* aCell, int32_t aColSpan);
+  nsresult SetRowSpan(Element* aCell, int32_t aRowSpan);
 
   /**
    * Helper used to get nsTableWrapperFrame for a table.
    */
-  nsTableWrapperFrame* GetTableFrame(nsIDOMElement* aTable);
+  nsTableWrapperFrame* GetTableFrame(Element* aTable);
 
   /**
    * Needed to do appropriate deleting when last cell or row is about to be
    * deleted.  This doesn't count cells that don't start in the given row (are
    * spanning from row above).
    */
-  int32_t GetNumberOfCellsInRow(nsIDOMElement* aTable, int32_t rowIndex);
+  int32_t GetNumberOfCellsInRow(Element* aTable, int32_t rowIndex);
 
   /**
    * Test if all cells in row or column at given index are selected.
    */
-  bool AllCellsInRowSelected(nsIDOMElement* aTable, int32_t aRowIndex,
+  bool AllCellsInRowSelected(Element* aTable, int32_t aRowIndex,
                              int32_t aNumberOfColumns);
-  bool AllCellsInColumnSelected(nsIDOMElement* aTable, int32_t aColIndex,
+  bool AllCellsInColumnSelected(Element* aTable, int32_t aColIndex,
                                 int32_t aNumberOfRows);
 
   bool IsEmptyCell(Element* aCell);
 
   /**
    * Most insert methods need to get the same basic context data.
    * Any of the pointers may be null if you don't need that datum (for more
    * efficiency).
    * Input: *aCell is a known cell,
    *        if null, cell is obtained from the anchor node of the selection.
    * Returns NS_EDITOR_ELEMENT_NOT_FOUND if cell is not found even if aCell is
    * null.
    */
-  nsresult GetCellContext(Selection** aSelection, nsIDOMElement** aTable,
-                          nsIDOMElement** aCell, nsIDOMNode** aCellParent,
+  nsresult GetCellContext(Selection** aSelection, Element** aTable,
+                          Element** aCell, nsINode** aCellParent,
                           int32_t* aCellOffset, int32_t* aRowIndex,
                           int32_t* aColIndex);
 
-  nsresult GetCellSpansAt(nsIDOMElement* aTable, int32_t aRowIndex,
+  nsresult GetCellSpansAt(Element* aTable, int32_t aRowIndex,
                           int32_t aColIndex, int32_t& aActualRowSpan,
                           int32_t& aActualColSpan);
 
-  nsresult SplitCellIntoColumns(nsIDOMElement* aTable, int32_t aRowIndex,
+  nsresult SplitCellIntoColumns(Element* aTable, int32_t aRowIndex,
                                 int32_t aColIndex, int32_t aColSpanLeft,
                                 int32_t aColSpanRight,
-                                nsIDOMElement** aNewCell);
+                                Element** aNewCell);
 
-  nsresult SplitCellIntoRows(nsIDOMElement* aTable, int32_t aRowIndex,
+  nsresult SplitCellIntoRows(Element* aTable, int32_t aRowIndex,
                              int32_t aColIndex, int32_t aRowSpanAbove,
-                             int32_t aRowSpanBelow, nsIDOMElement** aNewCell);
+                             int32_t aRowSpanBelow, Element** aNewCell);
 
-  nsresult CopyCellBackgroundColor(nsIDOMElement* destCell,
-                                   nsIDOMElement* sourceCell);
+  nsresult CopyCellBackgroundColor(Element* aDestCell,
+                                   Element* aSourceCell);
 
   /**
    * Reduce rowspan/colspan when cells span into nonexistent rows/columns.
    */
-  nsresult FixBadRowSpan(nsIDOMElement* aTable, int32_t aRowIndex,
+  nsresult FixBadRowSpan(Element* aTable, int32_t aRowIndex,
                          int32_t& aNewRowCount);
-  nsresult FixBadColSpan(nsIDOMElement* aTable, int32_t aColIndex,
+  nsresult FixBadColSpan(Element* aTable, int32_t aColIndex,
                          int32_t& aNewColCount);
 
   /**
    * Fallback method: Call this after using ClearSelection() and you
    * failed to set selection to some other content in the document.
    */
   nsresult SetSelectionAtDocumentStart(Selection* aSelection);
 
-  nsresult GetTableSize(Element* aTable,
-                        int32_t* aRowCount, int32_t* aColCount);
-
   // End of Table Editing utilities
 
   static Element* GetEnclosingTable(nsINode* aNode);
   static nsIDOMNode* GetEnclosingTable(nsIDOMNode* aNode);
 
   /**
    * Content-based query returns true if <aProperty aAttribute=aValue> effects
    * aNode.  If <aProperty aAttribute=aValue> contains aNode, but
@@ -1219,17 +1216,17 @@ protected:
    *                    row (ePreviousRow) or don't search for another cell
    *                    (aNoSearch).  If no cell is found, caret is place just
    *                    before table; and if that fails, at beginning of
    *                    document.  Thus we generally don't worry about the
    *                    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,
+  void SetSelectionAfterTableEdit(Element* 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);
--- a/editor/libeditor/HTMLEditorDataTransfer.cpp
+++ b/editor/libeditor/HTMLEditorDataTransfer.cpp
@@ -290,17 +290,17 @@ HTMLEditor::DoInsertHTMLWithContext(cons
       }
     }
     return NS_OK;
   }
 
   // Are there any table elements in the list?
   // check for table cell selection mode
   bool cellSelectionMode = false;
-  nsCOMPtr<nsIDOMElement> cell;
+  RefPtr<Element> cell;
   rv = GetFirstSelectedCell(nullptr, getter_AddRefs(cell));
   if (NS_SUCCEEDED(rv) && cell) {
     cellSelectionMode = true;
   }
 
   if (cellSelectionMode) {
     // do we have table content to paste?  If so, we want to delete
     // the selected table cells and replace with new table elements;
--- a/editor/libeditor/HTMLTableEditor.cpp
+++ b/editor/libeditor/HTMLTableEditor.cpp
@@ -18,17 +18,16 @@
 #include "nsAString.h"
 #include "nsAlgorithm.h"
 #include "nsCOMPtr.h"
 #include "nsDebug.h"
 #include "nsError.h"
 #include "nsGkAtoms.h"
 #include "nsAtom.h"
 #include "nsIContent.h"
-#include "nsIDOMElement.h"
 #include "nsIDOMNode.h"
 #include "nsIFrame.h"
 #include "nsINode.h"
 #include "nsIPresShell.h"
 #include "nsISupportsUtils.h"
 #include "nsITableCellLayout.h" // For efficient access to table cell
 #include "nsLiteralString.h"
 #include "nsQueryFrame.h"
@@ -46,22 +45,22 @@ using namespace dom;
 
 /**
  * Stack based helper class for restoring selection after table edit.
  */
 class MOZ_STACK_CLASS AutoSelectionSetterAfterTableEdit final
 {
 private:
   RefPtr<HTMLEditor> mHTMLEditor;
-  nsCOMPtr<nsIDOMElement> mTable;
+  RefPtr<Element> mTable;
   int32_t mCol, mRow, mDirection, mSelected;
 
 public:
   AutoSelectionSetterAfterTableEdit(HTMLEditor& aHTMLEditor,
-                                    nsIDOMElement* aTable,
+                                    Element* aTable,
                                     int32_t aRow,
                                     int32_t aCol,
                                     int32_t aDirection,
                                     bool aSelected)
     : mHTMLEditor(&aHTMLEditor)
     , mTable(aTable)
     , mCol(aCol)
     , mRow(aRow)
@@ -83,51 +82,47 @@ public:
   void CancelSetCaret()
   {
     mHTMLEditor = nullptr;
     mTable = nullptr;
   }
 };
 
 nsresult
-HTMLEditor::InsertCell(nsIDOMElement* aDOMCell,
+HTMLEditor::InsertCell(Element* aCell,
                        int32_t aRowSpan,
                        int32_t aColSpan,
                        bool aAfter,
                        bool aIsHeader,
-                       nsIDOMElement** aNewDOMCell)
+                       Element** aNewCell)
 {
-  if (aNewDOMCell) {
-    *aNewDOMCell = nullptr;
+  if (aNewCell) {
+    *aNewCell = nullptr;
   }
 
-  if (NS_WARN_IF(!aDOMCell)) {
+  if (NS_WARN_IF(!aCell)) {
     return NS_ERROR_NULL_POINTER;
   }
 
   // And the parent and offsets needed to do an insert
-  nsCOMPtr<nsIContent> cell = do_QueryInterface(aDOMCell);
-  if (NS_WARN_IF(!cell)) {
-    return NS_ERROR_INVALID_ARG;
-  }
-  EditorDOMPoint pointToInsert(cell);
+  EditorDOMPoint pointToInsert(aCell);
   if (NS_WARN_IF(!pointToInsert.IsSet())) {
     return NS_ERROR_INVALID_ARG;
   }
 
   RefPtr<Element> newCell =
     CreateElementWithDefaults(aIsHeader ? NS_LITERAL_STRING("th") :
                                           NS_LITERAL_STRING("tb"));
   if (NS_WARN_IF(!newCell)) {
     return NS_ERROR_FAILURE;
   }
 
   //Optional: return new cell created
-  if (aNewDOMCell) {
-    CallQueryInterface(newCell, aNewDOMCell);
+  if (aNewCell) {
+    *aNewCell = do_AddRef(newCell).take();
   }
 
   if (aRowSpan > 1) {
     // Note: Do NOT use editor transaction for this
     nsAutoString newRowSpan;
     newRowSpan.AppendInt(aRowSpan, 10);
     newCell->SetAttr(kNameSpaceID_None, nsGkAtoms::rowspan, newRowSpan, true);
   }
@@ -144,54 +139,46 @@ HTMLEditor::InsertCell(nsIDOMElement* aD
   }
 
   // Don't let Rules System change the selection.
   AutoTransactionsConserveSelection dontChangeSelection(this);
   return InsertNodeWithTransaction(*newCell, pointToInsert);
 }
 
 nsresult
-HTMLEditor::SetColSpan(nsIDOMElement* aDOMCell,
+HTMLEditor::SetColSpan(Element* aCell,
                        int32_t aColSpan)
 {
-  if (NS_WARN_IF(!aDOMCell)) {
-    return NS_ERROR_INVALID_ARG;
-  }
-  nsCOMPtr<Element> cell = do_QueryInterface(aDOMCell);
-  if (NS_WARN_IF(!cell)) {
+  if (NS_WARN_IF(!aCell)) {
     return NS_ERROR_INVALID_ARG;
   }
   nsAutoString newSpan;
   newSpan.AppendInt(aColSpan, 10);
-  return SetAttributeWithTransaction(*cell, *nsGkAtoms::colspan, newSpan);
+  return SetAttributeWithTransaction(*aCell, *nsGkAtoms::colspan, newSpan);
 }
 
 nsresult
-HTMLEditor::SetRowSpan(nsIDOMElement* aDOMCell,
+HTMLEditor::SetRowSpan(Element* aCell,
                        int32_t aRowSpan)
 {
-  if (NS_WARN_IF(!aDOMCell)) {
-    return NS_ERROR_INVALID_ARG;
-  }
-  nsCOMPtr<Element> cell = do_QueryInterface(aDOMCell);
-  if (NS_WARN_IF(!cell)) {
+  if (NS_WARN_IF(!aCell)) {
     return NS_ERROR_INVALID_ARG;
   }
   nsAutoString newSpan;
   newSpan.AppendInt(aRowSpan, 10);
-  return SetAttributeWithTransaction(*cell, *nsGkAtoms::rowspan, newSpan);
+  return SetAttributeWithTransaction(*aCell, *nsGkAtoms::rowspan, newSpan);
 }
 
 NS_IMETHODIMP
 HTMLEditor::InsertTableCell(int32_t aNumber,
                             bool aAfter)
 {
-  nsCOMPtr<nsIDOMElement> table;
-  nsCOMPtr<nsIDOMElement> curCell;
-  nsCOMPtr<nsIDOMNode> cellParent;
+  RefPtr<Element> table;
+  RefPtr<Element> curCell;
+  nsCOMPtr<nsINode> cellParent;
   int32_t cellOffset, startRowIndex, startColIndex;
   nsresult rv = GetCellContext(nullptr,
                                getter_AddRefs(table),
                                getter_AddRefs(curCell),
                                getter_AddRefs(cellParent), &cellOffset,
                                &startRowIndex, &startColIndex);
   NS_ENSURE_SUCCESS(rv, rv);
   // Don't fail if no cell found
@@ -230,153 +217,146 @@ HTMLEditor::InsertTableCell(int32_t aNum
     }
   }
   // XXX This is perhaps the result of the last call of
   //     InsertNodeWithTransaction() or CreateElementWithDefaults().
   return rv;
 }
 
 NS_IMETHODIMP
-HTMLEditor::GetFirstRow(nsIDOMElement* aTableElement,
-                        nsIDOMNode** aRowNode)
+HTMLEditor::GetFirstRow(Element* aTableElement,
+                        nsINode** aRowNode)
 {
   NS_ENSURE_TRUE(aRowNode, NS_ERROR_NULL_POINTER);
 
   *aRowNode = nullptr;
 
-  nsCOMPtr<nsINode> tableElement = do_QueryInterface(aTableElement);
+  nsCOMPtr<nsINode> tableElement = aTableElement;
   NS_ENSURE_TRUE(tableElement, NS_ERROR_NULL_POINTER);
 
   tableElement = GetElementOrParentByTagName(NS_LITERAL_STRING("table"),
                                              tableElement);
   NS_ENSURE_TRUE(tableElement, NS_ERROR_NULL_POINTER);
 
   nsCOMPtr<nsIContent> tableChild = tableElement->GetFirstChild();
   while (tableChild) {
     if (tableChild->IsHTMLElement(nsGkAtoms::tr)) {
       // Found a row directly under <table>
-      *aRowNode = tableChild->AsDOMNode();
-      NS_ADDREF(*aRowNode);
+      tableChild.forget(aRowNode);
       return NS_OK;
     }
     // Look for row in one of the row container elements
     if (tableChild->IsAnyOfHTMLElements(nsGkAtoms::tbody,
                                         nsGkAtoms::thead,
                                         nsGkAtoms::tfoot)) {
       nsCOMPtr<nsIContent> rowNode = tableChild->GetFirstChild();
 
       // We can encounter textnodes here -- must find a row
       while (rowNode && !HTMLEditUtils::IsTableRow(rowNode)) {
         rowNode = rowNode->GetNextSibling();
       }
 
       if (rowNode) {
-        *aRowNode = rowNode->AsDOMNode();
-        NS_ADDREF(*aRowNode);
+        rowNode.forget(aRowNode);
         return NS_OK;
       }
     }
     // Here if table child was a CAPTION or COLGROUP
     //  or child of a row parent wasn't a row (bad HTML?),
     //  or first child was a textnode
     // Look in next table child
     tableChild = tableChild->GetNextSibling();
   }
   // If here, row was not found
   return NS_SUCCESS_EDITOR_ELEMENT_NOT_FOUND;
 }
 
 NS_IMETHODIMP
-HTMLEditor::GetNextRow(nsIDOMNode* aCurrentRowNode,
-                       nsIDOMNode** aRowNode)
+HTMLEditor::GetNextRow(nsINode* aCurrentRowNode,
+                       nsINode** aRowNode)
 {
   NS_ENSURE_TRUE(aRowNode, NS_ERROR_NULL_POINTER);
 
   *aRowNode = nullptr;
 
-  nsCOMPtr<nsINode> currentRowNode = do_QueryInterface(aCurrentRowNode);
-  NS_ENSURE_TRUE(currentRowNode, NS_ERROR_NULL_POINTER);
-
-  if (!HTMLEditUtils::IsTableRow(currentRowNode)) {
+  NS_ENSURE_TRUE(aCurrentRowNode, NS_ERROR_NULL_POINTER);
+
+  if (!HTMLEditUtils::IsTableRow(aCurrentRowNode)) {
     return NS_ERROR_FAILURE;
   }
 
-  nsIContent* nextRow = currentRowNode->GetNextSibling();
+  nsIContent* nextRow = aCurrentRowNode->GetNextSibling();
 
   // Skip over any textnodes here
   while (nextRow && !HTMLEditUtils::IsTableRow(nextRow)) {
     nextRow = nextRow->GetNextSibling();
   }
   if (nextRow) {
-    *aRowNode = nextRow->AsDOMNode();
-    NS_ADDREF(*aRowNode);
+    *aRowNode = do_AddRef(nextRow).take();
     return NS_OK;
   }
 
   // No row found, search for rows in other table sections
-  nsINode* rowParent = currentRowNode->GetParentNode();
+  nsINode* rowParent = aCurrentRowNode->GetParentNode();
   NS_ENSURE_TRUE(rowParent, NS_ERROR_NULL_POINTER);
 
   nsIContent* parentSibling = rowParent->GetNextSibling();
 
   while (parentSibling) {
     nextRow = parentSibling->GetFirstChild();
 
     // We can encounter textnodes here -- must find a row
     while (nextRow && !HTMLEditUtils::IsTableRow(nextRow)) {
       nextRow = nextRow->GetNextSibling();
     }
     if (nextRow) {
-      *aRowNode = nextRow->AsDOMNode();
-      NS_ADDREF(*aRowNode);
+      *aRowNode = do_AddRef(nextRow).take();
       return NS_OK;
     }
 
     // We arrive here only if a table section has no children
     //  or first child of section is not a row (bad HTML or more "_moz_text" nodes!)
     // So look for another section sibling
     parentSibling = parentSibling->GetNextSibling();
   }
   // If here, row was not found
   return NS_SUCCESS_EDITOR_ELEMENT_NOT_FOUND;
 }
 
 nsresult
-HTMLEditor::GetLastCellInRow(nsIDOMNode* aRowNode,
-                             nsIDOMNode** aCellNode)
+HTMLEditor::GetLastCellInRow(nsINode* aRowNode,
+                             nsINode** aCellNode)
 {
   NS_ENSURE_TRUE(aCellNode, NS_ERROR_NULL_POINTER);
 
   *aCellNode = nullptr;
 
-  nsCOMPtr<nsINode> rowNode = do_QueryInterface(aRowNode);
-  NS_ENSURE_TRUE(rowNode, NS_ERROR_NULL_POINTER);
-
-  nsCOMPtr<nsINode> rowChild = rowNode->GetLastChild();
+  NS_ENSURE_TRUE(aRowNode, NS_ERROR_NULL_POINTER);
+
+  nsCOMPtr<nsINode> rowChild = aRowNode->GetLastChild();
 
   while (rowChild && !HTMLEditUtils::IsTableCell(rowChild)) {
     // Skip over textnodes
     rowChild = rowChild->GetPreviousSibling();
   }
   if (rowChild) {
-    *aCellNode = rowChild->AsDOMNode();
-    NS_ADDREF(*aCellNode);
+    rowChild.forget(aCellNode);
     return NS_OK;
   }
   // If here, cell was not found
   return NS_SUCCESS_EDITOR_ELEMENT_NOT_FOUND;
 }
 
 NS_IMETHODIMP
 HTMLEditor::InsertTableColumn(int32_t aNumber,
                               bool aAfter)
 {
   RefPtr<Selection> selection;
-  nsCOMPtr<nsIDOMElement> table;
-  nsCOMPtr<nsIDOMElement> curCell;
+  RefPtr<Element> table;
+  RefPtr<Element> curCell;
   int32_t startRowIndex, startColIndex;
   nsresult rv = GetCellContext(getter_AddRefs(selection),
                                getter_AddRefs(table),
                                getter_AddRefs(curCell),
                                nullptr, nullptr,
                                &startRowIndex, &startColIndex);
   NS_ENSURE_SUCCESS(rv, rv);
   // Don't fail if no cell found
@@ -422,17 +402,17 @@ HTMLEditor::InsertTableColumn(int32_t aN
 
   // If we are inserting after all existing columns
   // Make sure table is "well formed"
   //  before appending new column
   if (startColIndex >= colCount) {
     NormalizeTable(table);
   }
 
-  nsCOMPtr<nsIDOMNode> rowNode;
+  nsCOMPtr<nsINode> rowNode;
   for (rowIndex = 0; rowIndex < rowCount; rowIndex++) {
     if (startColIndex < colCount) {
       // We are inserting before an existing column
       rv = GetCellDataAt(table, rowIndex, startColIndex,
                          getter_AddRefs(curCell),
                          &curStartRowIndex, &curStartColIndex,
                          &rowSpan, &colSpan,
                          &actualRowSpan, &actualColSpan, &isSelected);
@@ -455,58 +435,56 @@ HTMLEditor::InsertTableColumn(int32_t aN
           // Insert a new cell before current one
           selection->Collapse(curCell, 0);
           rv = InsertTableCell(aNumber, false);
         }
       }
     } else {
       // Get current row and append new cells after last cell in row
       if (!rowIndex) {
-        rv = GetFirstRow(table.get(), getter_AddRefs(rowNode));
+        rv = GetFirstRow(table, getter_AddRefs(rowNode));
         if (NS_WARN_IF(NS_FAILED(rv))) {
           return rv;
         }
       } else {
-        nsCOMPtr<nsIDOMNode> nextRow;
-        rv = GetNextRow(rowNode.get(), getter_AddRefs(nextRow));
+        nsCOMPtr<nsINode> nextRow;
+        rv = GetNextRow(rowNode, getter_AddRefs(nextRow));
         if (NS_WARN_IF(NS_FAILED(rv))) {
           return rv;
         }
         rowNode = nextRow;
       }
 
       if (rowNode) {
-        nsCOMPtr<nsIDOMNode> lastCell;
+        nsCOMPtr<nsINode> lastCell;
         rv = GetLastCellInRow(rowNode, getter_AddRefs(lastCell));
         NS_ENSURE_SUCCESS(rv, rv);
         NS_ENSURE_TRUE(lastCell, NS_ERROR_FAILURE);
 
-        curCell = do_QueryInterface(lastCell);
-        if (curCell) {
-          // Simply add same number of cells to each row
-          // Although tempted to check cell indexes for curCell,
-          //  the effects of COLSPAN>1 in some cells makes this futile!
-          // We must use NormalizeTable first to assure
-          //  that there are cells in each cellmap location
-          selection->Collapse(curCell, 0);
-          rv = InsertTableCell(aNumber, true);
-        }
+        curCell = lastCell->AsElement();
+        // Simply add same number of cells to each row
+        // Although tempted to check cell indexes for curCell,
+        //  the effects of COLSPAN>1 in some cells makes this futile!
+        // We must use NormalizeTable first to assure
+        //  that there are cells in each cellmap location
+        selection->Collapse(curCell, 0);
+        rv = InsertTableCell(aNumber, true);
       }
     }
   }
   // XXX This is perhaps the result of the last call of InsertTableCell().
   return rv;
 }
 
 NS_IMETHODIMP
 HTMLEditor::InsertTableRow(int32_t aNumber,
                            bool aAfter)
 {
-  nsCOMPtr<nsIDOMElement> table;
-  nsCOMPtr<nsIDOMElement> curCell;
+  RefPtr<Element> table;
+  RefPtr<Element> curCell;
 
   int32_t startRowIndex, startColIndex;
   nsresult rv = GetCellContext(nullptr,
                                getter_AddRefs(table),
                                getter_AddRefs(curCell),
                                nullptr, nullptr,
                                &startRowIndex, &startColIndex);
   NS_ENSURE_SUCCESS(rv, rv);
@@ -547,17 +525,17 @@ HTMLEditor::InsertTableRow(int32_t aNumb
 
   //We control selection resetting after the insert...
   AutoSelectionSetterAfterTableEdit setCaret(*this, table, startRowIndex,
                                              startColIndex, ePreviousColumn,
                                              false);
   //...so suppress Rules System selection munging
   AutoTransactionsConserveSelection dontChangeSelection(this);
 
-  nsCOMPtr<nsIDOMElement> cellForRowParent;
+  RefPtr<Element> cellForRowParent;
   int32_t cellsInRow = 0;
   if (startRowIndex < rowCount) {
     // We are inserting above an existing row
     // Get each cell in the insert row to adjust for COLSPAN effects while we
     //   count how many cells are needed
     int32_t colIndex = 0;
     while (NS_SUCCEEDED(GetCellDataAt(table, startRowIndex, colIndex,
                                       getter_AddRefs(curCell),
@@ -615,27 +593,25 @@ HTMLEditor::InsertTableRow(int32_t aNumb
 
       // Save cell from the last row that we will use below
       if (!cellForRowParent && curStartRowIndex == lastRow) {
         cellForRowParent = curCell;
       }
     }
   }
 
-  nsCOMPtr<nsINode> cellNodeForRowParent = do_QueryInterface(cellForRowParent);
-
   if (cellsInRow > 0) {
 
     NS_NAMED_LITERAL_STRING(trStr, "tr");
-    if (!cellNodeForRowParent) {
+    if (!cellForRowParent) {
       return NS_ERROR_FAILURE;
     }
 
     nsCOMPtr<Element> parentRow =
-      GetElementOrParentByTagName(trStr, cellNodeForRowParent);
+      GetElementOrParentByTagName(trStr, cellForRowParent);
     NS_ENSURE_TRUE(parentRow, NS_ERROR_NULL_POINTER);
 
     // The row parent and offset where we will insert new row
     nsCOMPtr<nsINode> parentOfRow = parentRow->GetParentNode();
     NS_ENSURE_TRUE(parentOfRow, NS_ERROR_NULL_POINTER);
     int32_t newRowOffset = parentOfRow->ComputeIndexOf(parentRow);
 
     // Adjust for when adding past the end
@@ -683,17 +659,17 @@ HTMLEditor::InsertTableRow(int32_t aNumb
 
   return NS_OK;
 }
 
 // Editor helper only
 // XXX Code changed for bug 217717 and now we don't need aSelection param
 //     TODO: Remove aSelection param
 nsresult
-HTMLEditor::DeleteTable2(nsIDOMElement* aTable,
+HTMLEditor::DeleteTable2(Element* aTable,
                          Selection* aSelection)
 {
   NS_ENSURE_TRUE(aTable, NS_ERROR_NULL_POINTER);
 
   // Select the table
   nsresult rv = ClearSelection();
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return rv;
@@ -707,32 +683,32 @@ HTMLEditor::DeleteTable2(nsIDOMElement* 
   }
   return NS_OK;
 }
 
 NS_IMETHODIMP
 HTMLEditor::DeleteTable()
 {
   RefPtr<Selection> selection;
-  nsCOMPtr<nsIDOMElement> table;
+  RefPtr<Element> table;
   nsresult rv = GetCellContext(getter_AddRefs(selection),
                                getter_AddRefs(table),
                                nullptr, nullptr, nullptr, nullptr, nullptr);
   NS_ENSURE_SUCCESS(rv, rv);
 
   AutoPlaceholderBatch beginBatching(this);
   return DeleteTable2(table, selection);
 }
 
 NS_IMETHODIMP
 HTMLEditor::DeleteTableCell(int32_t aNumber)
 {
   RefPtr<Selection> selection;
-  nsCOMPtr<nsIDOMElement> table;
-  nsCOMPtr<nsIDOMElement> cell;
+  RefPtr<Element> table;
+  RefPtr<Element> cell;
   int32_t startRowIndex, startColIndex;
 
 
   nsresult rv = GetCellContext(getter_AddRefs(selection),
                                getter_AddRefs(table),
                                getter_AddRefs(cell),
                                nullptr, nullptr,
                                &startRowIndex, &startColIndex);
@@ -740,17 +716,17 @@ HTMLEditor::DeleteTableCell(int32_t aNum
   NS_ENSURE_SUCCESS(rv, rv);
   // Don't fail if we didn't find a table or cell
   NS_ENSURE_TRUE(table && cell, NS_SUCCESS_EDITOR_ELEMENT_NOT_FOUND);
 
   AutoPlaceholderBatch beginBatching(this);
   // Prevent rules testing until we're done
   AutoRules beginRulesSniffing(this, EditAction::deleteNode, nsIEditor::eNext);
 
-  nsCOMPtr<nsIDOMElement> firstCell;
+  RefPtr<Element> firstCell;
   rv = GetFirstSelectedCell(nullptr, getter_AddRefs(firstCell));
   NS_ENSURE_SUCCESS(rv, rv);
 
   if (firstCell && selection->RangeCount() > 1) {
     // When > 1 selected cell,
     //  ignore aNumber and use selected cells
     cell = firstCell;
 
@@ -834,26 +810,22 @@ HTMLEditor::DeleteTableCell(int32_t aNum
               startColIndex = nextCol - 1;
               // Set true since we know we will look at a new column next
               checkToDeleteColumn = true;
             }
           }
         }
         if (!deleteCol) {
           // First get the next cell to delete
-          nsCOMPtr<nsIDOMElement> nextCell;
+          RefPtr<Element> nextCell;
           rv = GetNextSelectedCell(nullptr, getter_AddRefs(nextCell));
           NS_ENSURE_SUCCESS(rv, rv);
 
           // Then delete the cell
-          nsCOMPtr<nsINode> cellToBeRemoved = do_QueryInterface(cell);
-          if (NS_WARN_IF(!cellToBeRemoved)) {
-            return NS_ERROR_FAILURE;
-          }
-          rv = DeleteNodeWithTransaction(*cellToBeRemoved);
+          rv = DeleteNodeWithTransaction(*cell);
           if (NS_WARN_IF(NS_FAILED(rv))) {
             return rv;
           }
 
           // The next cell to delete
           cell = nextCell;
           if (cell) {
             rv = GetCellIndexes(cell, &startRowIndex, &startColIndex);
@@ -869,20 +841,18 @@ HTMLEditor::DeleteTableCell(int32_t aNum
                           getter_AddRefs(cell),
                           nullptr, nullptr,
                           &startRowIndex, &startColIndex);
       NS_ENSURE_SUCCESS(rv, rv);
       // Don't fail if no cell found
       NS_ENSURE_TRUE(cell, NS_SUCCESS_EDITOR_ELEMENT_NOT_FOUND);
 
       if (GetNumberOfCellsInRow(table, startRowIndex) == 1) {
-        nsCOMPtr<nsIDOMElement> parentRow;
-        rv = GetElementOrParentByTagName(NS_LITERAL_STRING("tr"), cell,
-                                         getter_AddRefs(parentRow));
-        NS_ENSURE_SUCCESS(rv, rv);
+        RefPtr<Element> parentRow =
+          GetElementOrParentByTagName(NS_LITERAL_STRING("tr"), cell);
         NS_ENSURE_TRUE(parentRow, NS_ERROR_NULL_POINTER);
 
         // We should delete the row instead,
         //  but first check if its the only row left
         //  so we can delete the entire table
         int32_t rowCount, colCount;
         rv = GetTableSize(table, &rowCount, &colCount);
         NS_ENSURE_SUCCESS(rv, rv);
@@ -898,37 +868,33 @@ HTMLEditor::DeleteTableCell(int32_t aNum
         // More than 1 cell in the row
 
         // The setCaret object will call AutoSelectionSetterAfterTableEdit in its
         // destructor
         AutoSelectionSetterAfterTableEdit setCaret(*this, table, startRowIndex,
                                                    startColIndex, ePreviousColumn,
                                                    false);
         AutoTransactionsConserveSelection dontChangeSelection(this);
-        nsCOMPtr<nsINode> cellToBeRemoved = do_QueryInterface(cell);
-        if (NS_WARN_IF(!cellToBeRemoved)) {
-          return NS_ERROR_FAILURE;
-        }
-        rv = DeleteNodeWithTransaction(*cellToBeRemoved);
+        rv = DeleteNodeWithTransaction(*cell);
         // If we fail, don't try to delete any more cells???
         if (NS_WARN_IF(NS_FAILED(rv))) {
           return rv;
         }
       }
     }
   }
   return NS_OK;
 }
 
 NS_IMETHODIMP
 HTMLEditor::DeleteTableCellContents()
 {
   RefPtr<Selection> selection;
-  nsCOMPtr<nsIDOMElement> table;
-  nsCOMPtr<nsIDOMElement> cell;
+  RefPtr<Element> table;
+  RefPtr<Element> cell;
   int32_t startRowIndex, startColIndex;
   nsresult rv = GetCellContext(getter_AddRefs(selection),
                                getter_AddRefs(table),
                                getter_AddRefs(cell),
                                nullptr, nullptr,
                                &startRowIndex, &startColIndex);
   NS_ENSURE_SUCCESS(rv, rv);
   // Don't fail if no cell found
@@ -937,17 +903,17 @@ HTMLEditor::DeleteTableCellContents()
 
   AutoPlaceholderBatch beginBatching(this);
   // Prevent rules testing until we're done
   AutoRules beginRulesSniffing(this, EditAction::deleteNode, nsIEditor::eNext);
   //Don't let Rules System change the selection
   AutoTransactionsConserveSelection dontChangeSelection(this);
 
 
-  nsCOMPtr<nsIDOMElement> firstCell;
+  RefPtr<Element> firstCell;
   rv = GetFirstSelectedCell(nullptr, getter_AddRefs(firstCell));
   NS_ENSURE_SUCCESS(rv, rv);
 
 
   if (firstCell) {
     cell = firstCell;
     rv = GetCellIndexes(cell, &startRowIndex, &startColIndex);
     NS_ENSURE_SUCCESS(rv, rv);
@@ -966,39 +932,36 @@ HTMLEditor::DeleteTableCellContents()
     } else {
       cell = nullptr;
     }
   }
   return NS_OK;
 }
 
 nsresult
-HTMLEditor::DeleteCellContents(nsIDOMElement* aCell)
+HTMLEditor::DeleteCellContents(Element* aCell)
 {
-  nsCOMPtr<Element> cell = do_QueryInterface(aCell);
-  NS_ENSURE_TRUE(cell, NS_ERROR_NULL_POINTER);
-
   // Prevent rules testing until we're done
   AutoRules beginRulesSniffing(this, EditAction::deleteNode, nsIEditor::eNext);
 
-  while (nsCOMPtr<nsINode> child = cell->GetLastChild()) {
+  while (nsCOMPtr<nsINode> child = aCell->GetLastChild()) {
     nsresult rv = DeleteNodeWithTransaction(*child);
     if (NS_WARN_IF(NS_FAILED(rv))) {
       return rv;
     }
   }
   return NS_OK;
 }
 
 NS_IMETHODIMP
 HTMLEditor::DeleteTableColumn(int32_t aNumber)
 {
   RefPtr<Selection> selection;
-  nsCOMPtr<nsIDOMElement> table;
-  nsCOMPtr<nsIDOMElement> cell;
+  RefPtr<Element> table;
+  RefPtr<Element> cell;
   int32_t startRowIndex, startColIndex, rowCount, colCount;
   nsresult rv = GetCellContext(getter_AddRefs(selection),
                                getter_AddRefs(table),
                                getter_AddRefs(cell),
                                nullptr, nullptr,
                                &startRowIndex, &startColIndex);
   NS_ENSURE_SUCCESS(rv, rv);
   // Don't fail if no cell found
@@ -1015,17 +978,17 @@ HTMLEditor::DeleteTableColumn(int32_t aN
   // Check for counts too high
   aNumber = std::min(aNumber,(colCount-startColIndex));
 
   AutoPlaceholderBatch beginBatching(this);
   // Prevent rules testing until we're done
   AutoRules beginRulesSniffing(this, EditAction::deleteNode, nsIEditor::eNext);
 
   // Test if deletion is controlled by selected cells
-  nsCOMPtr<nsIDOMElement> firstCell;
+  RefPtr<Element> firstCell;
   rv = GetFirstSelectedCell(nullptr, getter_AddRefs(firstCell));
   NS_ENSURE_SUCCESS(rv, rv);
 
   uint32_t rangeCount = selection->RangeCount();
 
   if (firstCell && rangeCount > 1) {
     // Fetch indexes again - may be different for selected cells
     rv = GetCellIndexes(firstCell, &startRowIndex, &startColIndex);
@@ -1065,22 +1028,22 @@ HTMLEditor::DeleteTableColumn(int32_t aN
       rv = DeleteColumn(table, startColIndex);
       NS_ENSURE_SUCCESS(rv, rv);
     }
   }
   return NS_OK;
 }
 
 nsresult
-HTMLEditor::DeleteColumn(nsIDOMElement* aTable,
+HTMLEditor::DeleteColumn(Element* aTable,
                          int32_t aColIndex)
 {
   NS_ENSURE_TRUE(aTable, NS_ERROR_NULL_POINTER);
 
-  nsCOMPtr<nsIDOMElement> cell;
+  RefPtr<Element> cell;
   int32_t startRowIndex, startColIndex, rowSpan, colSpan, actualRowSpan, actualColSpan;
   bool    isSelected;
   int32_t rowIndex = 0;
 
   do {
     nsresult rv =
       GetCellDataAt(aTable, rowIndex, aColIndex, getter_AddRefs(cell),
                     &startRowIndex, &startColIndex, &rowSpan, &colSpan,
@@ -1104,20 +1067,18 @@ HTMLEditor::DeleteColumn(nsIDOMElement* 
           DeleteCellContents(cell);
         }
         // To next cell in column
         rowIndex += actualRowSpan;
       } else {
         // Delete the cell
         if (GetNumberOfCellsInRow(aTable, rowIndex) == 1) {
           // Only 1 cell in row - delete the row
-          nsCOMPtr<nsIDOMElement> parentRow;
-          rv = GetElementOrParentByTagName(NS_LITERAL_STRING("tr"), cell,
-                                           getter_AddRefs(parentRow));
-          NS_ENSURE_SUCCESS(rv, rv);
+          RefPtr<Element> parentRow =
+            GetElementOrParentByTagName(NS_LITERAL_STRING("tr"), cell);
           if (!parentRow) {
             return NS_ERROR_NULL_POINTER;
           }
 
           //  But first check if its the only row left
           //  so we can delete the entire table
           //  (This should never happen but it's the safe thing to do)
           int32_t rowCount, colCount;
@@ -1135,21 +1096,17 @@ HTMLEditor::DeleteColumn(nsIDOMElement* 
           rv = DeleteRow(aTable, startRowIndex);
           NS_ENSURE_SUCCESS(rv, rv);
 
           // Note that we don't incremenet rowIndex
           // since a row was deleted and "next"
           // row now has current rowIndex
         } else {
           // A more "normal" deletion
-          nsCOMPtr<nsINode> cellToBeRemoved = do_QueryInterface(cell);
-          if (NS_WARN_IF(!cellToBeRemoved)) {
-            return NS_ERROR_FAILURE;
-          }
-          rv = DeleteNodeWithTransaction(*cellToBeRemoved);
+          rv = DeleteNodeWithTransaction(*cell);
           if (NS_WARN_IF(NS_FAILED(rv))) {
             return rv;
           }
 
           //Skip over any rows spanned by this cell
           rowIndex += actualRowSpan;
         }
       }
@@ -1158,18 +1115,18 @@ HTMLEditor::DeleteColumn(nsIDOMElement* 
 
   return NS_OK;
 }
 
 NS_IMETHODIMP
 HTMLEditor::DeleteTableRow(int32_t aNumber)
 {
   RefPtr<Selection> selection;
-  nsCOMPtr<nsIDOMElement> table;
-  nsCOMPtr<nsIDOMElement> cell;
+  RefPtr<Element> table;
+  RefPtr<Element> cell;
   int32_t startRowIndex, startColIndex;
   int32_t rowCount, colCount;
   nsresult rv =  GetCellContext(getter_AddRefs(selection),
                                 getter_AddRefs(table),
                                 getter_AddRefs(cell),
                                 nullptr, nullptr,
                                 &startRowIndex, &startColIndex);
   NS_ENSURE_SUCCESS(rv, rv);
@@ -1183,17 +1140,17 @@ HTMLEditor::DeleteTableRow(int32_t aNumb
   if (!startRowIndex && aNumber >= rowCount) {
     return DeleteTable2(table, selection);
   }
 
   AutoPlaceholderBatch beginBatching(this);
   // Prevent rules testing until we're done
   AutoRules beginRulesSniffing(this, EditAction::deleteNode, nsIEditor::eNext);
 
-  nsCOMPtr<nsIDOMElement> firstCell;
+  RefPtr<Element> firstCell;
   rv = GetFirstSelectedCell(nullptr, getter_AddRefs(firstCell));
   NS_ENSURE_SUCCESS(rv, rv);
 
   uint32_t rangeCount = selection->RangeCount();
 
   if (firstCell && rangeCount > 1) {
     // Fetch indexes again - may be different for selected cells
     rv = GetCellIndexes(firstCell, &startRowIndex, &startColIndex);
@@ -1248,33 +1205,33 @@ HTMLEditor::DeleteTableRow(int32_t aNumb
       }
     }
   }
   return NS_OK;
 }
 
 // Helper that doesn't batch or change the selection
 nsresult
-HTMLEditor::DeleteRow(nsIDOMElement* aTable,
+HTMLEditor::DeleteRow(Element* aTable,
                       int32_t aRowIndex)
 {
   NS_ENSURE_TRUE(aTable, NS_ERROR_NULL_POINTER);
 
-  nsCOMPtr<nsIDOMElement> cell;
-  nsCOMPtr<nsIDOMElement> cellInDeleteRow;
+  RefPtr<Element> cell;
+  RefPtr<Element> cellInDeleteRow;
   int32_t startRowIndex, startColIndex, rowSpan, colSpan, actualRowSpan, actualColSpan;
   bool    isSelected;
   int32_t colIndex = 0;
 
   // Prevent rules testing until we're done
   AutoRules beginRulesSniffing(this, EditAction::deleteNode, nsIEditor::eNext);
 
   // The list of cells we will change rowspan in
   //  and the new rowspan values for each
-  nsTArray<nsCOMPtr<nsIDOMElement> > spanCellList;
+  nsTArray<RefPtr<Element> > spanCellList;
   nsTArray<int32_t> newSpanList;
 
   int32_t rowCount, colCount;
   nsresult rv = GetTableSize(aTable, &rowCount, &colCount);
   NS_ENSURE_SUCCESS(rv, rv);
 
   // Scan through cells in row to do rowspan adjustments
   // Note that after we delete row, startRowIndex will point to the
@@ -1326,109 +1283,93 @@ HTMLEditor::DeleteRow(nsIDOMElement* aTa
       colIndex += actualColSpan;
     }
   } while (cell);
 
   // Things are messed up if we didn't find a cell in the row!
   NS_ENSURE_TRUE(cellInDeleteRow, NS_ERROR_FAILURE);
 
   // Delete the entire row
-  nsCOMPtr<nsIDOMElement> parentRow;
-  rv = GetElementOrParentByTagName(NS_LITERAL_STRING("tr"), cellInDeleteRow,
-                                   getter_AddRefs(parentRow));
-  NS_ENSURE_SUCCESS(rv, rv);
+  RefPtr<Element> parentRow =
+    GetElementOrParentByTagName(NS_LITERAL_STRING("tr"), cellInDeleteRow);
 
   if (parentRow) {
-    nsCOMPtr<nsINode> rowToBeRemoved = do_QueryInterface(parentRow);
-    if (NS_WARN_IF(!rowToBeRemoved)) {
-      return NS_ERROR_FAILURE;
-    }
-    rv = DeleteNodeWithTransaction(*rowToBeRemoved);
+    rv = DeleteNodeWithTransaction(*parentRow);
     if (NS_WARN_IF(NS_FAILED(rv))) {
       return rv;
     }
   }
 
   // Now we can set new rowspans for cells stored above
   for (uint32_t i = 0, n = spanCellList.Length(); i < n; i++) {
-    nsIDOMElement *cellPtr = spanCellList[i];
+    Element* cellPtr = spanCellList[i];
     if (cellPtr) {
       rv = SetRowSpan(cellPtr, newSpanList[i]);
       NS_ENSURE_SUCCESS(rv, rv);
     }
   }
   return NS_OK;
 }
 
 
 NS_IMETHODIMP
 HTMLEditor::SelectTable()
 {
-  nsCOMPtr<nsIDOMElement> table;
-  nsresult rv = GetElementOrParentByTagName(NS_LITERAL_STRING("table"), nullptr,
-                                            getter_AddRefs(table));
-  NS_ENSURE_SUCCESS(rv, rv);
+  RefPtr<Element> table =
+    GetElementOrParentByTagName(NS_LITERAL_STRING("table"), nullptr);
   // Don't fail if we didn't find a table
   NS_ENSURE_TRUE(table, NS_OK);
 
-  rv = ClearSelection();
+  nsresult rv = ClearSelection();
   if (NS_FAILED(rv)) {
     return rv;
   }
   return AppendNodeToSelectionAsRange(table);
 }
 
 NS_IMETHODIMP
 HTMLEditor::SelectTableCell()
 {
-  nsCOMPtr<nsIDOMElement> cell;
-  nsresult rv = GetElementOrParentByTagName(NS_LITERAL_STRING("td"), nullptr,
-                                            getter_AddRefs(cell));
-  NS_ENSURE_SUCCESS(rv, rv);
+  RefPtr<Element> cell =
+    GetElementOrParentByTagName(NS_LITERAL_STRING("td"), nullptr);
   NS_ENSURE_TRUE(cell, NS_SUCCESS_EDITOR_ELEMENT_NOT_FOUND);
 
-  rv = ClearSelection();
+  nsresult rv = ClearSelection();
   if (NS_FAILED(rv)) {
     return rv;
   }
   return AppendNodeToSelectionAsRange(cell);
 }
 
 NS_IMETHODIMP
-HTMLEditor::SelectBlockOfCells(nsIDOMElement* aStartCell,
-                               nsIDOMElement* aEndCell)
+HTMLEditor::SelectBlockOfCells(Element* aStartCell,
+                               Element* aEndCell)
 {
   NS_ENSURE_TRUE(aStartCell && aEndCell, NS_ERROR_NULL_POINTER);
 
   RefPtr<Selection> selection = GetSelection();
   NS_ENSURE_TRUE(selection, NS_ERROR_FAILURE);
 
   NS_NAMED_LITERAL_STRING(tableStr, "table");
-  nsCOMPtr<nsIDOMElement> table;
-  nsresult rv = GetElementOrParentByTagName(tableStr, aStartCell,
-                                            getter_AddRefs(table));
-  NS_ENSURE_SUCCESS(rv, rv);
+  RefPtr<Element> table = GetElementOrParentByTagName(tableStr, aStartCell);
   NS_ENSURE_TRUE(table, NS_ERROR_FAILURE);
 
-  nsCOMPtr<nsIDOMElement> endTable;
-  rv = GetElementOrParentByTagName(tableStr, aEndCell,
-                                   getter_AddRefs(endTable));
-  NS_ENSURE_SUCCESS(rv, rv);
+  RefPtr<Element> endTable = GetElementOrParentByTagName(tableStr, aEndCell);
   NS_ENSURE_TRUE(endTable, NS_ERROR_FAILURE);
 
   // We can only select a block if within the same table,
   //  so do nothing if not within one table
   if (table != endTable) {
     return NS_OK;
   }
 
   int32_t startRowIndex, startColIndex, endRowIndex, endColIndex;
 
   // Get starting and ending cells' location in the cellmap
-  rv = GetCellIndexes(aStartCell, &startRowIndex, &startColIndex);
+  nsresult rv = GetCellIndexes(aStartCell, &startRowIndex, &startColIndex);
   if (NS_FAILED(rv)) {
     return rv;
   }
 
   rv = GetCellIndexes(aEndCell, &endRowIndex, &endColIndex);
   if (NS_FAILED(rv)) {
     return rv;
   }
@@ -1439,17 +1380,17 @@ HTMLEditor::SelectBlockOfCells(nsIDOMEle
 
   // Examine all cell nodes in current selection and
   //  remove those outside the new block cell region
   int32_t minColumn = std::min(startColIndex, endColIndex);
   int32_t minRow    = std::min(startRowIndex, endRowIndex);
   int32_t maxColumn   = std::max(startColIndex, endColIndex);
   int32_t maxRow      = std::max(startRowIndex, endRowIndex);
 
-  nsCOMPtr<nsIDOMElement> cell;
+  RefPtr<Element> cell;
   int32_t currentRowIndex, currentColIndex;
   nsCOMPtr<nsIDOMRange> range;
   rv = GetFirstSelectedCell(getter_AddRefs(range), getter_AddRefs(cell));
   NS_ENSURE_SUCCESS(rv, rv);
   if (rv == NS_SUCCESS_EDITOR_ELEMENT_NOT_FOUND) {
     return NS_OK;
   }
 
@@ -1492,37 +1433,33 @@ HTMLEditor::SelectBlockOfCells(nsIDOMEle
   // NS_OK, otherwise, the last failure of GetCellDataAt() or
   // AppendNodeToSelectionAsRange().
   return rv;
 }
 
 NS_IMETHODIMP
 HTMLEditor::SelectAllTableCells()
 {
-  nsCOMPtr<nsIDOMElement> cell;
-  nsresult rv = GetElementOrParentByTagName(NS_LITERAL_STRING("td"), nullptr,
-                                            getter_AddRefs(cell));
-  NS_ENSURE_SUCCESS(rv, rv);
+  RefPtr<Element> cell =
+    GetElementOrParentByTagName(NS_LITERAL_STRING("td"), nullptr);
 
   // Don't fail if we didn't find a cell
   NS_ENSURE_TRUE(cell, NS_SUCCESS_EDITOR_ELEMENT_NOT_FOUND);
 
-  nsCOMPtr<nsIDOMElement> startCell = cell;
+  RefPtr<Element> startCell = cell;
 
   // Get parent table
-  nsCOMPtr<nsIDOMElement> table;
-  rv = GetElementOrParentByTagName(NS_LITERAL_STRING("table"), cell,
-                                   getter_AddRefs(table));
-  NS_ENSURE_SUCCESS(rv, rv);
+  RefPtr<Element> table =
+    GetElementOrParentByTagName(NS_LITERAL_STRING("table"), cell);
   if (!table) {
     return NS_ERROR_NULL_POINTER;
   }
 
   int32_t rowCount, colCount;
-  rv = GetTableSize(table, &rowCount, &colCount);
+  nsresult rv = GetTableSize(table, &rowCount, &colCount);
   NS_ENSURE_SUCCESS(rv, rv);
 
   RefPtr<Selection> selection = GetSelection();
   NS_ENSURE_TRUE(selection, NS_ERROR_FAILURE);
 
   // Suppress nsISelectionListener notification
   //  until all selection changes are finished
   SelectionBatcher selectionBatcher(selection);
@@ -1561,35 +1498,33 @@ HTMLEditor::SelectAllTableCells()
   // NS_OK, otherwise, the error of ClearSelection() when there is no column or
   // the last failure of GetCellDataAt() or AppendNodeToSelectionAsRange().
   return rv;
 }
 
 NS_IMETHODIMP
 HTMLEditor::SelectTableRow()
 {
-  nsCOMPtr<nsIDOMElement> cell;
-  nsresult rv = GetElementOrParentByTagName(NS_LITERAL_STRING("td"), nullptr,
-                                            getter_AddRefs(cell));
-  NS_ENSURE_SUCCESS(rv, rv);
+  RefPtr<Element> cell =
+    GetElementOrParentByTagName(NS_LITERAL_STRING("td"), nullptr);
 
   // Don't fail if we didn't find a cell
   NS_ENSURE_TRUE(cell, NS_SUCCESS_EDITOR_ELEMENT_NOT_FOUND);
-  nsCOMPtr<nsIDOMElement> startCell = cell;
+  RefPtr<Element> startCell = cell;
 
   // Get table and location of cell:
   RefPtr<Selection> selection;
-  nsCOMPtr<nsIDOMElement> table;
+  RefPtr<Element> table;
   int32_t startRowIndex, startColIndex;
 
-  rv = GetCellContext(getter_AddRefs(selection),
-                      getter_AddRefs(table),
-                      getter_AddRefs(cell),
-                      nullptr, nullptr,
-                      &startRowIndex, &startColIndex);
+  nsresult rv = GetCellContext(getter_AddRefs(selection),
+                               getter_AddRefs(table),
+                               getter_AddRefs(cell),
+                               nullptr, nullptr,
+                               &startRowIndex, &startColIndex);
   NS_ENSURE_SUCCESS(rv, rv);
   NS_ENSURE_TRUE(table, NS_ERROR_FAILURE);
 
   int32_t rowCount, colCount;
   rv = GetTableSize(table, &rowCount, &colCount);
   NS_ENSURE_SUCCESS(rv, rv);
 
   //Note: At this point, we could get first and last cells in row,
@@ -1631,36 +1566,34 @@ HTMLEditor::SelectTableRow()
   // NS_OK, otherwise, the error of ClearSelection() when there is no column or
   // the last failure of GetCellDataAt() or AppendNodeToSelectionAsRange().
   return rv;
 }
 
 NS_IMETHODIMP
 HTMLEditor::SelectTableColumn()
 {
-  nsCOMPtr<nsIDOMElement> cell;
-  nsresult rv = GetElementOrParentByTagName(NS_LITERAL_STRING("td"), nullptr,
-                                            getter_AddRefs(cell));
-  NS_ENSURE_SUCCESS(rv, rv);
+  RefPtr<Element> cell =
+    GetElementOrParentByTagName(NS_LITERAL_STRING("td"), nullptr);
 
   // Don't fail if we didn't find a cell
   NS_ENSURE_TRUE(cell, NS_SUCCESS_EDITOR_ELEMENT_NOT_FOUND);
 
-  nsCOMPtr<nsIDOMElement> startCell = cell;
+  RefPtr<Element> startCell = cell;
 
   // Get location of cell:
   RefPtr<Selection> selection;
-  nsCOMPtr<nsIDOMElement> table;
+  RefPtr<Element> table;
   int32_t startRowIndex, startColIndex;
 
-  rv = GetCellContext(getter_AddRefs(selection),
-                      getter_AddRefs(table),
-                      getter_AddRefs(cell),
-                      nullptr, nullptr,
-                      &startRowIndex, &startColIndex);
+  nsresult rv = GetCellContext(getter_AddRefs(selection),
+                               getter_AddRefs(table),
+                               getter_AddRefs(cell),
+                               nullptr, nullptr,
+                               &startRowIndex, &startColIndex);
   NS_ENSURE_SUCCESS(rv, rv);
   NS_ENSURE_TRUE(table, NS_ERROR_FAILURE);
 
   int32_t rowCount, colCount;
   rv = GetTableSize(table, &rowCount, &colCount);
   NS_ENSURE_SUCCESS(rv, rv);
 
   // Suppress nsISelectionListener notification
@@ -1698,18 +1631,18 @@ HTMLEditor::SelectTableColumn()
   // NS_OK, otherwise, the error of ClearSelection() when there is no row or
   // the last failure of GetCellDataAt() or AppendNodeToSelectionAsRange().
   return rv;
 }
 
 NS_IMETHODIMP
 HTMLEditor::SplitTableCell()
 {
-  nsCOMPtr<nsIDOMElement> table;
-  nsCOMPtr<nsIDOMElement> cell;
+  RefPtr<Element> table;
+  RefPtr<Element> cell;
   int32_t startRowIndex, startColIndex, actualRowSpan, actualColSpan;
   // Get cell, table, etc. at selection anchor node
   nsresult rv = GetCellContext(nullptr,
                                getter_AddRefs(table),
                                getter_AddRefs(cell),
                                nullptr, nullptr,
                                &startRowIndex, &startColIndex);
   NS_ENSURE_SUCCESS(rv, rv);
@@ -1733,17 +1666,17 @@ HTMLEditor::SplitTableCell()
 
   // We reset selection
   AutoSelectionSetterAfterTableEdit setCaret(*this, table, startRowIndex,
                                              startColIndex, ePreviousColumn,
                                              false);
   //...so suppress Rules System selection munging
   AutoTransactionsConserveSelection dontChangeSelection(this);
 
-  nsCOMPtr<nsIDOMElement> newCell;
+  RefPtr<Element> newCell;
   int32_t rowIndex = startRowIndex;
   int32_t rowSpanBelow, colSpanAfter;
 
   // Split up cell row-wise first into rowspan=1 above, and the rest below,
   //  whittling away at the cell below until no more extra span
   for (rowSpanBelow = actualRowSpan-1; rowSpanBelow >= 0; rowSpanBelow--) {
     // We really split row-wise only if we had rowspan > 1
     if (rowSpanBelow > 0) {
@@ -1763,61 +1696,52 @@ HTMLEditor::SplitTableCell()
     }
     // Point to the new cell and repeat
     rowIndex++;
   }
   return NS_OK;
 }
 
 nsresult
-HTMLEditor::CopyCellBackgroundColor(nsIDOMElement* aDOMDestCell,
-                                    nsIDOMElement* aDOMSourceCell)
+HTMLEditor::CopyCellBackgroundColor(Element* aDestCell,
+                                    Element* aSourceCell)
 {
-  if (NS_WARN_IF(!aDOMDestCell) || NS_WARN_IF(!aDOMSourceCell)) {
-    return NS_ERROR_INVALID_ARG;
-  }
-  nsCOMPtr<Element> destCell = do_QueryInterface(aDOMDestCell);
-  if (NS_WARN_IF(!destCell)) {
-    return NS_ERROR_INVALID_ARG;
-  }
-
-  nsCOMPtr<Element> sourceCell = do_QueryInterface(aDOMSourceCell);
-  if (NS_WARN_IF(!sourceCell)) {
+  if (NS_WARN_IF(!aDestCell) || NS_WARN_IF(!aSourceCell)) {
     return NS_ERROR_INVALID_ARG;
   }
 
   // Copy backgournd color to new cell.
   nsAutoString color;
   bool isSet;
   nsresult rv =
-    GetAttributeValue(sourceCell, NS_LITERAL_STRING("bgcolor"),
+    GetAttributeValue(aSourceCell, NS_LITERAL_STRING("bgcolor"),
                       color, &isSet);
   if (NS_FAILED(rv)) {
     return rv;
   }
   if (!isSet) {
     return NS_OK;
   }
-  return SetAttributeWithTransaction(*destCell, *nsGkAtoms::bgcolor, color);
+  return SetAttributeWithTransaction(*aDestCell, *nsGkAtoms::bgcolor, color);
 }
 
 nsresult
-HTMLEditor::SplitCellIntoColumns(nsIDOMElement* aTable,
+HTMLEditor::SplitCellIntoColumns(Element* aTable,
                                  int32_t aRowIndex,
                                  int32_t aColIndex,
                                  int32_t aColSpanLeft,
                                  int32_t aColSpanRight,
-                                 nsIDOMElement** aNewCell)
+                                 Element** aNewCell)
 {
   NS_ENSURE_TRUE(aTable, NS_ERROR_NULL_POINTER);
   if (aNewCell) {
     *aNewCell = nullptr;
   }
 
-  nsCOMPtr<nsIDOMElement> cell;
+  RefPtr<Element> cell;
   int32_t startRowIndex, startColIndex, rowSpan, colSpan, actualRowSpan, actualColSpan;
   bool    isSelected;
   nsresult rv =
     GetCellDataAt(aTable, aRowIndex, aColIndex, getter_AddRefs(cell),
                   &startRowIndex, &startColIndex,
                   &rowSpan, &colSpan,
                   &actualRowSpan, &actualColSpan, &isSelected);
   NS_ENSURE_SUCCESS(rv, rv);
@@ -1829,41 +1753,41 @@ HTMLEditor::SplitCellIntoColumns(nsIDOME
   }
 
   // Reduce colspan of cell to split
   rv = SetColSpan(cell, aColSpanLeft);
   NS_ENSURE_SUCCESS(rv, rv);
 
   // Insert new cell after using the remaining span
   //  and always get the new cell so we can copy the background color;
-  nsCOMPtr<nsIDOMElement> newCell;
+  RefPtr<Element> newCell;
   rv = InsertCell(cell, actualRowSpan, aColSpanRight, true, false,
                   getter_AddRefs(newCell));
   NS_ENSURE_SUCCESS(rv, rv);
   if (!newCell) {
     return NS_OK;
   }
   if (aNewCell) {
     NS_ADDREF(*aNewCell = newCell.get());
   }
   return CopyCellBackgroundColor(newCell, cell);
 }
 
 nsresult
-HTMLEditor::SplitCellIntoRows(nsIDOMElement* aTable,
+HTMLEditor::SplitCellIntoRows(Element* aTable,
                               int32_t aRowIndex,
                               int32_t aColIndex,
                               int32_t aRowSpanAbove,
                               int32_t aRowSpanBelow,
-                              nsIDOMElement** aNewCell)
+                              Element** aNewCell)
 {
   NS_ENSURE_TRUE(aTable, NS_ERROR_NULL_POINTER);
   if (aNewCell) *aNewCell = nullptr;
 
-  nsCOMPtr<nsIDOMElement> cell;
+  RefPtr<Element> cell;
   int32_t startRowIndex, startColIndex, rowSpan, colSpan, actualRowSpan, actualColSpan;
   bool    isSelected;
   nsresult rv =
     GetCellDataAt(aTable, aRowIndex, aColIndex, getter_AddRefs(cell),
                   &startRowIndex, &startColIndex,
                   &rowSpan, &colSpan,
                   &actualRowSpan, &actualColSpan, &isSelected);
   NS_ENSURE_SUCCESS(rv, rv);
@@ -1873,18 +1797,18 @@ HTMLEditor::SplitCellIntoRows(nsIDOMElem
   if (actualRowSpan <= 1 || (aRowSpanAbove + aRowSpanBelow) > actualRowSpan) {
     return NS_OK;
   }
 
   int32_t rowCount, colCount;
   rv = GetTableSize(aTable, &rowCount, &colCount);
   NS_ENSURE_SUCCESS(rv, rv);
 
-  nsCOMPtr<nsIDOMElement> cell2;
-  nsCOMPtr<nsIDOMElement> lastCellFound;
+  RefPtr<Element> cell2;
+  RefPtr<Element> lastCellFound;
   int32_t startRowIndex2, startColIndex2, rowSpan2, colSpan2, actualRowSpan2, actualColSpan2;
   bool    isSelected2;
   int32_t colIndex = 0;
   bool insertAfter = (startColIndex > 0);
   // This is the row we will insert new cell into
   int32_t rowBelowIndex = startRowIndex+aRowSpanAbove;
 
   // Find a cell to insert before or after
@@ -1942,35 +1866,34 @@ HTMLEditor::SplitCellIntoRows(nsIDOMElem
 
   // Reduce rowspan of cell to split
   rv = SetRowSpan(cell, aRowSpanAbove);
   NS_ENSURE_SUCCESS(rv, rv);
 
 
   // Insert new cell after using the remaining span
   //  and always get the new cell so we can copy the background color;
-  nsCOMPtr<nsIDOMElement> newCell;
+  RefPtr<Element> newCell;
   rv = InsertCell(cell2, aRowSpanBelow, actualColSpan, insertAfter, false,
                   getter_AddRefs(newCell));
   NS_ENSURE_SUCCESS(rv, rv);
   if (!newCell) {
     return NS_OK;
   }
   if (aNewCell) {
     NS_ADDREF(*aNewCell = newCell.get());
   }
   return CopyCellBackgroundColor(newCell, cell2);
 }
 
 NS_IMETHODIMP
-HTMLEditor::SwitchTableCellHeaderType(nsIDOMElement* aSourceCell,
-                                      nsIDOMElement** aNewCell)
+HTMLEditor::SwitchTableCellHeaderType(Element* aSourceCell,
+                                      Element** aNewCell)
 {
-  nsCOMPtr<Element> sourceCell = do_QueryInterface(aSourceCell);
-  if (NS_WARN_IF(!sourceCell)) {
+  if (NS_WARN_IF(!aSourceCell)) {
     return NS_ERROR_INVALID_ARG;
   }
 
   AutoPlaceholderBatch beginBatching(this);
   // Prevent auto insertion of BR in new cell created by
   // ReplaceContainerAndCloneAttributesWithTransaction().
   AutoRules beginRulesSniffing(this, EditAction::insertNode, nsIEditor::eNext);
 
@@ -1981,45 +1904,43 @@ HTMLEditor::SwitchTableCellHeaderType(ns
   if (NS_WARN_IF(!selection)) {
     return NS_ERROR_FAILURE;
   }
 
   AutoSelectionRestorer selectionRestorer(selection, this);
 
   // Set to the opposite of current type
   nsAtom* newCellName =
-    sourceCell->IsHTMLElement(nsGkAtoms::td) ? nsGkAtoms::th : nsGkAtoms::td;
+    aSourceCell->IsHTMLElement(nsGkAtoms::td) ? nsGkAtoms::th : nsGkAtoms::td;
 
   // This creates new node, moves children, copies attributes (true)
   //   and manages the selection!
   RefPtr<Element> newCell =
-    ReplaceContainerAndCloneAttributesWithTransaction(*sourceCell,
+    ReplaceContainerAndCloneAttributesWithTransaction(*aSourceCell,
                                                       *newCellName);
   if (NS_WARN_IF(!newCell)) {
     return NS_ERROR_FAILURE;
   }
 
   // Return the new cell
   if (aNewCell) {
-    nsCOMPtr<nsIDOMElement> newElement = do_QueryInterface(newCell);
-    *aNewCell = newElement.get();
-    NS_ADDREF(*aNewCell);
+    newCell.forget(aNewCell);
   }
 
   return NS_OK;
 }
 
 NS_IMETHODIMP
 HTMLEditor::JoinTableCells(bool aMergeNonContiguousContents)
 {
-  nsCOMPtr<nsIDOMElement> table;
-  nsCOMPtr<nsIDOMElement> targetCell;
+  RefPtr<Element> table;
+  RefPtr<Element> targetCell;
   int32_t startRowIndex, startColIndex, rowSpan, colSpan, actualRowSpan, actualColSpan;
   bool    isSelected;
-  nsCOMPtr<nsIDOMElement> cell2;
+  RefPtr<Element> cell2;
   int32_t startRowIndex2, startColIndex2, rowSpan2, colSpan2, actualRowSpan2, actualColSpan2;
   bool    isSelected2;
 
   // Get cell, table, etc. at selection anchor node
   nsresult rv = GetCellContext(nullptr,
                                getter_AddRefs(table),
                                getter_AddRefs(targetCell),
                                nullptr, nullptr,
@@ -2032,25 +1953,25 @@ HTMLEditor::JoinTableCells(bool aMergeNo
   AutoPlaceholderBatch beginBatching(this);
   //Don't let Rules System change the selection
   AutoTransactionsConserveSelection dontChangeSelection(this);
 
   // Note: We dont' use AutoSelectionSetterAfterTableEdit here so the selection
   //  is retained after joining. This leaves the target cell selected
   //  as well as the "non-contiguous" cells, so user can see what happened.
 
-  nsCOMPtr<nsIDOMElement> firstCell;
+  RefPtr<Element> firstCell;
   int32_t firstRowIndex, firstColIndex;
   rv = GetFirstSelectedCellInTable(&firstRowIndex, &firstColIndex,
                                    getter_AddRefs(firstCell));
   NS_ENSURE_SUCCESS(rv, rv);
 
   bool joinSelectedCells = false;
   if (firstCell) {
-    nsCOMPtr<nsIDOMElement> secondCell;
+    RefPtr<Element> secondCell;
     rv = GetNextSelectedCell(nullptr, getter_AddRefs(secondCell));
     NS_ENSURE_SUCCESS(rv, rv);
 
     // If only one cell is selected, join with cell to the right
     joinSelectedCells = (secondCell != nullptr);
   }
 
   if (joinSelectedCells) {
@@ -2155,17 +2076,17 @@ HTMLEditor::JoinTableCells(bool aMergeNo
       } else {
         // No selected cells in this row -- stop at row above
         //  and leave last column at its previous value
         lastRowIndex = std::max(0,rowIndex - 1);
       }
     }
 
     // The list of cells we will delete after joining
-    nsTArray<nsCOMPtr<nsIDOMElement> > deleteList;
+    nsTArray<RefPtr<Element>> deleteList;
 
     // 2nd pass: Do the joining and merging
     for (rowIndex = 0; rowIndex < rowCount; rowIndex++) {
       for (colIndex = 0; colIndex < colCount;
            colIndex += std::max(actualColSpan2, 1)) {
         rv = GetCellDataAt(table, rowIndex, colIndex, getter_AddRefs(cell2),
                            &startRowIndex2, &startColIndex2,
                            &rowSpan2, &colSpan2,
@@ -2214,22 +2135,18 @@ HTMLEditor::JoinTableCells(bool aMergeNo
     }
 
     // All cell contents are merged. Delete the empty cells we accumulated
     // Prevent rules testing until we're done
     AutoRules beginRulesSniffing(this, EditAction::deleteNode,
                                  nsIEditor::eNext);
 
     for (uint32_t i = 0, n = deleteList.Length(); i < n; i++) {
-      nsIDOMElement *elementPtr = deleteList[i];
-      if (elementPtr) {
-        nsCOMPtr<nsINode> nodeToBeRemoved = do_QueryInterface(elementPtr);
-        if (NS_WARN_IF(!nodeToBeRemoved)) {
-          return NS_ERROR_FAILURE;
-        }
+      RefPtr<Element> nodeToBeRemoved = deleteList[i];
+      if (nodeToBeRemoved) {
         rv = DeleteNodeWithTransaction(*nodeToBeRemoved);
         if (NS_WARN_IF(NS_FAILED(rv))) {
           return rv;
         }
       }
     }
     // Cleanup selection: remove ranges where cells were deleted
     RefPtr<Selection> selection = GetSelection();
@@ -2237,17 +2154,17 @@ HTMLEditor::JoinTableCells(bool aMergeNo
 
     uint32_t rangeCount = selection->RangeCount();
 
     RefPtr<nsRange> range;
     for (uint32_t i = 0; i < rangeCount; i++) {
       range = selection->GetRangeAt(i);
       NS_ENSURE_TRUE(range, NS_ERROR_FAILURE);
 
-      nsCOMPtr<nsIDOMElement> deletedCell;
+      RefPtr<Element> deletedCell;
       GetCellFromRange(range, getter_AddRefs(deletedCell));
       if (!deletedCell) {
         selection->RemoveRange(*range, IgnoreErrors());
         rangeCount--;
         i--;
       }
     }
 
@@ -2322,54 +2239,52 @@ HTMLEditor::JoinTableCells(bool aMergeNo
     // Reset target cell's colspan to encompass cell to the right
     rv = SetColSpan(targetCell, actualColSpan+actualColSpan2);
     NS_ENSURE_SUCCESS(rv, rv);
   }
   return NS_OK;
 }
 
 nsresult
-HTMLEditor::MergeCells(nsCOMPtr<nsIDOMElement> aTargetCell,
-                       nsCOMPtr<nsIDOMElement> aCellToMerge,
+HTMLEditor::MergeCells(RefPtr<Element> aTargetCell,
+                       RefPtr<Element> aCellToMerge,
                        bool aDeleteCellToMerge)
 {
-  nsCOMPtr<dom::Element> targetCell = do_QueryInterface(aTargetCell);
-  nsCOMPtr<dom::Element> cellToMerge = do_QueryInterface(aCellToMerge);
-  NS_ENSURE_TRUE(targetCell && cellToMerge, NS_ERROR_NULL_POINTER);
+  NS_ENSURE_TRUE(aTargetCell && aCellToMerge, NS_ERROR_NULL_POINTER);
 
   // Prevent rules testing until we're done
   AutoRules beginRulesSniffing(this, EditAction::deleteNode, nsIEditor::eNext);
 
   // Don't need to merge if cell is empty
-  if (!IsEmptyCell(cellToMerge)) {
+  if (!IsEmptyCell(aCellToMerge)) {
     // Get index of last child in target cell
     // If we fail or don't have children,
     //  we insert at index 0
     int32_t insertIndex = 0;
 
     // Start inserting just after last child
-    uint32_t len = targetCell->GetChildCount();
-    if (len == 1 && IsEmptyCell(targetCell)) {
+    uint32_t len = aTargetCell->GetChildCount();
+    if (len == 1 && IsEmptyCell(aTargetCell)) {
       // Delete the empty node
-      nsIContent* cellChild = targetCell->GetFirstChild();
+      nsIContent* cellChild = aTargetCell->GetFirstChild();
       if (NS_WARN_IF(!cellChild)) {
         return NS_ERROR_FAILURE;
       }
       nsresult rv = DeleteNodeWithTransaction(*cellChild);
       if (NS_WARN_IF(NS_FAILED(rv))) {
         return rv;
       }
       insertIndex = 0;
     } else {
       insertIndex = (int32_t)len;
     }
 
     // Move the contents
-    while (cellToMerge->HasChildren()) {
-      nsCOMPtr<nsIContent> cellChild = cellToMerge->GetLastChild();
+    while (aCellToMerge->HasChildren()) {
+      nsCOMPtr<nsIContent> cellChild = aCellToMerge->GetLastChild();
       if (NS_WARN_IF(!cellChild)) {
         return NS_ERROR_FAILURE;
       }
       nsresult rv = DeleteNodeWithTransaction(*cellChild);
       if (NS_WARN_IF(NS_FAILED(rv))) {
         return rv;
       }
       rv = InsertNodeWithTransaction(*cellChild,
@@ -2381,40 +2296,36 @@ HTMLEditor::MergeCells(nsCOMPtr<nsIDOMEl
     }
   }
 
   if (!aDeleteCellToMerge) {
     return NS_OK;
   }
 
   // Delete cells whose contents were moved.
-  nsCOMPtr<nsIContent> cellToBeRemoved = do_QueryInterface(aCellToMerge);
-  if (NS_WARN_IF(!cellToBeRemoved)) {
-    return NS_ERROR_INVALID_ARG;
-  }
-  nsresult rv = DeleteNodeWithTransaction(*cellToBeRemoved);
+  nsresult rv = DeleteNodeWithTransaction(*aCellToMerge);
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return rv;
   }
   return NS_OK;
 }
 
 
 nsresult
-HTMLEditor::FixBadRowSpan(nsIDOMElement* aTable,
+HTMLEditor::FixBadRowSpan(Element* aTable,
                           int32_t aRowIndex,
                           int32_t& aNewRowCount)
 {
   NS_ENSURE_TRUE(aTable, NS_ERROR_NULL_POINTER);
 
   int32_t rowCount, colCount;
   nsresult rv = GetTableSize(aTable, &rowCount, &colCount);
   NS_ENSURE_SUCCESS(rv, rv);
 
-  nsCOMPtr<nsIDOMElement>cell;
+  RefPtr<Element> cell;
   int32_t startRowIndex, startColIndex, rowSpan, colSpan, actualRowSpan, actualColSpan;
   bool    isSelected;
 
   int32_t minRowSpan = -1;
   int32_t colIndex;
 
   for (colIndex = 0; colIndex < colCount;
        colIndex += std::max(actualColSpan, 1)) {
@@ -2459,27 +2370,27 @@ HTMLEditor::FixBadRowSpan(nsIDOMElement*
       }
       NS_ASSERTION((actualColSpan > 0),"ActualColSpan = 0 in FixBadRowSpan");
     }
   }
   return GetTableSize(aTable, &aNewRowCount, &colCount);
 }
 
 nsresult
-HTMLEditor::FixBadColSpan(nsIDOMElement* aTable,
+HTMLEditor::FixBadColSpan(Element* aTable,
                           int32_t aColIndex,
                           int32_t& aNewColCount)
 {
   NS_ENSURE_TRUE(aTable, NS_ERROR_NULL_POINTER);
 
   int32_t rowCount, colCount;
   nsresult rv = GetTableSize(aTable, &rowCount, &colCount);
   NS_ENSURE_SUCCESS(rv, rv);
 
-  nsCOMPtr<nsIDOMElement> cell;
+  RefPtr<Element> cell;
   int32_t startRowIndex, startColIndex, rowSpan, colSpan, actualRowSpan, actualColSpan;
   bool    isSelected;
 
   int32_t minColSpan = -1;
   int32_t rowIndex;
 
   for (rowIndex = 0; rowIndex < rowCount;
        rowIndex += std::max(actualRowSpan, 1)) {
@@ -2524,57 +2435,56 @@ HTMLEditor::FixBadColSpan(nsIDOMElement*
       }
       NS_ASSERTION((actualRowSpan > 0),"ActualRowSpan = 0 in FixBadColSpan");
     }
   }
   return GetTableSize(aTable, &rowCount, &aNewColCount);
 }
 
 NS_IMETHODIMP
-HTMLEditor::NormalizeTable(nsIDOMElement* aTable)
+HTMLEditor::NormalizeTable(Element* aTable)
 {
   RefPtr<Selection> selection = GetSelection();
   NS_ENSURE_TRUE(selection, NS_ERROR_FAILURE);
 
-  nsCOMPtr<nsIDOMElement> table;
-  nsresult rv = GetElementOrParentByTagName(NS_LITERAL_STRING("table"),
-                                            aTable, getter_AddRefs(table));
-  NS_ENSURE_SUCCESS(rv, rv);
+  RefPtr<Element> table =
+    GetElementOrParentByTagName(NS_LITERAL_STRING("table"),
+                                aTable);
   // Don't fail if we didn't find a table
   NS_ENSURE_TRUE(table, NS_OK);
 
   int32_t rowCount, colCount, rowIndex, colIndex;
-  rv = GetTableSize(table, &rowCount, &colCount);
+  nsresult rv = GetTableSize(table, &rowCount, &colCount);
   NS_ENSURE_SUCCESS(rv, rv);
 
   // Save current selection
   AutoSelectionRestorer selectionRestorer(selection, this);
 
   AutoPlaceholderBatch beginBatching(this);
   // Prevent auto insertion of BR in new cell until we're done
   AutoRules beginRulesSniffing(this, EditAction::insertNode, nsIEditor::eNext);
 
-  nsCOMPtr<nsIDOMElement> cell;
+  RefPtr<Element> cell;
   int32_t startRowIndex, startColIndex, rowSpan, colSpan, actualRowSpan, actualColSpan;
   bool    isSelected;
 
   // Scan all cells in each row to detect bad rowspan values
   for (rowIndex = 0; rowIndex < rowCount; rowIndex++) {
     rv = FixBadRowSpan(table, rowIndex, rowCount);
     NS_ENSURE_SUCCESS(rv, rv);
   }
   // and same for colspans
   for (colIndex = 0; colIndex < colCount; colIndex++) {
     rv = FixBadColSpan(table, colIndex, colCount);
     NS_ENSURE_SUCCESS(rv, rv);
   }
 
   // Fill in missing cellmap locations with empty cells
   for (rowIndex = 0; rowIndex < rowCount; rowIndex++) {
-    nsCOMPtr<nsIDOMElement> previousCellInRow;
+    RefPtr<Element> previousCellInRow;
     for (colIndex = 0; colIndex < colCount; colIndex++) {
       rv = GetCellDataAt(table, rowIndex, colIndex, getter_AddRefs(cell),
                          &startRowIndex, &startColIndex, &rowSpan, &colSpan,
                          &actualRowSpan, &actualColSpan, &isSelected);
       // NOTE: This is a *real* failure.
       // GetCellDataAt passes if cell is missing from cellmap
       if (NS_FAILED(rv)) {
         return rv;
@@ -2610,66 +2520,61 @@ HTMLEditor::NormalizeTable(nsIDOMElement
         previousCellInRow = cell;
       }
     }
   }
   return NS_OK;
 }
 
 NS_IMETHODIMP
-HTMLEditor::GetCellIndexes(nsIDOMElement* aCell,
+HTMLEditor::GetCellIndexes(Element* aCell,
                            int32_t* aRowIndex,
                            int32_t* aColIndex)
 {
   NS_ENSURE_ARG_POINTER(aRowIndex);
   *aColIndex=0; // initialize out params
   NS_ENSURE_ARG_POINTER(aColIndex);
   *aRowIndex=0;
+  RefPtr<Element> cell; // Needs to stay alive while we're using
+                        // aCell, since it may be keeping it alive.
   if (!aCell) {
     // Get the selected cell or the cell enclosing the selection anchor
-    nsCOMPtr<nsIDOMElement> cell;
-    nsresult rv = GetElementOrParentByTagName(NS_LITERAL_STRING("td"), nullptr,
-                                              getter_AddRefs(cell));
-    if (NS_FAILED(rv) || !cell) {
+    cell = GetElementOrParentByTagName(NS_LITERAL_STRING("td"), nullptr);
+    if (!cell) {
       return NS_ERROR_FAILURE;
     }
     aCell = cell;
   }
 
   nsCOMPtr<nsIPresShell> ps = GetPresShell();
   NS_ENSURE_TRUE(ps, NS_ERROR_NOT_INITIALIZED);
 
-  nsCOMPtr<nsIContent> nodeAsContent( do_QueryInterface(aCell) );
-  NS_ENSURE_TRUE(nodeAsContent, NS_ERROR_FAILURE);
   // frames are not ref counted, so don't use an nsCOMPtr
-  nsIFrame *layoutObject = nodeAsContent->GetPrimaryFrame();
+  nsIFrame *layoutObject = aCell->GetPrimaryFrame();
   NS_ENSURE_TRUE(layoutObject, NS_ERROR_FAILURE);
 
   nsITableCellLayout *cellLayoutObject = do_QueryFrame(layoutObject);
   NS_ENSURE_TRUE(cellLayoutObject, NS_ERROR_FAILURE);
   return cellLayoutObject->GetCellIndexes(*aRowIndex, *aColIndex);
 }
 
 nsTableWrapperFrame*
-HTMLEditor::GetTableFrame(nsIDOMElement* aTable)
+HTMLEditor::GetTableFrame(Element* aTable)
 {
   NS_ENSURE_TRUE(aTable, nullptr);
-
-  nsCOMPtr<nsIContent> nodeAsContent( do_QueryInterface(aTable) );
-  NS_ENSURE_TRUE(nodeAsContent, nullptr);
-  return do_QueryFrame(nodeAsContent->GetPrimaryFrame());
+  return do_QueryFrame(aTable->GetPrimaryFrame());
 }
 
 //Return actual number of cells (a cell with colspan > 1 counts as just 1)
 int32_t
-HTMLEditor::GetNumberOfCellsInRow(nsIDOMElement* aTable,
+HTMLEditor::GetNumberOfCellsInRow(Element* aTable,
                                   int32_t rowIndex)
 {
   int32_t cellCount = 0;
-  nsCOMPtr<nsIDOMElement> cell;
+  RefPtr<Element> cell;
   int32_t colIndex = 0;
   do {
     int32_t startRowIndex, startColIndex, rowSpan, colSpan, actualRowSpan, actualColSpan;
     bool    isSelected;
     nsresult rv =
       GetCellDataAt(aTable, rowIndex, colIndex, getter_AddRefs(cell),
                     &startRowIndex, &startColIndex, &rowSpan, &colSpan,
                     &actualRowSpan, &actualColSpan, &isSelected);
@@ -2685,25 +2590,16 @@ HTMLEditor::GetNumberOfCellsInRow(nsIDOM
       colIndex++;
     }
   } while (cell);
 
   return cellCount;
 }
 
 NS_IMETHODIMP
-HTMLEditor::GetTableSize(nsIDOMElement* aTable,
-                         int32_t* aRowCount,
-                         int32_t* aColCount)
-{
-  nsCOMPtr<Element> table = do_QueryInterface(aTable);
-  return GetTableSize(table, aRowCount, aColCount);
-}
-
-nsresult
 HTMLEditor::GetTableSize(Element* aTable,
                          int32_t* aRowCount,
                          int32_t* aColCount)
 {
   NS_ENSURE_ARG_POINTER(aRowCount);
   NS_ENSURE_ARG_POINTER(aColCount);
   *aRowCount = 0;
   *aColCount = 0;
@@ -2717,20 +2613,20 @@ HTMLEditor::GetTableSize(Element* aTable
 
   *aRowCount = tableFrame->GetRowCount();
   *aColCount = tableFrame->GetColCount();
 
   return NS_OK;
 }
 
 NS_IMETHODIMP
-HTMLEditor::GetCellDataAt(nsIDOMElement* aTable,
+HTMLEditor::GetCellDataAt(Element* aTable,
                           int32_t aRowIndex,
                           int32_t aColIndex,
-                          nsIDOMElement** aCell,
+                          Element** aCell,
                           int32_t* aStartRowIndex,
                           int32_t* aStartColIndex,
                           int32_t* aRowSpan,
                           int32_t* aColSpan,
                           int32_t* aActualRowSpan,
                           int32_t* aActualColSpan,
                           bool* aIsSelected)
 {
@@ -2748,23 +2644,20 @@ HTMLEditor::GetCellDataAt(nsIDOMElement*
   *aRowSpan = 0;
   *aColSpan = 0;
   *aActualRowSpan = 0;
   *aActualColSpan = 0;
   *aIsSelected = false;
 
   *aCell = nullptr;
 
+  RefPtr<Element> table; // needs to live while we use aTable
   if (!aTable) {
     // Get the selected table or the table enclosing the selection anchor
-    nsCOMPtr<nsIDOMElement> table;
-    nsresult rv =
-      GetElementOrParentByTagName(NS_LITERAL_STRING("table"), nullptr,
-                                  getter_AddRefs(table));
-    NS_ENSURE_SUCCESS(rv, rv);
+    table = GetElementOrParentByTagName(NS_LITERAL_STRING("table"), nullptr);
     if (!table) {
       return NS_ERROR_FAILURE;
     }
     aTable = table;
   }
 
   nsTableWrapperFrame* tableFrame = GetTableFrame(aTable);
   NS_ENSURE_TRUE(tableFrame, NS_ERROR_FAILURE);
@@ -2777,59 +2670,56 @@ HTMLEditor::GetCellDataAt(nsIDOMElement*
 
   *aIsSelected = cellFrame->IsSelected();
   *aStartRowIndex = cellFrame->RowIndex();
   *aStartColIndex = cellFrame->ColIndex();
   *aRowSpan = cellFrame->GetRowSpan();
   *aColSpan = cellFrame->GetColSpan();
   *aActualRowSpan = tableFrame->GetEffectiveRowSpanAt(aRowIndex, aColIndex);
   *aActualColSpan = tableFrame->GetEffectiveColSpanAt(aRowIndex, aColIndex);
-  nsCOMPtr<nsIDOMElement> domCell = do_QueryInterface(cellFrame->GetContent());
+  RefPtr<Element> domCell = cellFrame->GetContent()->AsElement();
   domCell.forget(aCell);
 
   return NS_OK;
 }
 
 // When all you want is the cell
 NS_IMETHODIMP
-HTMLEditor::GetCellAt(nsIDOMElement* aTable,
+HTMLEditor::GetCellAt(Element* aTable,
                       int32_t aRowIndex,
                       int32_t aColIndex,
-                      nsIDOMElement** aCell)
+                      Element** aCell)
 {
   NS_ENSURE_ARG_POINTER(aCell);
   *aCell = nullptr;
 
+  RefPtr<Element> table; // Needs to live as long as we use aTable
   if (!aTable) {
     // Get the selected table or the table enclosing the selection anchor
-    nsCOMPtr<nsIDOMElement> table;
-    nsresult rv =
-      GetElementOrParentByTagName(NS_LITERAL_STRING("table"), nullptr,
-                                  getter_AddRefs(table));
-    NS_ENSURE_SUCCESS(rv, rv);
+    table = GetElementOrParentByTagName(NS_LITERAL_STRING("table"), nullptr);
     NS_ENSURE_TRUE(table, NS_ERROR_FAILURE);
     aTable = table;
   }
 
   nsTableWrapperFrame* tableFrame = GetTableFrame(aTable);
   if (!tableFrame) {
     *aCell = nullptr;
     return NS_SUCCESS_EDITOR_ELEMENT_NOT_FOUND;
   }
 
-  nsCOMPtr<nsIDOMElement> domCell =
-    do_QueryInterface(tableFrame->GetCellAt(aRowIndex, aColIndex));
-  domCell.forget(aCell);
+  nsIContent* cell = tableFrame->GetCellAt(aRowIndex, aColIndex);
+  RefPtr<Element> cellElement = cell ? cell->AsElement() : nullptr;
+  cellElement.forget(aCell);
 
   return NS_OK;
 }
 
 // When all you want are the rowspan and colspan (not exposed in nsITableEditor)
 nsresult
-HTMLEditor::GetCellSpansAt(nsIDOMElement* aTable,
+HTMLEditor::GetCellSpansAt(Element* aTable,
                            int32_t aRowIndex,
                            int32_t aColIndex,
                            int32_t& aActualRowSpan,
                            int32_t& aActualColSpan)
 {
   nsTableWrapperFrame* tableFrame = GetTableFrame(aTable);
   if (!tableFrame) {
     return NS_ERROR_FAILURE;
@@ -2837,19 +2727,19 @@ HTMLEditor::GetCellSpansAt(nsIDOMElement
   aActualRowSpan = tableFrame->GetEffectiveRowSpanAt(aRowIndex, aColIndex);
   aActualColSpan = tableFrame->GetEffectiveColSpanAt(aRowIndex, aColIndex);
 
   return NS_OK;
 }
 
 nsresult
 HTMLEditor::GetCellContext(Selection** aSelection,
-                           nsIDOMElement** aTable,
-                           nsIDOMElement** aCell,
-                           nsIDOMNode** aCellParent,
+                           Element** aTable,
+                           Element** aCell,
+                           nsINode** aCellParent,
                            int32_t* aCellOffset,
                            int32_t* aRowIndex,
                            int32_t* aColIndex)
 {
   // Initialize return pointers
   if (aSelection) {
     *aSelection = nullptr;
   }
@@ -2874,103 +2764,97 @@ HTMLEditor::GetCellContext(Selection** a
 
   RefPtr<Selection> selection = GetSelection();
   NS_ENSURE_TRUE(selection, NS_ERROR_FAILURE);
 
   if (aSelection) {
     *aSelection = selection.get();
     NS_ADDREF(*aSelection);
   }
-  nsCOMPtr <nsIDOMElement> table;
-  nsCOMPtr <nsIDOMElement> cell;
+  RefPtr<Element> table;
+  RefPtr<Element> cell;
 
   // Caller may supply the cell...
   if (aCell && *aCell) {
     cell = *aCell;
   }
 
   // ...but if not supplied,
   //    get cell if it's the child of selection anchor node,
   //    or get the enclosing by a cell
   if (!cell) {
     // Find a selected or enclosing table element
-    nsCOMPtr<nsIDOMElement> cellOrTableElement;
+    RefPtr<Element> cellOrTableElement;
     int32_t selectedCount;
     nsAutoString tagName;
     nsresult rv =
       GetSelectedOrParentTableElement(tagName, &selectedCount,
                                       getter_AddRefs(cellOrTableElement));
     NS_ENSURE_SUCCESS(rv, rv);
     if (tagName.EqualsLiteral("table")) {
       // We have a selected table, not a cell
       if (aTable) {
-        *aTable = cellOrTableElement.get();
-        NS_ADDREF(*aTable);
+        cellOrTableElement.forget(aTable);
       }
       return NS_OK;
     }
     if (!tagName.EqualsLiteral("td")) {
       return NS_SUCCESS_EDITOR_ELEMENT_NOT_FOUND;
     }
 
     // We found a cell
     cell = cellOrTableElement;
   }
   if (aCell) {
-    *aCell = cell.get();
-    NS_ADDREF(*aCell);
+    // we don't want to cell.forget() here, because we use it below.
+    *aCell = do_AddRef(cell).take();
   }
 
   // Get containing table
-  nsresult rv = GetElementOrParentByTagName(NS_LITERAL_STRING("table"), cell,
-                                            getter_AddRefs(table));
-  NS_ENSURE_SUCCESS(rv, rv);
+  table = GetElementOrParentByTagName(NS_LITERAL_STRING("table"), cell);
   // Cell must be in a table, so fail if not found
   NS_ENSURE_TRUE(table, NS_ERROR_FAILURE);
   if (aTable) {
-    *aTable = table.get();
-    NS_ADDREF(*aTable);
+    table.forget(aTable);
   }
 
   // Get the rest of the related data only if requested
   if (aRowIndex || aColIndex) {
     int32_t rowIndex, colIndex;
     // Get current cell location so we can put caret back there when done
-    rv = GetCellIndexes(cell, &rowIndex, &colIndex);
+    nsresult rv = GetCellIndexes(cell, &rowIndex, &colIndex);
     if (NS_FAILED(rv)) {
       return rv;
     }
     if (aRowIndex) {
       *aRowIndex = rowIndex;
     }
     if (aColIndex) {
       *aColIndex = colIndex;
     }
   }
   if (aCellParent) {
-    nsCOMPtr<nsINode> cellNode = do_QueryInterface(cell);
     // Get the immediate parent of the cell
-    nsINode* cellParent = cellNode->GetParentNode();
+    nsCOMPtr<nsINode> cellParent = cell->GetParentNode();
     // Cell has to have a parent, so fail if not found
     NS_ENSURE_TRUE(cellParent, NS_ERROR_FAILURE);
 
-    *aCellParent = cellParent->AsDOMNode();
-    NS_ADDREF(*aCellParent);
+    cellParent.forget(aCellParent);
 
     if (aCellOffset) {
-      *aCellOffset = GetChildOffset(cell, cellParent->AsDOMNode());
+      *aCellOffset = GetChildOffset(cell, cellParent);
     }
   }
 
   return NS_OK;
 }
 
 nsresult
 HTMLEditor::GetCellFromRange(nsRange* aRange,
-                             nsIDOMElement** aCell)
+                             Element** aCell)
 {
   // Note: this might return a node that is outside of the range.
   // Use carefully.
   NS_ENSURE_TRUE(aRange && aCell, NS_ERROR_NULL_POINTER);
 
   *aCell = nullptr;
 
   nsCOMPtr<nsINode> startContainer = aRange->GetStartContainer();
@@ -2994,26 +2878,26 @@ HTMLEditor::GetCellFromRange(nsRange* aR
   // If a cell is deleted, the range is collapse
   //   (startOffset == aRange->EndOffset())
   //   so tell caller the cell wasn't found
   if (startContainer == endContainer &&
       aRange->EndOffset() == startOffset+1 &&
       HTMLEditUtils::IsTableCell(childNode)) {
     // Should we also test if frame is selected? (Use GetCellDataAt())
     // (Let's not for now -- more efficient)
-    nsCOMPtr<nsIDOMElement> cellElement = do_QueryInterface(childNode);
+    RefPtr<Element> cellElement = childNode->AsElement();
     cellElement.forget(aCell);
     return NS_OK;
   }
   return NS_SUCCESS_EDITOR_ELEMENT_NOT_FOUND;
 }
 
 NS_IMETHODIMP
 HTMLEditor::GetFirstSelectedCell(nsIDOMRange** aRange,
-                                 nsIDOMElement** aCell)
+                                 Element** aCell)
 {
   NS_ENSURE_TRUE(aCell, NS_ERROR_NULL_POINTER);
   *aCell = nullptr;
   if (aRange) {
     *aRange = nullptr;
   }
 
   RefPtr<Selection> selection = GetSelection();
@@ -3043,17 +2927,17 @@ HTMLEditor::GetFirstSelectedCell(nsIDOMR
   // Setup for next cell
   mSelectedCellIndex = 1;
 
   return NS_OK;
 }
 
 NS_IMETHODIMP
 HTMLEditor::GetNextSelectedCell(nsIDOMRange** aRange,
-                                nsIDOMElement** aCell)
+                                Element** aCell)
 {
   NS_ENSURE_TRUE(aCell, NS_ERROR_NULL_POINTER);
   *aCell = nullptr;
   if (aRange) {
     *aRange = nullptr;
   }
 
   RefPtr<Selection> selection = GetSelection();
@@ -3095,34 +2979,34 @@ HTMLEditor::GetNextSelectedCell(nsIDOMRa
   mSelectedCellIndex++;
 
   return NS_OK;
 }
 
 NS_IMETHODIMP
 HTMLEditor::GetFirstSelectedCellInTable(int32_t* aRowIndex,
                                         int32_t* aColIndex,
-                                        nsIDOMElement** aCell)
+                                        Element** aCell)
 {
   NS_ENSURE_TRUE(aCell, NS_ERROR_NULL_POINTER);
   *aCell = nullptr;
   if (aRowIndex) {
     *aRowIndex = 0;
   }
   if (aColIndex) {
     *aColIndex = 0;
   }
 
-  nsCOMPtr<nsIDOMElement> cell;
+  RefPtr<Element> cell;
   nsresult rv = GetFirstSelectedCell(nullptr, getter_AddRefs(cell));
   NS_ENSURE_SUCCESS(rv, rv);
   NS_ENSURE_TRUE(cell, NS_SUCCESS_EDITOR_ELEMENT_NOT_FOUND);
 
-  *aCell = cell.get();
-  NS_ADDREF(*aCell);
+  // We don't want to cell.forget() here, because we use "cell" below.
+  *aCell = do_AddRef(cell).take();
 
   // Also return the row and/or column if requested
   if (aRowIndex || aColIndex) {
     int32_t startRowIndex, startColIndex;
     rv = GetCellIndexes(cell, &startRowIndex, &startColIndex);
     if (NS_FAILED(rv)) {
       return rv;
     }
@@ -3134,56 +3018,51 @@ HTMLEditor::GetFirstSelectedCellInTable(
       *aColIndex = startColIndex;
     }
   }
 
   return NS_OK;
 }
 
 void
-HTMLEditor::SetSelectionAfterTableEdit(nsIDOMElement* aTable,
+HTMLEditor::SetSelectionAfterTableEdit(Element* aTable,
                                        int32_t aRow,
                                        int32_t aCol,
                                        int32_t aDirection,
                                        bool aSelected)
 {
   if (NS_WARN_IF(!aTable) || Destroyed()) {
     return;
   }
 
   RefPtr<Selection> selection = GetSelection();
   if (!selection) {
     return;
   }
 
-  nsCOMPtr<nsIDOMElement> cell;
+  RefPtr<Element> cell;
   bool done = false;
   do {
     nsresult rv = GetCellAt(aTable, aRow, aCol, getter_AddRefs(cell));
     if (NS_FAILED(rv)) {
       break;
     }
 
     if (cell) {
       if (aSelected) {
         // Reselect the cell
-        // Remove "temp" once we nix nsIDOMElement from here.
-        nsCOMPtr<Element> temp = do_QueryInterface(cell);
-        SelectElement(temp);
+        SelectElement(cell);
         return;
       }
 
       // Set the caret to deepest first child
       //   but don't go into nested tables
       // TODO: Should we really be placing the caret at the END
       //  of the cell content?
-      nsCOMPtr<nsINode> cellNode = do_QueryInterface(cell);
-      if (cellNode) {
-        CollapseSelectionToDeepestNonTableFirstChild(selection, cellNode);
-      }
+      CollapseSelectionToDeepestNonTableFirstChild(selection, cell);
       return;
     }
 
     // Setup index to find another cell in the
     //   direction requested, but move in other direction if already at
     //   beginning of row or column
     switch (aDirection) {
       case ePreviousColumn:
@@ -3210,50 +3089,45 @@ HTMLEditor::SetSelectionAfterTableEdit(n
         break;
       default:
         done = true;
     }
   } while (!done);
 
   // We didn't find a cell
   // Set selection to just before the table
-  nsCOMPtr<nsINode> table = do_QueryInterface(aTable);
-  if (table->GetParentNode()) {
-    nsCOMPtr<nsIContent> table = do_QueryInterface(aTable);
-    if (NS_WARN_IF(!table)) {
-      return;
-    }
-    EditorRawDOMPoint atTable(table);
+  if (aTable->GetParentNode()) {
+    EditorRawDOMPoint atTable(aTable);
     if (NS_WARN_IF(!atTable.IsSetAndValid())) {
       return;
     }
     selection->Collapse(atTable);
     return;
   }
   // Last resort: Set selection to start of doc
   // (it's very bad to not have a valid selection!)
   SetSelectionAtDocumentStart(selection);
 }
 
 NS_IMETHODIMP
 HTMLEditor::GetSelectedOrParentTableElement(nsAString& aTagName,
                                             int32_t* aSelectedCount,
-                                            nsIDOMElement** aTableElement)
+                                            Element** aTableElement)
 {
   NS_ENSURE_ARG_POINTER(aTableElement);
   NS_ENSURE_ARG_POINTER(aSelectedCount);
   *aTableElement = nullptr;
   aTagName.Truncate();
   *aSelectedCount = 0;
 
   RefPtr<Selection> selection = GetSelection();
   NS_ENSURE_TRUE(selection, NS_ERROR_FAILURE);
 
   // Try to get the first selected cell
-  nsCOMPtr<nsIDOMElement> tableOrCellElement;
+  RefPtr<Element> tableOrCellElement;
   nsresult rv = GetFirstSelectedCell(nullptr,
                                      getter_AddRefs(tableOrCellElement));
   NS_ENSURE_SUCCESS(rv, rv);
 
   NS_NAMED_LITERAL_STRING(tdName, "td");
 
   if (tableOrCellElement) {
       // Each cell is in its own selection range,
@@ -3270,73 +3144,65 @@ HTMLEditor::GetSelectedOrParentTableElem
     if (anchorNode->HasChildNodes()) {
       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);
+          tableOrCellElement = selectedNode->AsElement();
           aTagName = tdName;
           // Each cell is in its own selection range,
           //  so count signals multiple-cell selection
           *aSelectedCount = selection->RangeCount();
         } else if (selectedNode->IsHTMLElement(nsGkAtoms::table)) {
-          tableOrCellElement = do_QueryInterface(selectedNode);
+          tableOrCellElement = selectedNode->AsElement();
           aTagName.AssignLiteral("table");
           *aSelectedCount = 1;
         } else if (selectedNode->IsHTMLElement(nsGkAtoms::tr)) {
-          tableOrCellElement = do_QueryInterface(selectedNode);
+          tableOrCellElement = selectedNode->AsElement();
           aTagName.AssignLiteral("tr");
           *aSelectedCount = 1;
         }
       }
     }
     if (!tableOrCellElement) {
       // Didn't find a table element -- find a cell parent
-      rv = GetElementOrParentByTagName(tdName, GetAsDOMNode(anchorNode),
-                                       getter_AddRefs(tableOrCellElement));
-      if (NS_FAILED(rv)) {
-        return rv;
-      }
+      tableOrCellElement = GetElementOrParentByTagName(tdName, anchorNode);
       if (tableOrCellElement) {
         aTagName = tdName;
       }
     }
   }
   if (tableOrCellElement) {
-    *aTableElement = tableOrCellElement.get();
-    NS_ADDREF(*aTableElement);
+    tableOrCellElement.forget(aTableElement);
   }
   return NS_OK;
 }
 
 NS_IMETHODIMP
-HTMLEditor::GetSelectedCellsType(nsIDOMElement* aElement,
+HTMLEditor::GetSelectedCellsType(Element* aElement,
                                  uint32_t* aSelectionType)
 {
   NS_ENSURE_ARG_POINTER(aSelectionType);
   *aSelectionType = 0;
 
   // Be sure we have a table element
   //  (if aElement is null, this uses selection's anchor node)
-  nsCOMPtr<nsIDOMElement> table;
-
-  nsresult rv =
-    GetElementOrParentByTagName(NS_LITERAL_STRING("table"), aElement,
-                                getter_AddRefs(table));
-  NS_ENSURE_SUCCESS(rv, rv);
-
+  RefPtr<Element> table =
+    GetElementOrParentByTagName(NS_LITERAL_STRING("table"), aElement);
+
+  // table might be null at this point, but if so GetTableSize will fail.
   int32_t rowCount, colCount;
-  rv = GetTableSize(table, &rowCount, &colCount);
+  nsresult rv = GetTableSize(table, &rowCount, &colCount);
   NS_ENSURE_SUCCESS(rv, rv);
 
   // Traverse all selected cells
-  nsCOMPtr<nsIDOMElement> selectedCell;
+  RefPtr<Element> selectedCell;
   rv = GetFirstSelectedCell(nullptr, getter_AddRefs(selectedCell));
   NS_ENSURE_SUCCESS(rv, rv);
   if (rv == NS_SUCCESS_EDITOR_ELEMENT_NOT_FOUND) {
     return NS_OK;
   }
 
   // We have at least one selected cell, so set return value
   *aSelectionType = nsISelectionPrivate::TABLESELECTION_CELL;
@@ -3397,28 +3263,28 @@ HTMLEditor::GetSelectedCellsType(nsIDOME
   if (allCellsInColAreSelected) {
     *aSelectionType = nsISelectionPrivate::TABLESELECTION_COLUMN;
   }
 
   return NS_OK;
 }
 
 bool
-HTMLEditor::AllCellsInRowSelected(nsIDOMElement* aTable,
+HTMLEditor::AllCellsInRowSelected(Element* aTable,
                                   int32_t aRowIndex,
                                   int32_t aNumberOfColumns)
 {
   NS_ENSURE_TRUE(aTable, false);
 
   int32_t curStartRowIndex, curStartColIndex, rowSpan, colSpan, actualRowSpan, actualColSpan;
   bool    isSelected;
 
   for (int32_t col = 0; col < aNumberOfColumns;
        col += std::max(actualColSpan, 1)) {
-    nsCOMPtr<nsIDOMElement> cell;
+    RefPtr<Element> cell;
     nsresult rv = GetCellDataAt(aTable, aRowIndex, col, getter_AddRefs(cell),
                                 &curStartRowIndex, &curStartColIndex,
                                 &rowSpan, &colSpan,
                                 &actualRowSpan, &actualColSpan, &isSelected);
 
     NS_ENSURE_SUCCESS(rv, false);
     // If no cell, we may have a "ragged" right edge,
     //   so return TRUE only if we already found a cell in the row
@@ -3428,28 +3294,28 @@ HTMLEditor::AllCellsInRowSelected(nsIDOM
     NS_ENSURE_TRUE(isSelected, false);
 
     NS_ASSERTION((actualColSpan > 0),"ActualColSpan = 0 in AllCellsInRowSelected");
   }
   return true;
 }
 
 bool
-HTMLEditor::AllCellsInColumnSelected(nsIDOMElement* aTable,
+HTMLEditor::AllCellsInColumnSelected(Element* aTable,
                                      int32_t aColIndex,
                                      int32_t aNumberOfRows)
 {
   NS_ENSURE_TRUE(aTable, false);
 
   int32_t curStartRowIndex, curStartColIndex, rowSpan, colSpan, actualRowSpan, actualColSpan;
   bool    isSelected;
 
   for (int32_t row = 0; row < aNumberOfRows;
        row += std::max(actualRowSpan, 1)) {
-    nsCOMPtr<nsIDOMElement> cell;
+    RefPtr<Element> cell;
     nsresult rv = GetCellDataAt(aTable, row, aColIndex, getter_AddRefs(cell),
                                 &curStartRowIndex, &curStartColIndex,
                                 &rowSpan, &colSpan,
                                 &actualRowSpan, &actualColSpan, &isSelected);
 
     NS_ENSURE_SUCCESS(rv, false);
     // If no cell, we must have a "ragged" right edge on the last column
     //   so return TRUE only if we already found a cell in the row
--- a/editor/nsITableEditor.idl
+++ b/editor/nsITableEditor.idl
@@ -2,21 +2,22 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 
 #include "nsISupports.idl"
 
 interface nsIDOMNode;
-interface nsIDOMElement;
 interface nsIDOMRange;
 
+webidl Element;
+webidl Node;
+
 [scriptable, builtinclass, uuid(4805e684-49b9-11d3-9ce4-ed60bd6cb5bc)]
-
 interface nsITableEditor : nsISupports
 {
   const short eNoSearch = 0;
   const short ePreviousColumn = 1;
   const short ePreviousRow = 2;
 
   /* ------------ Table editing Methods -------------- */
 
@@ -74,33 +75,33 @@ interface nsITableEditor : nsISupports
   /** Select a rectangular block of cells:
     *  all cells falling within the row/column index of aStartCell
     *  to through the row/column index of the aEndCell
     *  aStartCell can be any location relative to aEndCell,
     *   as long as they are in the same table
     *  @param aStartCell  starting cell in block
     *  @param aEndCell    ending cell in block
     */
-  void selectBlockOfCells(in nsIDOMElement aStartCell,
-                          in nsIDOMElement aEndCell);
+  void selectBlockOfCells(in Element aStartCell,
+                          in Element aEndCell);
 
   void selectTableRow();
   void selectTableColumn();
   void selectTable();
   void selectAllTableCells();
 
   /** Create a new TD or TH element, the opposite type of the supplied aSourceCell
     *   1. Copy all attributes from aSourceCell to the new cell
     *   2. Move all contents of aSourceCell to the new cell
     *   3. Replace aSourceCell in the table with the new cell
     *
     *  @param aSourceCell   The cell to be replaced
     *  @return              The new cell that replaces aSourceCell
     */
-  nsIDOMElement switchTableCellHeaderType(in nsIDOMElement aSourceCell);
+  Element switchTableCellHeaderType(in Element aSourceCell);
 
   /** Merges contents of all selected cells
     * for selected cells that are adjacent,
     * this will result in a larger cell with appropriate
     * rowspan and colspan, and original cells are deleted
     * The resulting cell is in the location of the
     *   cell at the upper-left corner of the adjacent
     *   block of selected cells
@@ -132,49 +133,49 @@ interface nsITableEditor : nsISupports
     *   Used after inserting single cells or pasting
     *   a collection of cells that extend past the
     *   previous size of the table
     * If aTable is null, it uses table enclosing the selection anchor
     * This doesn't doesn't change the selection,
     *   thus it can be used to fixup all tables
     *   in a page independent of the selection
     */
-  void normalizeTable(in nsIDOMElement aTable);
+  void normalizeTable(in Element aTable);
 
   /** Get the row an column index from the layout's cellmap
     * If aCell is null, it will try to find enclosing table of selection anchor
     *
     */
-  void getCellIndexes(in nsIDOMElement aCell,
+  void getCellIndexes(in Element aCell,
                       out long aRowIndex, out long aColIndex);
 
   /** Get the number of rows and columns in a table from the layout's cellmap
     * If aTable is null, it will try to find enclosing table of selection ancho
     * Note that all rows in table will not have this many because of
     * ROWSPAN effects or if table is not "rectangular" (has short rows)
     */
-  void getTableSize(in nsIDOMElement aTable,
+  void getTableSize(in Element aTable,
                     out long aRowCount, out long aColCount);
 
   /** Get a cell element at cellmap grid coordinates
     * A cell that spans across multiple cellmap locations will
     *   be returned multiple times, once for each location it occupies
     *
     * @param aTable                   A table in the document
     * @param aRowIndex, aColIndex     The 0-based cellmap indexes
     *
     * (in C++ returns: NS_EDITOR_ELEMENT_NOT_FOUND if an element is not found
     *  passes NS_SUCCEEDED macro)
     *
     *   You can scan for all cells in a row or column
     *   by iterating through the appropriate indexes
     *   until the returned aCell is null
     */
-  nsIDOMElement getCellAt(in nsIDOMElement aTable,
-                          in long aRowIndex, in long aColIndex);
+  Element getCellAt(in Element aTable,
+                    in long aRowIndex, in long aColIndex);
 
   /** Get a cell at cellmap grid coordinates and associated data
     * A cell that spans across multiple cellmap locations will
     *   be returned multiple times, once for each location it occupies
     * Examine the returned aStartRowIndex and aStartColIndex to see
     *   if it is in the same layout column or layout row:
     *   A "layout row" is all cells sharing the same top edge
     *   A "layout column" is all cells sharing the same left edge
@@ -191,44 +192,44 @@ interface nsITableEditor : nsISupports
     *  @param aActualRowSpan           The actual number of cellmap locations (rows) spanned by the cell
     *  @param aActualColSpan           The actual number of cellmap locations (columns) spanned by the cell
     *  @param aIsSelected
     *  @param
     *
     * (in C++ returns: NS_EDITOR_ELEMENT_NOT_FOUND if an element is not found
     *  passes NS_SUCCEEDED macro)
     */
-  void getCellDataAt(in nsIDOMElement aTable,
+  void getCellDataAt(in Element aTable,
                      in long  aRowIndex, in long  aColIndex,
-                     out nsIDOMElement aCell,
+                     out Element aCell,
                      out long  aStartRowIndex, out long  aStartColIndex,
                      out long  aRowSpan, out long  aColSpan,
                      out long  aActualRowSpan, out long  aActualColSpan,
                      out boolean aIsSelected);
 
   /** Get the first row element in a table
     *
     * @return            The row at the requested index
     *                    Returns null if there are no rows in table
     * (in C++ returns: NS_EDITOR_ELEMENT_NOT_FOUND if an element is not found
     *  passes NS_SUCCEEDED macro)
     */
-  nsIDOMNode getFirstRow(in nsIDOMElement aTableElement);
+  Node getFirstRow(in Element aTableElement);
 
   /** Get the next row element starting the search from aTableElement
     *
     * @param aTableElement Any TR or child-of-TR element in the document
     *
     * @return            The row to start search from
     *                    and the row returned from the search
     *                    Returns null if there isn't another row
     * (in C++ returns: NS_EDITOR_ELEMENT_NOT_FOUND if an element is not found
     *  passes NS_SUCCEEDED macro)
     */
-  nsIDOMNode getNextRow(in nsIDOMNode aTableElement);
+  Node getNextRow(in Node aTableElement);
 
   /** Preferred direction to search for neighboring cell
     * when trying to locate a cell to place caret in after
     * a table editing action.
     * Used for aDirection param in SetSelectionAfterTableEdit
     */
 
   /** Examine the current selection and find
@@ -240,17 +241,17 @@ interface nsITableEditor : nsISupports
     *                         Note that "td" will be returned if name
     *                         is actually "th"
     * @param aCount           How many table elements were selected
     *                         This tells us if we have multiple cells selected
     *                           (0 if element is a parent cell of selection)
     * @return                 The table element (table, row, or first selected cell)
     *
     */
-  nsIDOMElement getSelectedOrParentTableElement(out AString aTagName, out long aCount);
+  Element getSelectedOrParentTableElement(out AString aTagName, out long aCount);
 
   /** Generally used after GetSelectedOrParentTableElement
     *   to test if selected cells are complete rows or columns
     *
     * @param aElement           Any table or cell element or any element
     *                           inside a table
     *                           Used to get enclosing table.
     *                           If null, selection's anchorNode is used
@@ -262,55 +263,55 @@ interface nsITableEditor : nsISupports
     *                              complete rows or columns are not selected
     *     TABLESELECTION_ROW       All cells are in 1 or more rows
     *                              and in each row, all cells selected
     *                              Note: This is the value if all rows
     *                              (thus all cells) are selected
     *     TABLESELECTION_COLUMN    All cells are in 1 or more columns
     *                              and in each column, all cells are selected
     */
-  uint32_t getSelectedCellsType(in nsIDOMElement aElement);
+  uint32_t getSelectedCellsType(in Element aElement);
 
   /** Get first selected element from first selection range.
     *   (If multiple cells were selected this is the first in the order they were selected)
     * Assumes cell-selection model where each cell
     * is in a separate range (selection parent node is table row)
     * @param aCell     [OUT] Selected cell or null if ranges don't contain
     *                  cell selections
     * @param aRange    [OUT] Optional: if not null, return the selection range
     *                     associated with the cell
     * Returns the DOM cell element
     *   (in C++: returns NS_EDITOR_ELEMENT_NOT_FOUND if an element is not found
     *    passes NS_SUCCEEDED macro)
     */
-  nsIDOMElement getFirstSelectedCell(out nsIDOMRange aRange);
+  Element getFirstSelectedCell(out nsIDOMRange aRange);
 
   /** Get first selected element in the table
     *   This is the upper-left-most selected cell in table,
     *   ignoring the order that the user selected them (order in the selection ranges)
     * Assumes cell-selection model where each cell
     * is in a separate range (selection parent node is table row)
     * @param aCell       Selected cell or null if ranges don't contain
     *                    cell selections
     * @param aRowIndex   Optional: if not null, return row index of 1st cell
     * @param aColIndex   Optional: if not null, return column index of 1st cell
     *
     * Returns the DOM cell element
     *   (in C++: returns NS_EDITOR_ELEMENT_NOT_FOUND if an element is not found
     *    passes NS_SUCCEEDED macro)
     */
-  nsIDOMElement getFirstSelectedCellInTable(out long aRowIndex, out long aColIndex);
+  Element getFirstSelectedCellInTable(out long aRowIndex, out long aColIndex);
 
   /** Get next selected cell element from first selection range.
     * Assumes cell-selection model where each cell
     * is in a separate range (selection parent node is table row)
     * Always call GetFirstSelectedCell() to initialize stored index of "next" cell
     * @param aCell     Selected cell or null if no more selected cells
     *                     or ranges don't contain cell selections
     * @param aRange    Optional: if not null, return the selection range
     *                     associated with the cell
     *
     * Returns the DOM cell element
     *   (in C++: returns NS_EDITOR_ELEMENT_NOT_FOUND if an element is not found
     *    passes NS_SUCCEEDED macro)
     */
-  nsIDOMElement getNextSelectedCell(out nsIDOMRange aRange);
+  Element getNextSelectedCell(out nsIDOMRange aRange);
 };