Bug 1465478: Introduce Element::FromNode. r=smaug

And use it in a couple places I noticed.

MozReview-Commit-ID: 8baSMrbdEbF
This commit is contained in:
Emilio Cobos Álvarez 2018-05-30 16:56:24 +02:00
parent b254a63736
commit fe09ffd3af
20 changed files with 77 additions and 105 deletions

View File

@ -290,7 +290,7 @@ class AttrIterator
{
public:
explicit AttrIterator(nsIContent* aContent)
: mElement(aContent->IsElement() ? aContent->AsElement() : nullptr)
: mElement(Element::FromNode(aContent))
, mAttrIdx(0)
{
mAttrCount = mElement ? mElement->GetAttrCount() : 0;

View File

@ -168,8 +168,8 @@ public:
virtual nsINode* GetNode() const;
nsIContent* GetContent() const { return mContent; }
mozilla::dom::Element* Elm() const
{ return mContent && mContent->IsElement() ? mContent->AsElement() : nullptr; }
dom::Element* Elm() const
{ return dom::Element::FromNodeOrNull(mContent); }
/**
* Return node type information of DOM node associated with the accessible.

View File

@ -334,11 +334,10 @@ HyperTextAccessible::TransformOffset(Accessible* aDescendant,
*/
static nsIContent* GetElementAsContentOf(nsINode* aNode)
{
if (aNode->IsElement()) {
return aNode->AsContent();
if (Element* element = Element::FromNode(aNode)) {
return element;
}
nsIContent* parent = aNode->GetParent();
return parent && parent->IsElement() ? parent : nullptr;
return aNode->GetParentElement();
}
bool

View File

@ -225,9 +225,7 @@ protected:
virtual Element* GetNameSpaceElement() override
{
nsINode *parent = GetParentNode();
return parent && parent->IsElement() ? parent->AsElement() : nullptr;
return Element::FromNodeOrNull(GetParentNode());
}
nsresult SetTextInternal(uint32_t aOffset, uint32_t aCount,

View File

@ -167,6 +167,8 @@ public:
NS_DECL_ADDSIZEOFEXCLUDINGTHIS
NS_IMPL_FROMNODE_HELPER(Element, IsElement())
NS_IMETHOD QueryInterface(REFNSIID aIID, void** aInstancePtr) override;
/**
@ -2048,7 +2050,7 @@ inline const mozilla::dom::Element* nsINode::AsElement() const
inline mozilla::dom::Element* nsINode::GetParentElement() const
{
return mParent && mParent->IsElement() ? mParent->AsElement() : nullptr;
return mozilla::dom::Element::FromNodeOrNull(mParent);
}
/**

View File

@ -3803,14 +3803,15 @@ AssertNoStaleServoDataIn(const nsINode& aSubtreeRoot)
for (const nsINode* node = &aSubtreeRoot;
node;
node = node->GetNextNode(&aSubtreeRoot)) {
if (!node->IsElement()) {
Element* element = Element::FromNode(node);
if (!element) {
continue;
}
MOZ_ASSERT(!node->AsElement()->HasServoData());
if (auto* shadow = node->AsElement()->GetShadowRoot()) {
MOZ_ASSERT(!element->HasServoData());
if (auto* shadow = element->GetShadowRoot()) {
AssertNoStaleServoDataIn(*shadow);
}
if (nsXBLBinding* binding = node->AsElement()->GetXBLBinding()) {
if (nsXBLBinding* binding = element->GetXBLBinding()) {
if (nsXBLBinding* bindingWithContent = binding->GetBindingWithContent()) {
nsIContent* content = bindingWithContent->GetAnonymousContent();
MOZ_ASSERT(!content->AsElement()->HasServoData());
@ -4144,10 +4145,9 @@ nsIDocument::GetRootElementInternal() const
// are likely to appear before the root element.
uint32_t i;
for (i = mChildren.ChildCount(); i > 0; --i) {
nsIContent* child = mChildren.ChildAt(i - 1);
if (child->IsElement()) {
const_cast<nsIDocument*>(this)->mCachedRootElement = child->AsElement();
return child->AsElement();
if (Element* element = Element::FromNode(mChildren.ChildAt(i - 1))) {
const_cast<nsIDocument*>(this)->mCachedRootElement = element;
return element;
}
}
@ -5553,7 +5553,7 @@ nsIDocument::GetAnonRootIfInAnonymousContentContainer(nsINode* aNode) const
nsINode* parent = aNode->GetParentNode();
while (parent && parent->IsInNativeAnonymousSubtree()) {
if (parent == customContainer) {
return child->IsElement() ? child->AsElement() : nullptr;
return Element::FromNode(child);
}
child = parent;
parent = child->GetParentNode();
@ -6124,14 +6124,13 @@ nsIDocument::GetAnonymousElementByAttribute(nsIContent* aElement,
bool universalMatch = aAttrValue.EqualsLiteral("*");
for (uint32_t i = 0; i < length; ++i) {
nsIContent* current = nodeList->Item(i);
if (!current->IsElement()) {
Element* current = Element::FromNode(nodeList->Item(i));
if (!current) {
continue;
}
Element* matchedElm =
GetElementByAttribute(current->AsElement(), aAttrName, aAttrValue,
universalMatch);
GetElementByAttribute(current, aAttrName, aAttrValue, universalMatch);
if (matchedElm)
return matchedElm;
}
@ -6933,12 +6932,10 @@ nsIDocument::GetCompatMode(nsString& aCompatMode) const
}
void
nsDOMAttributeMap::BlastSubtreeToPieces(nsINode *aNode)
nsDOMAttributeMap::BlastSubtreeToPieces(nsINode* aNode)
{
if (aNode->IsElement()) {
Element *element = aNode->AsElement();
const nsDOMAttributeMap *map = element->GetAttributeMap();
if (map) {
if (Element* element = Element::FromNode(aNode)) {
if (const nsDOMAttributeMap* map = element->GetAttributeMap()) {
while (true) {
nsCOMPtr<nsIAttribute> attr;
{
@ -7062,8 +7059,8 @@ nsIDocument::AdoptNode(nsINode& aAdoptedNode, ErrorResult& rv)
// have a binding applied. Remove the binding from the element now
// that it's getting adopted into a new document.
// TODO Fully tear down the binding.
if (adoptedNode->IsElement()) {
adoptedNode->AsElement()->SetXBLBinding(nullptr);
if (Element* element = Element::FromNode(adoptedNode)) {
element->SetXBLBinding(nullptr);
}
}

View File

@ -373,11 +373,8 @@ nsDocumentEncoder::SerializeNodeStart(nsINode* aNode,
nsLayoutUtils::IsInvisibleBreak(node)) {
return NS_OK;
}
Element* originalElement =
aOriginalNode && aOriginalNode->IsElement() ?
aOriginalNode->AsElement() : nullptr;
mSerializer->AppendElementStart(node->AsElement(),
originalElement, aStr);
Element* originalElement = Element::FromNodeOrNull(aOriginalNode);
mSerializer->AppendElementStart(node->AsElement(), originalElement, aStr);
return NS_OK;
}

View File

@ -354,13 +354,9 @@ nsNodeUtils::LastRelease(nsINode* aNode)
aNode->UnsetFlags(NODE_HAS_LISTENERMANAGER);
}
if (aNode->IsElement()) {
nsIDocument* ownerDoc = aNode->OwnerDoc();
Element* elem = aNode->AsElement();
ownerDoc->ClearBoxObjectFor(elem);
NS_ASSERTION(!elem->GetXBLBinding(),
"Node has binding on destruction");
if (Element* element = Element::FromNode(aNode)) {
element->OwnerDoc()->ClearBoxObjectFor(element);
NS_ASSERTION(!element->GetXBLBinding(), "Node has binding on destruction");
}
aNode->ReleaseWrapper(aNode);
@ -429,7 +425,7 @@ nsNodeUtils::CloneAndAdopt(nsINode *aNode, bool aClone, bool aDeep,
nodeInfo = newNodeInfo;
}
Element *elem = aNode->IsElement() ? aNode->AsElement() : nullptr;
Element* elem = Element::FromNode(aNode);
nsCOMPtr<nsINode> clone;
if (aClone) {
@ -496,10 +492,9 @@ nsNodeUtils::CloneAndAdopt(nsINode *aNode, bool aClone, bool aDeep,
else if (nodeInfoManager) {
nsIDocument* oldDoc = aNode->OwnerDoc();
bool wasRegistered = false;
if (aNode->IsElement()) {
Element* element = aNode->AsElement();
oldDoc->ClearBoxObjectFor(element);
wasRegistered = oldDoc->UnregisterActivityObserver(element);
if (elem) {
oldDoc->ClearBoxObjectFor(elem);
wasRegistered = oldDoc->UnregisterActivityObserver(elem);
}
aNode->mNodeInfo.swap(newNodeInfo);
@ -509,20 +504,17 @@ nsNodeUtils::CloneAndAdopt(nsINode *aNode, bool aClone, bool aDeep,
nsIDocument* newDoc = aNode->OwnerDoc();
if (newDoc) {
if (CustomElementRegistry::IsCustomElementEnabled(newDoc)) {
if (elem && CustomElementRegistry::IsCustomElementEnabled(newDoc)) {
// Adopted callback must be enqueued whenever a nodes
// shadow-including inclusive descendants that is custom.
Element* element = aNode->IsElement() ? aNode->AsElement() : nullptr;
if (element) {
CustomElementData* data = element->GetCustomElementData();
if (data && data->mState == CustomElementData::State::eCustom) {
LifecycleAdoptedCallbackArgs args = {
oldDoc,
newDoc
};
nsContentUtils::EnqueueLifecycleCallback(nsIDocument::eAdopted,
element, nullptr, &args);
}
CustomElementData* data = elem->GetCustomElementData();
if (data && data->mState == CustomElementData::State::eCustom) {
LifecycleAdoptedCallbackArgs args = {
oldDoc,
newDoc
};
nsContentUtils::EnqueueLifecycleCallback(nsIDocument::eAdopted,
elem, nullptr, &args);
}
}

View File

@ -257,7 +257,7 @@ HTMLLabelElement::GetFirstLabelableDescendant() const
{
for (nsIContent* cur = nsINode::GetFirstChild(); cur;
cur = cur->GetNextNode(this)) {
Element* element = cur->IsElement() ? cur->AsElement() : nullptr;
Element* element = Element::FromNode(cur);
if (element && element->IsLabelable()) {
return static_cast<nsGenericHTMLElement*>(element);
}

View File

@ -6,6 +6,7 @@
#include "mozilla/dom/SVGAnimationElement.h"
#include "mozilla/dom/SVGSVGElement.h"
#include "mozilla/dom/ElementInlines.h"
#include "nsSMILTimeContainer.h"
#include "nsSMILAnimationController.h"
#include "nsSMILAnimationFunction.h"
@ -71,8 +72,7 @@ SVGAnimationElement::GetTargetElementContent()
"if we don't have an xlink:href or href attribute");
// No "href" or "xlink:href" attribute --> I should target my parent.
nsIContent* parent = GetFlattenedTreeParent();
return parent && parent->IsElement() ? parent->AsElement() : nullptr;
return GetFlattenedTreeParentElement();
}
bool

View File

@ -404,17 +404,18 @@ EnsureSubtreeStyled(Element* aElement)
for (nsIContent* child = iter.GetNextChild();
child;
child = iter.GetNextChild()) {
if (!child->IsElement()) {
Element* element = Element::FromNode(child);
if (!element) {
continue;
}
if (child->AsElement()->HasServoData()) {
if (element->HasServoData()) {
// If any child was styled, all of them should be styled already, so we
// can bail out.
return;
}
servoSet->StyleNewSubtree(child->AsElement());
servoSet->StyleNewSubtree(element);
}
}
@ -615,8 +616,7 @@ nsXBLService::AttachGlobalKeyHandler(EventTarget* aTarget)
if (contentNode && contentNode->GetProperty(nsGkAtoms::listener))
return NS_OK;
Element* elt =
contentNode && contentNode->IsElement() ? contentNode->AsElement() : nullptr;
Element* elt = Element::FromNodeOrNull(contentNode);
// Create the key handler
RefPtr<nsXBLWindowKeyHandler> handler =

View File

@ -1419,12 +1419,11 @@ XULDocument::AddSubtreeToDocument(nsIContent* aContent)
{
NS_ASSERTION(aContent->GetUncomposedDoc() == this, "Element not in doc!");
// From here on we only care about elements.
if (!aContent->IsElement()) {
Element* aElement = Element::FromNode(aContent);
if (!aElement) {
return NS_OK;
}
Element* aElement = aContent->AsElement();
// Do pre-order addition magic
nsresult rv = AddElementToDocumentPre(aElement);
if (NS_FAILED(rv)) return rv;
@ -1447,12 +1446,11 @@ nsresult
XULDocument::RemoveSubtreeFromDocument(nsIContent* aContent)
{
// From here on we only care about elements.
if (!aContent->IsElement()) {
Element* aElement = Element::FromNode(aContent);
if (!aElement) {
return NS_OK;
}
Element* aElement = aContent->AsElement();
// Do a bunch of cleanup to remove an element from the XUL
// document.
nsresult rv;
@ -3709,7 +3707,7 @@ XULDocument::FindBroadcaster(Element* aElement,
return NS_FINDBROADCASTER_AWAIT_OVERLAYS;
}
*aListener = parent->IsElement() ? parent->AsElement() : nullptr;
*aListener = Element::FromNode(parent);
NS_IF_ADDREF(*aListener);
aElement->GetAttr(kNameSpaceID_None, nsGkAtoms::element, aBroadcasterID);
@ -3841,14 +3839,14 @@ XULDocument::InsertElement(nsINode* aParent, nsIContent* aChild, bool aNotify)
bool wasInserted = false;
// insert after an element of a given id
if (aChild->IsElement()) {
aChild->AsElement()->GetAttr(kNameSpaceID_None, nsGkAtoms::insertafter, posStr);
if (Element* element = Element::FromNode(aChild)) {
element->GetAttr(kNameSpaceID_None, nsGkAtoms::insertafter, posStr);
}
bool isInsertAfter = true;
if (posStr.IsEmpty()) {
if (aChild->IsElement()) {
aChild->AsElement()->GetAttr(kNameSpaceID_None, nsGkAtoms::insertbefore, posStr);
if (Element* element = Element::FromNode(aChild)) {
element->GetAttr(kNameSpaceID_None, nsGkAtoms::insertbefore, posStr);
}
isInsertAfter = false;
}

View File

@ -226,16 +226,16 @@ XULPopupElement::GetTriggerNode() const
return nsMenuPopupFrame::GetTriggerContent(menuPopupFrame);
}
// FIXME(emilio): should probably be renamed to GetAnchorElement?
Element*
XULPopupElement::GetAnchorNode() const
{
nsMenuPopupFrame *menuPopupFrame = do_QueryFrame(GetPrimaryFrame());
nsMenuPopupFrame* menuPopupFrame = do_QueryFrame(GetPrimaryFrame());
if (!menuPopupFrame) {
return nullptr;
}
nsIContent* anchor = menuPopupFrame->GetAnchor();
return anchor && anchor->IsElement() ? anchor->AsElement() : nullptr;
return Element::FromNodeOrNull(menuPopupFrame->GetAnchor());
}
already_AddRefed<DOMRect>

View File

@ -1266,8 +1266,7 @@ EditorBase::MarkNodeDirty(nsINode* aNode)
if (!OutputsMozDirty()) {
return NS_OK;
}
if (aNode && aNode->IsElement()) {
RefPtr<Element> element = aNode->AsElement();
if (RefPtr<Element> element = Element::FromNodeOrNull(aNode)) {
element->SetAttr(kNameSpaceID_None, nsGkAtoms::mozdirty, EmptyString(),
false);
}
@ -3920,8 +3919,7 @@ EditorBase::IsPreformatted(nsINode* aNode)
}
// Look at the node (and its parent if it's not an element), and grab its
// ComputedStyle.
RefPtr<ComputedStyle> elementStyle;
Element* element = aNode->IsElement() ? aNode->AsElement() : nullptr;
Element* element = Element::FromNode(aNode);
if (!element) {
element = aNode->GetParentElement();
if (!element) {
@ -3929,7 +3927,7 @@ EditorBase::IsPreformatted(nsINode* aNode)
}
}
elementStyle =
RefPtr<ComputedStyle> elementStyle =
nsComputedDOMStyle::GetComputedStyleNoFlush(element, nullptr);
if (!elementStyle) {
// Consider nodes without a ComputedStyle to be NOT preformatted:

View File

@ -170,7 +170,7 @@ public:
dom::Element*
GetContainerAsElement() const
{
return mParent && mParent->IsElement() ? mParent->AsElement() : nullptr;
return Element::FromNodeOrNull(mParent);
}
dom::Text*

View File

@ -377,8 +377,7 @@ public:
nsIContent* GetLeftContent() const { return mLeftContent; }
dom::Element* GetLeftContentAsElement() const
{
return mLeftContent && mLeftContent->IsElement() ?
mLeftContent->AsElement() : nullptr;
return Element::FromNodeOrNull(mLeftContent);
}
/**
@ -389,8 +388,7 @@ public:
nsIContent* GetMiddleContent() const { return mMiddleContent; }
dom::Element* GetMiddleContentAsElement() const
{
return mMiddleContent && mMiddleContent->IsElement() ?
mMiddleContent->AsElement() : nullptr;
return Element::FromNodeOrNull(mMiddleContent);
}
/**
@ -401,8 +399,7 @@ public:
nsIContent* GetRightContent() const { return mRightContent; }
dom::Element* GetRightContentAsElement() const
{
return mRightContent && mRightContent->IsElement() ?
mRightContent->AsElement() : nullptr;
return Element::FromNodeOrNull(mRightContent);
}
SplitRangeOffFromNodeResult(nsIContent* aLeftContent, nsIContent* aMiddleContent,

View File

@ -2593,9 +2593,7 @@ HTMLEditor::GetSelectedElement(const nsAString& aTagName,
// Query interface to cast nsIContent to Element
// then get tagType to compare to aTagName
// Clone node of each desired type and append it to the aDomFrag
nsINode* currentNode = iter->GetCurrentNode();
selectedElement = currentNode && currentNode->IsElement() ?
currentNode->AsElement() : nullptr;
selectedElement = Element::FromNodeOrNull(iter->GetCurrentNode());
if (selectedElement) {
// If we already found a node, then we have another element,
// thus there's not just one element selected
@ -2604,7 +2602,7 @@ HTMLEditor::GetSelectedElement(const nsAString& aTagName,
break;
}
domTagName = currentNode->NodeName();
domTagName = selectedElement->NodeName();
ToLowerCase(domTagName);
if (anyTag) {

View File

@ -861,8 +861,7 @@ AccessibleCaretManager::ChangeFocusToOrClearOldFocus(nsIFrame* aFrame) const
if (aFrame) {
nsIContent* focusableContent = aFrame->GetContent();
MOZ_ASSERT(focusableContent, "Focusable frame must have content!");
RefPtr<Element> focusableElement =
focusableContent->IsElement() ? focusableContent->AsElement() : nullptr;
RefPtr<Element> focusableElement = Element::FromNode(focusableContent);
fm->SetFocus(focusableElement, nsIFocusManager::FLAG_BYMOUSE);
} else {
nsPIDOMWindowOuter* win = mPresShell->GetDocument()->GetWindow();

View File

@ -9831,9 +9831,7 @@ GetCorrectedParent(const nsIFrame* aFrame)
// inherit from the NAC generator element instead.
if (pseudo) {
MOZ_ASSERT(aFrame->GetContent());
Element* element =
aFrame->GetContent()->IsElement()
? aFrame->GetContent()->AsElement() : nullptr;
Element* element = Element::FromNode(aFrame->GetContent());
// Make sure to avoid doing the fixup for non-element-backed pseudos like
// ::first-line and such.
if (element &&

View File

@ -398,8 +398,7 @@ nsWebBrowserFind::SetSelectionAndScroll(nsPIDOMWindowOuter* aWindow,
nsCOMPtr<nsIFocusManager> fm = do_GetService(FOCUSMANAGER_CONTRACTID);
if (fm) {
if (tcFrame) {
RefPtr<Element> newFocusedElement =
content->IsElement() ? content->AsElement() : nullptr;
RefPtr<Element> newFocusedElement = Element::FromNode(content);
fm->SetFocus(newFocusedElement, nsIFocusManager::FLAG_NOSCROLL);
} else {
RefPtr<Element> result;