Bug 1455674 part 12. Remove nsIDOMElement use from xpidl in layout. r=mossop
authorBoris Zbarsky <bzbarsky@mit.edu>
Thu, 26 Apr 2018 23:36:30 -0400
changeset 472092 5add57b9a0cc9a37590bbd970ad04ad1d2f3cf7e
parent 472091 a19af55d0bd25817a0902b7fa771fb392cb0791b
child 472093 7f3223b376bb4f473b44e8d3d504df58b70652a0
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)
reviewersmossop
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 12. Remove nsIDOMElement use from xpidl in layout. r=mossop
accessible/base/nsCoreUtils.cpp
accessible/xul/XULTreeGridAccessible.cpp
dom/xul/nsXULElement.cpp
layout/base/PresShell.cpp
layout/xul/BoxObject.cpp
layout/xul/BoxObject.h
layout/xul/ListBoxObject.cpp
layout/xul/nsIBoxObject.idl
layout/xul/nsIListBoxObject.idl
layout/xul/nsListBoxBodyFrame.cpp
layout/xul/nsListBoxBodyFrame.h
layout/xul/nsPIBoxObject.h
layout/xul/tree/TreeBoxObject.cpp
layout/xul/tree/nsITreeBoxObject.idl
layout/xul/tree/nsITreeColumns.idl
layout/xul/tree/nsITreeContentView.idl
layout/xul/tree/nsTreeBodyFrame.cpp
layout/xul/tree/nsTreeBodyFrame.h
layout/xul/tree/nsTreeColumns.cpp
layout/xul/tree/nsTreeColumns.h
layout/xul/tree/nsTreeContentView.cpp
layout/xul/tree/nsTreeSelection.cpp
--- a/accessible/base/nsCoreUtils.cpp
+++ b/accessible/base/nsCoreUtils.cpp
@@ -69,23 +69,22 @@ nsCoreUtils::HasClickListener(nsIContent
      listenerManager->HasListenersFor(nsGkAtoms::onmouseup));
 }
 
 void
 nsCoreUtils::DispatchClickEvent(nsITreeBoxObject *aTreeBoxObj,
                                 int32_t aRowIndex, nsITreeColumn *aColumn,
                                 const nsAString& aPseudoElt)
 {
-  nsCOMPtr<nsIDOMElement> tcElm;
+  RefPtr<dom::Element> tcElm;
   aTreeBoxObj->GetTreeBody(getter_AddRefs(tcElm));
   if (!tcElm)
     return;
 
-  nsCOMPtr<nsIContent> tcContent(do_QueryInterface(tcElm));
-  nsIDocument *document = tcContent->GetUncomposedDoc();
+  nsIDocument *document = tcElm->GetUncomposedDoc();
   if (!document)
     return;
 
   nsCOMPtr<nsIPresShell> presShell = document->GetShell();
   if (!presShell)
     return;
 
   // Ensure row is visible.
@@ -105,36 +104,36 @@ nsCoreUtils::DispatchClickEvent(nsITreeB
 
   int32_t tcX = 0;
   tcBoxObj->GetX(&tcX);
 
   int32_t tcY = 0;
   tcBoxObj->GetY(&tcY);
 
   // Dispatch mouse events.
-  AutoWeakFrame tcFrame = tcContent->GetPrimaryFrame();
+  AutoWeakFrame tcFrame = tcElm->GetPrimaryFrame();
   nsIFrame* rootFrame = presShell->GetRootFrame();
 
   nsPoint offset;
   nsIWidget *rootWidget =
     rootFrame->GetView()->GetNearestWidget(&offset);
 
   RefPtr<nsPresContext> presContext = presShell->GetPresContext();
 
   int32_t cnvdX = presContext->CSSPixelsToDevPixels(tcX + x + 1) +
     presContext->AppUnitsToDevPixels(offset.x);
   int32_t cnvdY = presContext->CSSPixelsToDevPixels(tcY + y + 1) +
     presContext->AppUnitsToDevPixels(offset.y);
 
   // XUL is just desktop, so there is no real reason for senfing touch events.
   DispatchMouseEvent(eMouseDown, cnvdX, cnvdY,
-                     tcContent, tcFrame, presShell, rootWidget);
+                     tcElm, tcFrame, presShell, rootWidget);
 
   DispatchMouseEvent(eMouseUp, cnvdX, cnvdY,
-                     tcContent, tcFrame, presShell, rootWidget);
+                     tcElm, tcFrame, presShell, rootWidget);
 }
 
 void
 nsCoreUtils::DispatchMouseEvent(EventMessage aMessage, int32_t aX, int32_t aY,
                                 nsIContent *aContent, nsIFrame *aFrame,
                                 nsIPresShell *aPresShell, nsIWidget *aRootWidget)
 {
   WidgetMouseEvent event(true, aMessage, aRootWidget,
@@ -486,20 +485,19 @@ nsCoreUtils::GetLanguageFor(nsIContent *
          (!walkUp->IsElement() ||
           !walkUp->AsElement()->GetAttr(kNameSpaceID_None, nsGkAtoms::lang, aLanguage)))
     walkUp = walkUp->GetParent();
 }
 
 already_AddRefed<nsIBoxObject>
 nsCoreUtils::GetTreeBodyBoxObject(nsITreeBoxObject *aTreeBoxObj)
 {
-  nsCOMPtr<nsIDOMElement> tcElm;
+  RefPtr<dom::Element> tcElm;
   aTreeBoxObj->GetTreeBody(getter_AddRefs(tcElm));
-  nsCOMPtr<nsIContent> tcContent(do_QueryInterface(tcElm));
-  RefPtr<nsXULElement> tcXULElm = nsXULElement::FromNodeOrNull(tcContent);
+  RefPtr<nsXULElement> tcXULElm = nsXULElement::FromNodeOrNull(tcElm);
   if (!tcXULElm)
     return nullptr;
 
   return tcXULElm->GetBoxObject(IgnoreErrors());
 }
 
 already_AddRefed<nsITreeBoxObject>
 nsCoreUtils::GetTreeBoxObject(nsIContent *aContent)
@@ -609,20 +607,19 @@ nsCoreUtils::GetPreviousSensibleColumn(n
   }
 
   return prevColumn.forget();
 }
 
 bool
 nsCoreUtils::IsColumnHidden(nsITreeColumn *aColumn)
 {
-  nsCOMPtr<nsIDOMElement> element;
+  RefPtr<Element> element;
   aColumn->GetElement(getter_AddRefs(element));
-  nsCOMPtr<Element> content = do_QueryInterface(element);
-  return content->AttrValueIs(kNameSpaceID_None, nsGkAtoms::hidden,
+  return element->AttrValueIs(kNameSpaceID_None, nsGkAtoms::hidden,
                               nsGkAtoms::_true, eCaseMatters);
 }
 
 void
 nsCoreUtils::ScrollTo(nsIPresShell* aPresShell, nsIContent* aContent,
                       uint32_t aScrollType)
 {
   nsIPresShell::ScrollAxis vertical, horizontal;
--- a/accessible/xul/XULTreeGridAccessible.cpp
+++ b/accessible/xul/XULTreeGridAccessible.cpp
@@ -15,18 +15,20 @@
 #include "States.h"
 #include "nsQueryObject.h"
 
 #include "nsIBoxObject.h"
 #include "nsIMutableArray.h"
 #include "nsIPersistentProperties2.h"
 #include "nsITreeSelection.h"
 #include "nsComponentManagerUtils.h"
+#include "mozilla/dom/Element.h"
 
 using namespace mozilla::a11y;
+using namespace mozilla;
 
 XULTreeGridAccessible::~XULTreeGridAccessible()
 {
 }
 
 ////////////////////////////////////////////////////////////////////////////////
 // XULTreeGridAccessible: Table
 
@@ -626,21 +628,20 @@ uint32_t
 XULTreeGridCellAccessible::RowIdx() const
 {
   return mRow;
 }
 
 void
 XULTreeGridCellAccessible::ColHeaderCells(nsTArray<Accessible*>* aHeaderCells)
 {
-  nsCOMPtr<nsIDOMElement> columnElm;
+  RefPtr<dom::Element> columnElm;
   mColumn->GetElement(getter_AddRefs(columnElm));
 
-  nsCOMPtr<nsIContent> columnContent(do_QueryInterface(columnElm));
-  Accessible* headerCell = mDoc->GetAccessible(columnContent);
+  Accessible* headerCell = mDoc->GetAccessible(columnElm);
   if (headerCell)
     aHeaderCells->AppendElement(headerCell);
 }
 
 bool
 XULTreeGridCellAccessible::Selected()
 {
   nsCOMPtr<nsITreeSelection> selection;
@@ -821,24 +822,23 @@ XULTreeGridCellAccessible::IsEditable() 
 
   // XXX: logic corresponds to tree.xml, it's preferable to have interface
   // method to check it.
   bool isEditable = false;
   nsresult rv = mTreeView->IsEditable(mRow, mColumn, &isEditable);
   if (NS_FAILED(rv) || !isEditable)
     return false;
 
-  nsCOMPtr<nsIDOMElement> columnElm;
+  RefPtr<dom::Element> columnElm;
   mColumn->GetElement(getter_AddRefs(columnElm));
   if (!columnElm)
     return false;
 
-  nsCOMPtr<Element> columnContent(do_QueryInterface(columnElm));
-  if (!columnContent->AttrValueIs(kNameSpaceID_None,
-                                  nsGkAtoms::editable,
-                                  nsGkAtoms::_true,
-                                  eCaseMatters))
+  if (!columnElm->AttrValueIs(kNameSpaceID_None,
+                              nsGkAtoms::editable,
+                              nsGkAtoms::_true,
+                              eCaseMatters))
     return false;
 
   return mContent->AsElement()->AttrValueIs(kNameSpaceID_None,
                                             nsGkAtoms::editable,
                                             nsGkAtoms::_true, eCaseMatters);
 }
--- a/dom/xul/nsXULElement.cpp
+++ b/dom/xul/nsXULElement.cpp
@@ -828,44 +828,42 @@ nsXULElement::RemoveChildAt_Deprecated(u
       controlElement = do_QueryObject(this);
 
       // If it's not, look at our parent
       if (!controlElement)
         GetParentTree(getter_AddRefs(controlElement));
       nsCOMPtr<nsIContent> controlContent(do_QueryInterface(controlElement));
       RefPtr<nsXULElement> xulElement = FromNodeOrNull(controlContent);
 
-      nsCOMPtr<nsIDOMElement> oldKidElem = do_QueryInterface(oldKid);
-      if (xulElement && oldKidElem) {
+      if (xulElement) {
         // Iterate over all of the items and find out if they are contained inside
         // the removed subtree.
         int32_t length;
         controlElement->GetSelectedCount(&length);
         for (int32_t i = 0; i < length; i++) {
-          nsCOMPtr<nsIDOMXULSelectControlItemElement> node;
-          controlElement->MultiGetSelectedItem(i, getter_AddRefs(node));
-          // we need to QI here to do an XPCOM-correct pointercompare
-          nsCOMPtr<nsIDOMElement> selElem = do_QueryInterface(node);
-          if (selElem == oldKidElem &&
-              NS_SUCCEEDED(controlElement->RemoveItemFromSelection(node))) {
+          nsCOMPtr<nsIDOMXULSelectControlItemElement> item;
+          controlElement->MultiGetSelectedItem(i, getter_AddRefs(item));
+          nsCOMPtr<nsINode> node = do_QueryInterface(item);
+          if (node == oldKid &&
+              NS_SUCCEEDED(controlElement->RemoveItemFromSelection(item))) {
             length--;
             i--;
             fireSelectionHandler = true;
           }
         }
 
         nsCOMPtr<nsIDOMXULSelectControlItemElement> curItem;
         controlElement->GetCurrentItem(getter_AddRefs(curItem));
         nsCOMPtr<nsIContent> curNode = do_QueryInterface(curItem);
         if (curNode && nsContentUtils::ContentIsDescendantOf(curNode, oldKid)) {
             // Current item going away
             nsCOMPtr<nsIBoxObject> box = xulElement->GetBoxObject(IgnoreErrors());
             listBox = do_QueryInterface(box);
-            if (listBox && oldKidElem) {
-              listBox->GetIndexOfItem(oldKidElem, &newCurrentIndex);
+            if (listBox) {
+              listBox->GetIndexOfItem(oldKid->AsElement(), &newCurrentIndex);
             }
 
             // If any of this fails, we'll just set the current item to null
             if (newCurrentIndex == -1)
               newCurrentIndex = -2;
         }
       }
     }
@@ -875,17 +873,17 @@ nsXULElement::RemoveChildAt_Deprecated(u
     if (newCurrentIndex == -2) {
         controlElement->SetCurrentItem(nullptr);
     } else if (newCurrentIndex > -1) {
         // Make sure the index is still valid
         int32_t treeRows;
         listBox->GetRowCount(&treeRows);
         if (treeRows > 0) {
             newCurrentIndex = std::min((treeRows - 1), newCurrentIndex);
-            nsCOMPtr<nsIDOMElement> newCurrentItem;
+            RefPtr<Element> newCurrentItem;
             listBox->GetItemAtIndex(newCurrentIndex, getter_AddRefs(newCurrentItem));
             nsCOMPtr<nsIDOMXULSelectControlItemElement> xulCurItem = do_QueryInterface(newCurrentItem);
             if (xulCurItem)
                 controlElement->SetCurrentItem(xulCurItem);
         } else {
             controlElement->SetCurrentItem(nullptr);
         }
     }
@@ -922,44 +920,42 @@ nsXULElement::RemoveChildNode(nsIContent
       controlElement = do_QueryObject(this);
 
       // If it's not, look at our parent
       if (!controlElement)
         GetParentTree(getter_AddRefs(controlElement));
       nsCOMPtr<nsIContent> controlContent(do_QueryInterface(controlElement));
       RefPtr<nsXULElement> xulElement = FromNodeOrNull(controlContent);
 
-      nsCOMPtr<nsIDOMElement> oldKidElem = do_QueryInterface(aKid);
-      if (xulElement && oldKidElem) {
+      if (xulElement) {
         // Iterate over all of the items and find out if they are contained inside
         // the removed subtree.
         int32_t length;
         controlElement->GetSelectedCount(&length);
         for (int32_t i = 0; i < length; i++) {
-          nsCOMPtr<nsIDOMXULSelectControlItemElement> node;
-          controlElement->MultiGetSelectedItem(i, getter_AddRefs(node));
-          // we need to QI here to do an XPCOM-correct pointercompare
-          nsCOMPtr<nsIDOMElement> selElem = do_QueryInterface(node);
-          if (selElem == oldKidElem &&
-              NS_SUCCEEDED(controlElement->RemoveItemFromSelection(node))) {
+          nsCOMPtr<nsIDOMXULSelectControlItemElement> item;
+          controlElement->MultiGetSelectedItem(i, getter_AddRefs(item));
+          nsCOMPtr<nsINode> node = do_QueryInterface(item);
+          if (node == aKid &&
+              NS_SUCCEEDED(controlElement->RemoveItemFromSelection(item))) {
             length--;
             i--;
             fireSelectionHandler = true;
           }
         }
 
         nsCOMPtr<nsIDOMXULSelectControlItemElement> curItem;
         controlElement->GetCurrentItem(getter_AddRefs(curItem));
         nsCOMPtr<nsIContent> curNode = do_QueryInterface(curItem);
         if (curNode && nsContentUtils::ContentIsDescendantOf(curNode, aKid)) {
             // Current item going away
             nsCOMPtr<nsIBoxObject> box = xulElement->GetBoxObject(IgnoreErrors());
             listBox = do_QueryInterface(box);
-            if (listBox && oldKidElem) {
-              listBox->GetIndexOfItem(oldKidElem, &newCurrentIndex);
+            if (listBox) {
+              listBox->GetIndexOfItem(aKid->AsElement(), &newCurrentIndex);
             }
 
             // If any of this fails, we'll just set the current item to null
             if (newCurrentIndex == -1)
               newCurrentIndex = -2;
         }
       }
     }
@@ -969,17 +965,17 @@ nsXULElement::RemoveChildNode(nsIContent
     if (newCurrentIndex == -2) {
         controlElement->SetCurrentItem(nullptr);
     } else if (newCurrentIndex > -1) {
         // Make sure the index is still valid
         int32_t treeRows;
         listBox->GetRowCount(&treeRows);
         if (treeRows > 0) {
             newCurrentIndex = std::min((treeRows - 1), newCurrentIndex);
-            nsCOMPtr<nsIDOMElement> newCurrentItem;
+            RefPtr<Element> newCurrentItem;
             listBox->GetItemAtIndex(newCurrentIndex, getter_AddRefs(newCurrentItem));
             nsCOMPtr<nsIDOMXULSelectControlItemElement> xulCurItem = do_QueryInterface(newCurrentItem);
             if (xulCurItem)
                 controlElement->SetCurrentItem(xulCurItem);
         } else {
             controlElement->SetCurrentItem(nullptr);
         }
     }
--- a/layout/base/PresShell.cpp
+++ b/layout/base/PresShell.cpp
@@ -8382,21 +8382,20 @@ PresShell::GetCurrentItemAndPositionForE
           istree = true;
 
           nsCOMPtr<nsITreeColumns> cols;
           treeBox->GetColumns(getter_AddRefs(cols));
           if (cols) {
             nsCOMPtr<nsITreeColumn> col;
             cols->GetFirstColumn(getter_AddRefs(col));
             if (col) {
-              nsCOMPtr<nsIDOMElement> colElement;
+              RefPtr<Element> colElement;
               col->GetElement(getter_AddRefs(colElement));
-              nsCOMPtr<nsIContent> colContent(do_QueryInterface(colElement));
-              if (colContent) {
-                nsIFrame* frame = colContent->GetPrimaryFrame();
+              if (colElement) {
+                nsIFrame* frame = colElement->GetPrimaryFrame();
                 if (frame) {
                   extraTreeY += frame->GetSize().height;
                 }
               }
             }
           }
         }
         else {
--- a/layout/xul/BoxObject.cpp
+++ b/layout/xul/BoxObject.cpp
@@ -63,32 +63,29 @@ BoxObject::BoxObject()
 {
 }
 
 BoxObject::~BoxObject()
 {
 }
 
 NS_IMETHODIMP
-BoxObject::GetElement(nsIDOMElement** aResult)
+BoxObject::GetElement(Element** aResult)
 {
-  if (mContent) {
-    return CallQueryInterface(mContent, aResult);
-  }
-
-  *aResult = nullptr;
+  RefPtr<Element> element = mContent;
+  element.forget(aResult);
   return NS_OK;
 }
 
 // nsPIBoxObject //////////////////////////////////////////////////////////////////////////
 
 nsresult
-BoxObject::Init(nsIContent* aContent)
+BoxObject::Init(Element* aElement)
 {
-  mContent = aContent;
+  mContent = aElement;
   return NS_OK;
 }
 
 void
 BoxObject::Clear()
 {
   mPropertyTable = nullptr;
   mContent = nullptr;
@@ -350,116 +347,144 @@ BoxObject::RemoveProperty(const char16_t
 
   if (!mPropertyTable) return NS_OK;
 
   nsDependentString propertyName(aPropertyName);
   mPropertyTable->Remove(propertyName);
   return NS_OK;
 }
 
-NS_IMETHODIMP
-BoxObject::GetParentBox(nsIDOMElement * *aParentBox)
+Element*
+BoxObject::GetParentBox()
 {
-  *aParentBox = nullptr;
   nsIFrame* frame = GetFrame(false);
-  if (!frame) return NS_OK;
+  if (!frame) {
+    return nullptr;
+  }
+
   nsIFrame* parent = frame->GetParent();
-  if (!parent) return NS_OK;
+  if (!parent) {
+    return nullptr;
+  }
 
-  nsCOMPtr<nsIDOMElement> el = do_QueryInterface(parent->GetContent());
-  *aParentBox = el;
-  NS_IF_ADDREF(*aParentBox);
-  return NS_OK;
+  nsIContent* parentContent = parent->GetContent();
+  // In theory parent could be viewport, and then parentContent is null.
+  if (parentContent && parentContent->IsElement()) {
+    return parentContent->AsElement();
+  }
+
+  return nullptr;
 }
 
-NS_IMETHODIMP
-BoxObject::GetFirstChild(nsIDOMElement * *aFirstVisibleChild)
+Element*
+BoxObject::GetFirstChild()
 {
-  *aFirstVisibleChild = nullptr;
   nsIFrame* frame = GetFrame(false);
-  if (!frame) return NS_OK;
+  if (!frame) {
+    return nullptr;
+  }
+
   nsIFrame* firstFrame = frame->PrincipalChildList().FirstChild();
-  if (!firstFrame) return NS_OK;
-  // get the content for the box and query to a dom element
-  nsCOMPtr<nsIDOMElement> el = do_QueryInterface(firstFrame->GetContent());
-  el.swap(*aFirstVisibleChild);
-  return NS_OK;
+  if (!firstFrame) {
+    return nullptr;
+  }
+
+  nsIContent* content = firstFrame->GetContent();
+  if (content->IsElement()) {
+    return content->AsElement();
+  }
+
+  return nullptr;
 }
 
-NS_IMETHODIMP
-BoxObject::GetLastChild(nsIDOMElement * *aLastVisibleChild)
+Element*
+BoxObject::GetLastChild()
 {
-  *aLastVisibleChild = nullptr;
   nsIFrame* frame = GetFrame(false);
-  if (!frame) return NS_OK;
-  return GetPreviousSibling(frame, nullptr, aLastVisibleChild);
+  if (!frame) {
+    return nullptr;
+  }
+  return GetPreviousSibling(frame, nullptr);
 }
 
-NS_IMETHODIMP
-BoxObject::GetNextSibling(nsIDOMElement **aNextOrdinalSibling)
+Element*
+BoxObject::GetNextSibling()
 {
-  *aNextOrdinalSibling = nullptr;
   nsIFrame* frame = GetFrame(false);
-  if (!frame) return NS_OK;
+  if (!frame) {
+    return nullptr;
+  }
+
   nsIFrame* nextFrame = frame->GetNextSibling();
-  if (!nextFrame) return NS_OK;
-  // get the content for the box and query to a dom element
-  nsCOMPtr<nsIDOMElement> el = do_QueryInterface(nextFrame->GetContent());
-  el.swap(*aNextOrdinalSibling);
-  return NS_OK;
+  if (!nextFrame) {
+    return nullptr;
+  }
+
+  nsIContent* content = nextFrame->GetContent();
+  if (content->IsElement()) {
+    return content->AsElement();
+  }
+
+  return nullptr;
 }
 
-NS_IMETHODIMP
-BoxObject::GetPreviousSibling(nsIDOMElement **aPreviousOrdinalSibling)
+Element*
+BoxObject::GetPreviousSibling()
 {
-  *aPreviousOrdinalSibling = nullptr;
   nsIFrame* frame = GetFrame(false);
-  if (!frame) return NS_OK;
+  if (!frame) {
+    return nullptr;
+  }
   nsIFrame* parentFrame = frame->GetParent();
-  if (!parentFrame) return NS_OK;
-  return GetPreviousSibling(parentFrame, frame, aPreviousOrdinalSibling);
+  if (!parentFrame) {
+    return nullptr;
+  }
+  return GetPreviousSibling(parentFrame, frame);
 }
 
-nsresult
-BoxObject::GetPreviousSibling(nsIFrame* aParentFrame, nsIFrame* aFrame,
-                              nsIDOMElement** aResult)
+Element*
+BoxObject::GetPreviousSibling(nsIFrame* aParentFrame, nsIFrame* aFrame)
 {
-  *aResult = nullptr;
   nsIFrame* nextFrame = aParentFrame->PrincipalChildList().FirstChild();
   nsIFrame* prevFrame = nullptr;
   while (nextFrame) {
     if (nextFrame == aFrame)
       break;
     prevFrame = nextFrame;
     nextFrame = nextFrame->GetNextSibling();
   }
 
-  if (!prevFrame) return NS_OK;
-  // get the content for the box and query to a dom element
-  nsCOMPtr<nsIDOMElement> el = do_QueryInterface(prevFrame->GetContent());
-  el.swap(*aResult);
-  return NS_OK;
+  if (!prevFrame) {
+    return nullptr;
+  }
+
+  nsIContent* content = prevFrame->GetContent();
+  if (!content->IsElement()) {
+    return nullptr;
+  }
+
+  return content->AsElement();
 }
 
-nsIContent*
+Element*
 BoxObject::GetParentObject() const
 {
   return mContent;
 }
 
 JSObject*
 BoxObject::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
 {
   return BoxObjectBinding::Wrap(aCx, this, aGivenProto);
 }
 
 Element*
 BoxObject::GetElement() const
 {
-  return mContent && mContent->IsElement() ? mContent->AsElement() : nullptr;
+  return mContent;
 }
 
 int32_t
 BoxObject::X()
 {
   int32_t ret = 0;
   GetX(&ret);
   return ret;
@@ -543,61 +568,16 @@ BoxObject::SetProperty(const nsAString& 
 }
 
 void
 BoxObject::RemoveProperty(const nsAString& propertyName)
 {
   RemoveProperty(PromiseFlatString(propertyName).get());
 }
 
-already_AddRefed<Element>
-BoxObject::GetParentBox()
-{
-  nsCOMPtr<nsIDOMElement> el;
-  GetParentBox(getter_AddRefs(el));
-  nsCOMPtr<Element> ret(do_QueryInterface(el));
-  return ret.forget();
-}
-
-already_AddRefed<Element>
-BoxObject::GetFirstChild()
-{
-  nsCOMPtr<nsIDOMElement> el;
-  GetFirstChild(getter_AddRefs(el));
-  nsCOMPtr<Element> ret(do_QueryInterface(el));
-  return ret.forget();
-}
-
-already_AddRefed<Element>
-BoxObject::GetLastChild()
-{
-  nsCOMPtr<nsIDOMElement> el;
-  GetLastChild(getter_AddRefs(el));
-  nsCOMPtr<Element> ret(do_QueryInterface(el));
-  return ret.forget();
-}
-
-already_AddRefed<Element>
-BoxObject::GetNextSibling()
-{
-  nsCOMPtr<nsIDOMElement> el;
-  GetNextSibling(getter_AddRefs(el));
-  nsCOMPtr<Element> ret(do_QueryInterface(el));
-  return ret.forget();
-}
-
-already_AddRefed<Element>
-BoxObject::GetPreviousSibling()
-{
-  nsCOMPtr<nsIDOMElement> el;
-  GetPreviousSibling(getter_AddRefs(el));
-  nsCOMPtr<Element> ret(do_QueryInterface(el));
-  return ret.forget();
-}
-
 } // namespace dom
 } // namespace mozilla
 
 // Creation Routine ///////////////////////////////////////////////////////////////////////
 
 using namespace mozilla::dom;
 
 nsresult
--- a/layout/xul/BoxObject.h
+++ b/layout/xul/BoxObject.h
@@ -34,32 +34,31 @@ class BoxObject : public nsPIBoxObject,
   NS_DECL_CYCLE_COLLECTING_ISUPPORTS
   NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(BoxObject)
   NS_DECL_NSIBOXOBJECT
 
 public:
   BoxObject();
 
   // nsPIBoxObject
-  virtual nsresult Init(nsIContent* aContent) override;
+  virtual nsresult Init(Element* aElement) override;
   virtual void Clear() override;
   virtual void ClearCachedValues() override;
 
   nsIFrame* GetFrame(bool aFlushLayout);
   nsIPresShell* GetPresShell(bool aFlushLayout);
   nsresult GetOffsetRect(nsIntRect& aRect);
   nsresult GetScreenPosition(nsIntPoint& aPoint);
 
   // Given a parent frame and a child frame, find the frame whose
   // next sibling is the given child frame and return its element
-  static nsresult GetPreviousSibling(nsIFrame* aParentFrame, nsIFrame* aFrame,
-                                     nsIDOMElement** aResult);
+  static Element* GetPreviousSibling(nsIFrame* aParentFrame, nsIFrame* aFrame);
 
   // WebIDL (wraps old impls)
-  nsIContent* GetParentObject() const;
+  Element* GetParentObject() const;
   virtual JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
 
   Element* GetElement() const;
 
   int32_t X();
   int32_t Y();
   int32_t GetScreenX(ErrorResult& aRv);
   int32_t GetScreenY(ErrorResult& aRv);
@@ -67,26 +66,26 @@ public:
   int32_t Height();
 
   already_AddRefed<nsISupports> GetPropertyAsSupports(const nsAString& propertyName);
   void SetPropertyAsSupports(const nsAString& propertyName, nsISupports* value);
   void GetProperty(const nsAString& propertyName, nsString& aRetVal, ErrorResult& aRv);
   void SetProperty(const nsAString& propertyName, const nsAString& propertyValue);
   void RemoveProperty(const nsAString& propertyName);
 
-  already_AddRefed<Element> GetParentBox();
-  already_AddRefed<Element> GetFirstChild();
-  already_AddRefed<Element> GetLastChild();
-  already_AddRefed<Element> GetNextSibling();
-  already_AddRefed<Element> GetPreviousSibling();
+  Element* GetParentBox();
+  Element* GetFirstChild();
+  Element* GetLastChild();
+  Element* GetNextSibling();
+  Element* GetPreviousSibling();
 
 protected:
   virtual ~BoxObject();
 
   nsAutoPtr<nsInterfaceHashtable<nsStringHashKey,nsISupports> > mPropertyTable; //[OWNER]
 
-  nsIContent* mContent; // [WEAK]
+  Element* mContent; // [WEAK]
 };
 
 } // namespace dom
 } // namespace mozilla
 
 #endif
--- a/layout/xul/ListBoxObject.cpp
+++ b/layout/xul/ListBoxObject.cpp
@@ -38,27 +38,27 @@ JSObject* ListBoxObject::WrapObject(JSCo
 NS_IMETHODIMP
 ListBoxObject::GetRowCount(int32_t *aResult)
 {
   *aResult = GetRowCount();
   return NS_OK;
 }
 
 NS_IMETHODIMP
-ListBoxObject::GetItemAtIndex(int32_t index, nsIDOMElement **_retval)
+ListBoxObject::GetItemAtIndex(int32_t index, Element **_retval)
 {
   nsListBoxBodyFrame* body = GetListBoxBody(true);
   if (body) {
     return body->GetItemAtIndex(index, _retval);
   }
   return NS_OK;
  }
 
 NS_IMETHODIMP
-ListBoxObject::GetIndexOfItem(nsIDOMElement* aElement, int32_t *aResult)
+ListBoxObject::GetIndexOfItem(Element* aElement, int32_t *aResult)
 {
   *aResult = 0;
 
   nsListBoxBodyFrame* body = GetListBoxBody(true);
   if (body) {
     return body->GetIndexOfItem(aElement, aResult);
   }
   return NS_OK;
@@ -131,28 +131,26 @@ ListBoxObject::ScrollByLines(int32_t aNu
   if (body) {
     body->ScrollByLines(aNumLines);
   }
 }
 
 already_AddRefed<Element>
 ListBoxObject::GetItemAtIndex(int32_t index)
 {
-  nsCOMPtr<nsIDOMElement> el;
+  RefPtr<Element> el;
   GetItemAtIndex(index, getter_AddRefs(el));
-  nsCOMPtr<Element> ret(do_QueryInterface(el));
-  return ret.forget();
+  return el.forget();
 }
 
 int32_t
 ListBoxObject::GetIndexOfItem(Element& aElement)
 {
   int32_t ret;
-  nsCOMPtr<nsIDOMElement> el(do_QueryInterface(&aElement));
-  GetIndexOfItem(el, &ret);
+  GetIndexOfItem(&aElement, &ret);
   return ret;
 }
 
 //////////////////////
 
 static nsIContent*
 FindBodyContent(nsIContent* aParent)
 {
--- a/layout/xul/nsIBoxObject.idl
+++ b/layout/xul/nsIBoxObject.idl
@@ -1,40 +1,33 @@
 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* 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 nsIDOMElement;
+webidl Element;
 
 [scriptable, uuid(ce572460-b0f2-4650-a9e7-c53a99d3b6ad)]
 interface nsIBoxObject : nsISupports
 {
-  readonly attribute nsIDOMElement element;
+  readonly attribute Element element;
 
   readonly attribute long x;
   readonly attribute long y;
   readonly attribute long screenX;
   readonly attribute long screenY;
   readonly attribute long width;
   readonly attribute long height;
 
   nsISupports getPropertyAsSupports(in wstring propertyName);
   void setPropertyAsSupports(in wstring propertyName, in nsISupports value);
   wstring getProperty(in wstring propertyName);
   void setProperty(in wstring propertyName, in wstring propertyValue);
   void removeProperty(in wstring propertyName);
-  
-  // for stepping through content in the expanded dom with box-ordinal-group order
-  readonly attribute nsIDOMElement parentBox;
-  readonly attribute nsIDOMElement firstChild;
-  readonly attribute nsIDOMElement lastChild;
-  readonly attribute nsIDOMElement nextSibling;
-  readonly attribute nsIDOMElement previousSibling;
 };
 
 %{C++
 nsresult
 NS_NewBoxObject(nsIBoxObject** aResult);
 
 %}
--- a/layout/xul/nsIListBoxObject.idl
+++ b/layout/xul/nsIListBoxObject.idl
@@ -2,18 +2,18 @@
 /* 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"
 
 // DEPRECATED: see ListBoxObject.webidl
 
-interface nsIDOMElement;
+webidl Element;
 
 [shim(ListBoxObject), uuid(AA9DEF4E-2E59-412d-A6DF-B76F52167795)]
 interface nsIListBoxObject : nsISupports
 {
   long getRowCount();
 
-  nsIDOMElement getItemAtIndex(in long index);
-  long getIndexOfItem(in nsIDOMElement item);
+  Element getItemAtIndex(in long index);
+  long getIndexOfItem(in Element item);
 };
--- a/layout/xul/nsListBoxBodyFrame.cpp
+++ b/layout/xul/nsListBoxBodyFrame.cpp
@@ -11,26 +11,26 @@
 #include "mozilla/MathAlgorithms.h"
 #include "nsCOMPtr.h"
 #include "nsGridRowGroupLayout.h"
 #include "nsIServiceManager.h"
 #include "nsGkAtoms.h"
 #include "nsIContent.h"
 #include "nsNameSpaceManager.h"
 #include "nsIDocument.h"
-#include "nsIDOMElement.h"
 #include "nsCSSFrameConstructor.h"
 #include "nsIScrollableFrame.h"
 #include "nsScrollbarFrame.h"
 #include "nsView.h"
 #include "nsViewManager.h"
 #include "mozilla/ComputedStyle.h"
 #include "nsFontMetrics.h"
 #include "nsITimer.h"
 #include "mozilla/ServoStyleSet.h"
+#include "mozilla/dom/Element.h"
 #include "mozilla/dom/Text.h"
 #include "nsPIBoxObject.h"
 #include "nsLayoutUtils.h"
 #include "nsPIListBoxObject.h"
 #include "nsContentUtils.h"
 #include "ChildIterator.h"
 #include "gfxContext.h"
 #include "prtime.h"
@@ -581,55 +581,55 @@ nsListBoxBodyFrame::ScrollByLines(int32_
 
   ScrollToIndex(scrollIndex);
 
   return NS_OK;
 }
 
 // walks the DOM to get the zero-based row index of the content
 nsresult
-nsListBoxBodyFrame::GetIndexOfItem(nsIDOMElement* aItem, int32_t* _retval)
+nsListBoxBodyFrame::GetIndexOfItem(Element* aItem, int32_t* _retval)
 {
   if (aItem) {
     *_retval = 0;
-    nsCOMPtr<nsIContent> itemContent(do_QueryInterface(aItem));
 
     FlattenedChildIterator iter(mContent);
     for (nsIContent* child = iter.GetNextChild(); child; child = iter.GetNextChild()) {
       // we hit a list row, count it
       if (child->IsXULElement(nsGkAtoms::listitem)) {
         // is this it?
-        if (child == itemContent)
+        if (child == aItem)
           return NS_OK;
 
         ++(*_retval);
       }
     }
   }
 
   // not found
   *_retval = -1;
   return NS_OK;
 }
 
 nsresult
-nsListBoxBodyFrame::GetItemAtIndex(int32_t aIndex, nsIDOMElement** aItem)
+nsListBoxBodyFrame::GetItemAtIndex(int32_t aIndex, Element** aItem)
 {
   *aItem = nullptr;
   if (aIndex < 0)
     return NS_OK;
 
   int32_t itemCount = 0;
   FlattenedChildIterator iter(mContent);
   for (nsIContent* child = iter.GetNextChild(); child; child = iter.GetNextChild()) {
     // we hit a list row, check if it is the one we are looking for
     if (child->IsXULElement(nsGkAtoms::listitem)) {
       // is this it?
       if (itemCount == aIndex) {
-        return CallQueryInterface(child, aItem);
+        *aItem = do_AddRef(child->AsElement()).take();
+        return NS_OK;
       }
       ++itemCount;
     }
   }
 
   // not found
   return NS_OK;
 }
@@ -702,25 +702,24 @@ nscoord
 nsListBoxBodyFrame::ComputeIntrinsicISize(nsBoxLayoutState& aBoxLayoutState)
 {
   if (mStringWidth != -1)
     return mStringWidth;
 
   nscoord largestWidth = 0;
 
   int32_t index = 0;
-  nsCOMPtr<nsIDOMElement> firstRowEl;
+  RefPtr<Element> firstRowEl;
   GetItemAtIndex(index, getter_AddRefs(firstRowEl));
-  nsCOMPtr<nsIContent> firstRowContent(do_QueryInterface(firstRowEl));
 
-  if (firstRowContent) {
+  if (firstRowEl) {
     nsPresContext* presContext = aBoxLayoutState.PresContext();
     RefPtr<ComputedStyle> computedStyle =
       presContext->StyleSet()->ResolveStyleFor(
-          firstRowContent->AsElement(), nullptr, LazyComputeBehavior::Allow);
+          firstRowEl, nullptr, LazyComputeBehavior::Allow);
 
     nscoord width = 0;
     nsMargin margin(0,0,0,0);
 
     if (computedStyle->StylePadding()->GetPadding(margin))
       width += margin.LeftRight();
     width += computedStyle->StyleBorder()->GetComputedBorder().LeftRight();
     if (computedStyle->StyleMargin()->GetMargin(margin))
--- a/layout/xul/nsListBoxBodyFrame.h
+++ b/layout/xul/nsListBoxBodyFrame.h
@@ -16,16 +16,22 @@
 #include "nsThreadUtils.h"
 #include "nsPIBoxObject.h"
 
 class nsPresContext;
 class nsListScrollSmoother;
 nsIFrame* NS_NewListBoxBodyFrame(nsIPresShell* aPresShell,
                                  mozilla::ComputedStyle* aStyle);
 
+namespace mozilla {
+namespace dom {
+class Element;
+} // namespace dom
+} // namespace mozilla
+
 class nsListBoxBodyFrame final : public nsBoxFrame,
                                  public nsIScrollbarMediator,
                                  public nsIReflowCallback
 {
   nsListBoxBodyFrame(ComputedStyle* aStyle,
                      nsBoxLayout* aLayoutManager);
   virtual ~nsListBoxBodyFrame();
 
@@ -34,18 +40,18 @@ public:
   NS_DECL_FRAMEARENA_HELPERS(nsListBoxBodyFrame)
 
   // non-virtual ListBoxObject
   int32_t GetNumberOfVisibleRows();
   int32_t GetIndexOfFirstVisibleRow();
   nsresult EnsureIndexIsVisible(int32_t aRowIndex);
   nsresult ScrollToIndex(int32_t aRowIndex);
   nsresult ScrollByLines(int32_t aNumLines);
-  nsresult GetItemAtIndex(int32_t aIndex, nsIDOMElement **aResult);
-  nsresult GetIndexOfItem(nsIDOMElement *aItem, int32_t *aResult);
+  nsresult GetItemAtIndex(int32_t aIndex, mozilla::dom::Element **aResult);
+  nsresult GetIndexOfItem(mozilla::dom::Element *aItem, int32_t *aResult);
 
   friend nsIFrame* NS_NewListBoxBodyFrame(nsIPresShell* aPresShell,
                                           ComputedStyle* aStyle);
 
   // nsIFrame
   virtual void Init(nsIContent*       aContent,
                     nsContainerFrame* aParent,
                     nsIFrame*         aPrevInFlow) override;
--- a/layout/xul/nsPIBoxObject.h
+++ b/layout/xul/nsPIBoxObject.h
@@ -10,24 +10,28 @@
 #include "nsIBoxObject.h"
 
 // {2b8bb262-1b0f-4572-ba87-5d4ae4954445}
 #define NS_PIBOXOBJECT_IID \
 { 0x2b8bb262, 0x1b0f, 0x4572, \
   { 0xba, 0x87, 0x5d, 0x4a, 0xe4, 0x95, 0x44, 0x45 } }
 
 
-class nsIContent;
+namespace mozilla {
+namespace dom {
+class Element;
+} // namespace dom
+} // namespace mozilla
 
 class nsPIBoxObject : public nsIBoxObject
 {
 public:
   NS_DECLARE_STATIC_IID_ACCESSOR(NS_PIBOXOBJECT_IID)
 
-  virtual nsresult Init(nsIContent* aContent) = 0;
+  virtual nsresult Init(mozilla::dom::Element* aElement) = 0;
 
   // Drop the weak ref to the content node as needed
   virtual void Clear() = 0;
 
   // The values cached by the implementation of this interface should be
   // cleared when this method is called.
   virtual void ClearCachedValues() = 0;
 };
--- a/layout/xul/tree/TreeBoxObject.cpp
+++ b/layout/xul/tree/TreeBoxObject.cpp
@@ -206,32 +206,31 @@ NS_IMETHODIMP TreeBoxObject::GetFocused(
 NS_IMETHODIMP TreeBoxObject::SetFocused(bool aFocused)
 {
   nsTreeBodyFrame* body = GetTreeBodyFrame();
   if (body)
     return body->SetFocused(aFocused);
   return NS_OK;
 }
 
-NS_IMETHODIMP TreeBoxObject::GetTreeBody(nsIDOMElement** aElement)
+NS_IMETHODIMP TreeBoxObject::GetTreeBody(Element** aElement)
 {
   *aElement = nullptr;
   nsTreeBodyFrame* body = GetTreeBodyFrame();
   if (body)
     return body->GetTreeBody(aElement);
   return NS_OK;
 }
 
 already_AddRefed<Element>
 TreeBoxObject::GetTreeBody()
 {
-  nsCOMPtr<nsIDOMElement> el;
+  RefPtr<Element> el;
   GetTreeBody(getter_AddRefs(el));
-  nsCOMPtr<Element> ret(do_QueryInterface(el));
-  return ret.forget();
+  return el.forget();
 }
 
 already_AddRefed<nsTreeColumns>
 TreeBoxObject::GetColumns()
 {
   nsTreeBodyFrame* body = GetTreeBodyFrame();
   if (body)
     return body->Columns();
--- a/layout/xul/tree/nsITreeBoxObject.idl
+++ b/layout/xul/tree/nsITreeBoxObject.idl
@@ -1,22 +1,23 @@
 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* 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 nsIDOMElement;
 interface nsITreeView;
 interface nsITreeSelection;
 interface nsITreeColumn;
 interface nsITreeColumns;
 interface nsIScriptableRegion;
 
+webidl Element;
+
 /**
  * This interface cannot become builtinclass until bug 1438525 is fixed.
  */
 [scriptable, uuid(f3da0c5e-51f5-45f0-b2cd-6be3ab6847ae)]
 interface nsITreeBoxObject : nsISupports
 {
   /**
    * Obtain the columns.
@@ -33,17 +34,17 @@ interface nsITreeBoxObject : nsISupports
   /**
    * Whether or not we are currently focused.
    */
   attribute boolean focused;
 
   /**
    * Obtain the treebody content node
    */
-  readonly attribute nsIDOMElement treeBody;
+  readonly attribute Element treeBody;
 
   /**
    * Obtain the height of a row.
    */
   readonly attribute long rowHeight;
 
   /**
    * Obtain the width of a row.
--- a/layout/xul/tree/nsITreeColumns.idl
+++ b/layout/xul/tree/nsITreeColumns.idl
@@ -1,21 +1,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 nsITreeColumns;
-interface nsIDOMElement;
+
+webidl Element;
 
 [scriptable, uuid(ae835ecf-6b32-4660-9b43-8a270df56e02)]
 interface nsITreeColumn : nsISupports
 {
-  readonly attribute nsIDOMElement element;
+  readonly attribute Element element;
 
   readonly attribute nsITreeColumns columns;
 
   readonly attribute long x;
   readonly attribute long width;
 
   readonly attribute AString id;
   [noscript] void getIdConst([shared] out wstring idConst);
@@ -70,17 +71,17 @@ interface nsITreeColumns : nsISupports
    */
   nsITreeColumn getPrimaryColumn();
   nsITreeColumn getSortedColumn();
   nsITreeColumn getKeyColumn();
 
   /**
    * Get the column for the given element.
    */
-  nsITreeColumn getColumnFor(in nsIDOMElement element);
+  nsITreeColumn getColumnFor(in Element element);
 
   /**
    * Parametric column getters.
    */
   nsITreeColumn getNamedColumn(in AString id);
   nsITreeColumn getColumnAt(in long index);
 
   /**
--- a/layout/xul/tree/nsITreeContentView.idl
+++ b/layout/xul/tree/nsITreeContentView.idl
@@ -1,22 +1,22 @@
 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* 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 nsIDOMElement;
+webidl Element;
 
 [scriptable, uuid(5ef62896-0c0a-41f1-bb3c-44a60f5dfdab)]
 interface nsITreeContentView : nsISupports
 {
   /**
    * Retrieve the content item associated with the specified index.
    */
-  nsIDOMElement getItemAtIndex(in long index);
+  Element getItemAtIndex(in long index);
 
   /**
    * Retrieve the index associated with the specified content item.
    */
-  long getIndexOfItem(in nsIDOMElement item);
+  long getIndexOfItem(in Element item);
 };
--- a/layout/xul/tree/nsTreeBodyFrame.cpp
+++ b/layout/xul/tree/nsTreeBodyFrame.cpp
@@ -548,23 +548,25 @@ nsTreeBodyFrame::SetFocused(bool aFocuse
       if (sel)
         sel->InvalidateSelection();
     }
   }
   return NS_OK;
 }
 
 nsresult
-nsTreeBodyFrame::GetTreeBody(nsIDOMElement** aElement)
+nsTreeBodyFrame::GetTreeBody(Element** aElement)
 {
   //NS_ASSERTION(mContent, "no content, see bug #104878");
   if (!mContent)
     return NS_ERROR_NULL_POINTER;
 
-  return mContent->QueryInterface(NS_GET_IID(nsIDOMElement), (void**)aElement);
+  RefPtr<Element> element = mContent->AsElement();
+  element.forget(aElement);
+  return NS_OK;
 }
 
 int32_t
 nsTreeBodyFrame::RowHeight() const
 {
   return nsPresContext::AppUnitsToIntCSSPixels(mRowHeight);
 }
 
--- a/layout/xul/tree/nsTreeBodyFrame.h
+++ b/layout/xul/tree/nsTreeBodyFrame.h
@@ -77,17 +77,17 @@ public:
   {
     nsCOMPtr<nsITreeView> view = mView;
     return view.forget();
   }
   nsresult GetView(nsITreeView **aView);
   nsresult SetView(nsITreeView *aView);
   bool GetFocused() const { return mFocused; }
   nsresult SetFocused(bool aFocused);
-  nsresult GetTreeBody(nsIDOMElement **aElement);
+  nsresult GetTreeBody(mozilla::dom::Element **aElement);
   int32_t RowHeight() const;
   int32_t RowWidth();
   int32_t GetHorizontalPosition() const;
   nsresult GetSelectionRegion(nsIScriptableRegion **aRegion);
   int32_t FirstVisibleRow() const { return mTopRowIndex; }
   int32_t LastVisibleRow() const { return mTopRowIndex + mPageLength; }
   int32_t PageLength() const { return mPageLength; }
   nsresult EnsureRowIsVisible(int32_t aRow);
--- a/layout/xul/tree/nsTreeColumns.cpp
+++ b/layout/xul/tree/nsTreeColumns.cpp
@@ -16,23 +16,23 @@
 #include "mozilla/dom/Element.h"
 #include "mozilla/dom/TreeBoxObject.h"
 #include "mozilla/dom/TreeColumnBinding.h"
 #include "mozilla/dom/TreeColumnsBinding.h"
 
 using namespace mozilla;
 
 // Column class that caches all the info about our column.
-nsTreeColumn::nsTreeColumn(nsTreeColumns* aColumns, nsIContent* aContent)
-  : mContent(aContent),
+nsTreeColumn::nsTreeColumn(nsTreeColumns* aColumns, dom::Element* aElement)
+  : mContent(aElement),
     mColumns(aColumns),
     mPrevious(nullptr)
 {
-  NS_ASSERTION(aContent &&
-               aContent->NodeInfo()->Equals(nsGkAtoms::treecol,
+  NS_ASSERTION(aElement &&
+               aElement->NodeInfo()->Equals(nsGkAtoms::treecol,
                                             kNameSpaceID_XUL),
                "nsTreeColumn's content must be a <xul:treecol>");
 
   Invalidate();
 }
 
 nsTreeColumn::~nsTreeColumn()
 {
@@ -146,20 +146,22 @@ nsTreeColumn::GetWidthInTwips(nsTreeBody
   *aResult = frame->GetRect().width;
   if (IsLastVisible(aBodyFrame))
     *aResult += aBodyFrame->mAdjustWidth;
   return NS_OK;
 }
 
 
 NS_IMETHODIMP
-nsTreeColumn::GetElement(nsIDOMElement** aElement)
+nsTreeColumn::GetElement(Element** aElement)
 {
   if (mContent) {
-    return CallQueryInterface(mContent, aElement);
+    RefPtr<dom::Element> element = mContent;
+    element.forget(aElement);
+    return NS_OK;
   }
   *aElement = nullptr;
   return NS_ERROR_FAILURE;
 }
 
 NS_IMETHODIMP
 nsTreeColumn::GetColumns(nsITreeColumns** aColumns)
 {
@@ -358,23 +360,22 @@ nsTreeColumn::GetParentObject() const
 nsTreeColumn::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
 {
   return dom::TreeColumnBinding::Wrap(aCx, this, aGivenProto);
 }
 
 mozilla::dom::Element*
 nsTreeColumn::GetElement(mozilla::ErrorResult& aRv)
 {
-  nsCOMPtr<nsIDOMElement> element;
+  RefPtr<Element> element;
   aRv = GetElement(getter_AddRefs(element));
   if (aRv.Failed()) {
     return nullptr;
   }
-  nsCOMPtr<nsINode> node = do_QueryInterface(element);
-  return node->AsElement();
+  return element;
 }
 
 int32_t
 nsTreeColumn::GetX(mozilla::ErrorResult& aRv)
 {
   int32_t x;
   aRv = GetX(&x);
   return x;
@@ -583,20 +584,19 @@ nsTreeColumns::GetColumnFor(dom::Element
     if (currCol->mContent == aElement) {
       return currCol;
     }
   }
   return nullptr;
 }
 
 NS_IMETHODIMP
-nsTreeColumns::GetColumnFor(nsIDOMElement* aElement, nsITreeColumn** _retval)
+nsTreeColumns::GetColumnFor(dom::Element* aElement, nsITreeColumn** _retval)
 {
-  nsCOMPtr<dom::Element> element = do_QueryInterface(aElement);
-  NS_ADDREF(*_retval = GetColumnFor(element));
+  NS_IF_ADDREF(*_retval = GetColumnFor(aElement));
   return NS_OK;
 }
 
 nsTreeColumn*
 nsTreeColumns::NamedGetter(const nsAString& aId, bool& aFound)
 {
   EnsureColumns();
   for (nsTreeColumn* currCol = mFirstColumn; currCol; currCol = currCol->GetNext()) {
@@ -747,17 +747,17 @@ nsTreeColumns::EnsureColumns()
     // we can enumerate the columns in visible order
     nsTreeColumn* currCol = nullptr;
     while (colFrame) {
       nsIContent* colContent = colFrame->GetContent();
 
       if (colContent->NodeInfo()->Equals(nsGkAtoms::treecol,
                                          kNameSpaceID_XUL)) {
         // Create a new column structure.
-        nsTreeColumn* col = new nsTreeColumn(this, colContent);
+        nsTreeColumn* col = new nsTreeColumn(this, colContent->AsElement());
         if (!col)
           return;
 
         if (currCol) {
           currCol->SetNext(col);
           col->SetPrevious(currCol);
         }
         else {
--- a/layout/xul/tree/nsTreeColumns.h
+++ b/layout/xul/tree/nsTreeColumns.h
@@ -5,16 +5,17 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #ifndef nsTreeColumns_h__
 #define nsTreeColumns_h__
 
 #include "nsITreeColumns.h"
 #include "nsITreeBoxObject.h"
 #include "mozilla/Attributes.h"
+#include "mozilla/RefPtr.h"
 #include "nsCoord.h"
 #include "nsCycleCollectionParticipant.h"
 #include "nsQueryObject.h"
 #include "nsWrapperCache.h"
 #include "nsString.h"
 
 class nsTreeBodyFrame;
 class nsTreeColumns;
@@ -39,17 +40,17 @@ class TreeBoxObject;
 }
 
 // This class is our column info.  We use it to iterate our columns and to obtain
 // information about each column.
 class nsTreeColumn final : public nsITreeColumn
                          , public nsWrapperCache
 {
 public:
-  nsTreeColumn(nsTreeColumns* aColumns, nsIContent* aContent);
+  nsTreeColumn(nsTreeColumns* aColumns, mozilla::dom::Element* aElement);
 
   NS_DECLARE_STATIC_IID_ACCESSOR(NS_TREECOLUMN_IMPL_CID)
 
   static already_AddRefed<nsTreeColumn> From(nsITreeColumn* aColumn)
   {
     RefPtr<nsTreeColumn> col = do_QueryObject(aColumn);
     return col.forget();
   }
@@ -128,17 +129,17 @@ protected:
     mNext = aNext;
   }
   void SetPrevious(nsTreeColumn* aPrevious) { mPrevious = aPrevious; }
 
 private:
   /**
    * Non-null nsIContent for the associated <treecol> element.
    */
-  nsCOMPtr<nsIContent> mContent;
+  RefPtr<mozilla::dom::Element> mContent;
 
   nsTreeColumns* mColumns;
 
   nsString mId;
   RefPtr<nsAtom> mAtom;
 
   int32_t mIndex;
 
--- a/layout/xul/tree/nsTreeContentView.cpp
+++ b/layout/xul/tree/nsTreeContentView.cpp
@@ -596,33 +596,36 @@ nsTreeContentView::SetTree(nsITreeBoxObj
 
   if (aTree) {
     // Get our root element
     nsCOMPtr<nsIBoxObject> boxObject = do_QueryInterface(mBoxObject);
     if (!boxObject) {
       mBoxObject = nullptr;
       return NS_ERROR_INVALID_ARG;
     }
-    nsCOMPtr<nsIDOMElement> element;
-    boxObject->GetElement(getter_AddRefs(element));
 
-    mRoot = do_QueryInterface(element);
-    NS_ENSURE_STATE(mRoot);
+    { // Scope for element
+      RefPtr<dom::Element> element;
+      boxObject->GetElement(getter_AddRefs(element));
+
+      mRoot = element.forget();
+      NS_ENSURE_STATE(mRoot);
+    }
 
     // Add ourselves to document's observers.
     nsIDocument* document = mRoot->GetComposedDoc();
     if (document) {
       document->AddObserver(this);
       mDocument = document;
     }
 
-    nsCOMPtr<nsIDOMElement> bodyElement;
+    RefPtr<dom::Element> bodyElement;
     mBoxObject->GetTreeBody(getter_AddRefs(bodyElement));
     if (bodyElement) {
-      mBody = do_QueryInterface(bodyElement);
+      mBody = bodyElement.forget();
       int32_t index = 0;
       Serialize(mBody, -1, &index, mRows);
     }
   }
 
   return NS_OK;
 }
 
@@ -653,20 +656,19 @@ nsTreeContentView::ToggleOpenState(int32
 }
 
 void
 nsTreeContentView::CycleHeader(nsTreeColumn& aColumn, ErrorResult& aError)
 {
   if (!mRoot)
     return;
 
-  nsCOMPtr<nsIDOMElement> element;
-  aColumn.GetElement(getter_AddRefs(element));
-  if (element) {
-    nsCOMPtr<Element> column = do_QueryInterface(element);
+  RefPtr<Element> column;
+  aColumn.GetElement(getter_AddRefs(column));
+  if (column) {
     nsAutoString sort;
     column->GetAttr(kNameSpaceID_None, nsGkAtoms::sort, sort);
     if (!sort.IsEmpty()) {
       nsCOMPtr<nsIXULSortService> xs = do_GetService("@mozilla.org/xul/xul-sort-service;1");
       if (xs) {
         nsAutoString sortdirection;
         static Element::AttrValuesArray strings[] =
           {&nsGkAtoms::ascending, &nsGkAtoms::descending, nullptr};
@@ -870,45 +872,38 @@ nsTreeContentView::GetItemAtIndex(int32_
     aError.Throw(NS_ERROR_INVALID_ARG);
     return nullptr;
   }
 
   return mRows[aIndex]->mContent;
 }
 
 NS_IMETHODIMP
-nsTreeContentView::GetItemAtIndex(int32_t aIndex, nsIDOMElement** _retval)
+nsTreeContentView::GetItemAtIndex(int32_t aIndex, Element** _retval)
 {
   ErrorResult rv;
-  Element* element = GetItemAtIndex(aIndex, rv);
+  RefPtr<Element> element = GetItemAtIndex(aIndex, rv);
   if (rv.Failed()) {
     return rv.StealNSResult();
   }
 
-  if (!element) {
-    *_retval = nullptr;
-    return NS_OK;
-  }
-
-  return CallQueryInterface(element, _retval);
+  element.forget(_retval);
+  return NS_OK;
 }
 
 int32_t
 nsTreeContentView::GetIndexOfItem(Element* aItem)
 {
   return FindContent(aItem);
 }
 
 NS_IMETHODIMP
-nsTreeContentView::GetIndexOfItem(nsIDOMElement* aItem, int32_t* _retval)
+nsTreeContentView::GetIndexOfItem(Element* aItem, int32_t* _retval)
 {
-  nsCOMPtr<Element> element = do_QueryInterface(aItem);
-
-  *_retval = GetIndexOfItem(element);
-
+  *_retval = GetIndexOfItem(aItem);
   return NS_OK;
 }
 
 void
 nsTreeContentView::AttributeChanged(dom::Element* aElement,
                                     int32_t       aNameSpaceID,
                                     nsAtom*      aAttribute,
                                     int32_t       aModType,
@@ -976,19 +971,18 @@ nsTreeContentView::AttributeChanged(dom:
   }
 
   if (aElement->IsXULElement(nsGkAtoms::treecol)) {
     if (aAttribute == nsGkAtoms::properties) {
       if (mBoxObject) {
         nsCOMPtr<nsITreeColumns> cols;
         mBoxObject->GetColumns(getter_AddRefs(cols));
         if (cols) {
-          nsCOMPtr<nsIDOMElement> element = do_QueryInterface(aElement);
           nsCOMPtr<nsITreeColumn> col;
-          cols->GetColumnFor(element, getter_AddRefs(col));
+          cols->GetColumnFor(aElement, getter_AddRefs(col));
           mBoxObject->InvalidateColumn(col);
         }
       }
     }
   }
   else if (aElement->IsXULElement(nsGkAtoms::treeitem)) {
     int32_t index = FindContent(aElement);
     if (index >= 0) {
--- a/layout/xul/tree/nsTreeSelection.cpp
+++ b/layout/xul/tree/nsTreeSelection.cpp
@@ -1,15 +1,16 @@
 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
 /* 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 "mozilla/AsyncEventDispatcher.h"
+#include "mozilla/dom/Element.h"
 #include "nsCOMPtr.h"
 #include "nsTreeSelection.h"
 #include "nsIBoxObject.h"
 #include "nsITreeBoxObject.h"
 #include "nsITreeView.h"
 #include "nsString.h"
 #include "nsIDOMElement.h"
 #include "nsIContent.h"
@@ -634,27 +635,26 @@ NS_IMETHODIMP nsTreeSelection::SetCurren
   if (aIndex != -1)
     mTree->InvalidateRow(aIndex);
 
   // Fire DOMMenuItemActive or DOMMenuItemInactive event for tree.
   nsCOMPtr<nsIBoxObject> boxObject = do_QueryInterface(mTree);
   NS_ASSERTION(boxObject, "no box object!");
   if (!boxObject)
     return NS_ERROR_UNEXPECTED;
-  nsCOMPtr<nsIDOMElement> treeElt;
+  RefPtr<dom::Element> treeElt;
   boxObject->GetElement(getter_AddRefs(treeElt));
 
-  nsCOMPtr<nsINode> treeDOMNode(do_QueryInterface(treeElt));
-  NS_ENSURE_STATE(treeDOMNode);
+  NS_ENSURE_STATE(treeElt);
 
   NS_NAMED_LITERAL_STRING(DOMMenuItemActive, "DOMMenuItemActive");
   NS_NAMED_LITERAL_STRING(DOMMenuItemInactive, "DOMMenuItemInactive");
 
   RefPtr<AsyncEventDispatcher> asyncDispatcher =
-    new AsyncEventDispatcher(treeDOMNode,
+    new AsyncEventDispatcher(treeElt,
                              (aIndex != -1 ? DOMMenuItemActive :
                                              DOMMenuItemInactive),
                              true, false);
   return asyncDispatcher->PostDOMEvent();
 }
 
 NS_IMETHODIMP nsTreeSelection::GetCurrentColumn(nsITreeColumn** aCurrentColumn)
 {
@@ -825,25 +825,22 @@ nsTreeSelection::FireOnSelectHandler()
 {
   if (mSuppressed || !mTree)
     return NS_OK;
 
   nsCOMPtr<nsIBoxObject> boxObject = do_QueryInterface(mTree);
   NS_ASSERTION(boxObject, "no box object!");
   if (!boxObject)
      return NS_ERROR_UNEXPECTED;
-  nsCOMPtr<nsIDOMElement> elt;
+  RefPtr<dom::Element> elt;
   boxObject->GetElement(getter_AddRefs(elt));
   NS_ENSURE_STATE(elt);
 
-  nsCOMPtr<nsINode> node(do_QueryInterface(elt));
-  NS_ENSURE_STATE(node);
-
   RefPtr<AsyncEventDispatcher> asyncDispatcher =
-    new AsyncEventDispatcher(node, NS_LITERAL_STRING("select"), true, false);
+    new AsyncEventDispatcher(elt, NS_LITERAL_STRING("select"), true, false);
   asyncDispatcher->RunDOMEventWhenSafe();
   return NS_OK;
 }
 
 void
 nsTreeSelection::SelectCallback(nsITimer *aTimer, void *aClosure)
 {
   RefPtr<nsTreeSelection> self = static_cast<nsTreeSelection*>(aClosure);
@@ -858,21 +855,19 @@ already_AddRefed<nsIContent>
 nsTreeSelection::GetContent()
 {
   if (!mTree) {
     return nullptr;
   }
 
   nsCOMPtr<nsIBoxObject> boxObject = do_QueryInterface(mTree);
 
-  nsCOMPtr<nsIDOMElement> element;
+  RefPtr<dom::Element> element;
   boxObject->GetElement(getter_AddRefs(element));
-
-  nsCOMPtr<nsIContent> content = do_QueryInterface(element);
-  return content.forget();
+  return element.forget();
 }
 
 ///////////////////////////////////////////////////////////////////////////////////
 
 nsresult
 NS_NewTreeSelection(nsITreeBoxObject* aTree, nsITreeSelection** aResult)
 {
   *aResult = new nsTreeSelection(aTree);