From a02a40bdb5f007722a4721f5427abe6187150e5d Mon Sep 17 00:00:00 2001 From: Boris Zbarsky Date: Fri, 18 May 2018 23:37:56 -0400 Subject: [PATCH] Bug 1462737. Make AsElement() not compile when called on Element or some subclass. r=mccr8 It's just noise if we already have an Element. --- dom/base/CustomElementRegistry.cpp | 2 +- dom/base/DirectionalityUtils.cpp | 2 -- dom/base/Element.h | 4 +++ dom/base/nsDocument.cpp | 2 +- dom/html/HTMLSelectElement.cpp | 2 +- editor/libeditor/HTMLEditRules.cpp | 4 +-- layout/forms/nsNumberControlFrame.cpp | 2 +- layout/xul/tree/nsTreeBodyFrame.cpp | 18 +++++----- layout/xul/tree/nsTreeColumns.cpp | 51 +++++++++++++-------------- 9 files changed, 43 insertions(+), 44 deletions(-) diff --git a/dom/base/CustomElementRegistry.cpp b/dom/base/CustomElementRegistry.cpp index ac194bb1f738..6a6f241c5e1c 100644 --- a/dom/base/CustomElementRegistry.cpp +++ b/dom/base/CustomElementRegistry.cpp @@ -565,7 +565,7 @@ CandidateFinder::OrderedCandidates() nsTArray> orderedElements(mCandidates.Count()); for (Element* child = mDoc->GetFirstElementChild(); child; child = child->GetNextElementSibling()) { - if (!Traverse(child->AsElement(), orderedElements)) { + if (!Traverse(child, orderedElements)) { break; } } diff --git a/dom/base/DirectionalityUtils.cpp b/dom/base/DirectionalityUtils.cpp index 8cc5bdc4de45..a90804e5e142 100644 --- a/dom/base/DirectionalityUtils.cpp +++ b/dom/base/DirectionalityUtils.cpp @@ -509,7 +509,6 @@ private: static nsCheapSetOperator SetNodeDirection(nsPtrHashKey* aEntry, void* aDir) { - MOZ_ASSERT(aEntry->GetKey()->IsElement(), "Must be an Element"); aEntry->GetKey()->SetDirectionality(*reinterpret_cast(aDir), true); return OpNext; @@ -523,7 +522,6 @@ private: static nsCheapSetOperator ResetNodeDirection(nsPtrHashKey* aEntry, void* aData) { - MOZ_ASSERT(aEntry->GetKey()->IsElement(), "Must be an Element"); // run the downward propagation algorithm // and remove the text node from the map nsTextNodeDirectionalityMapAndElement* data = diff --git a/dom/base/Element.h b/dom/base/Element.h index 1a3497a0476d..956faa19955f 100644 --- a/dom/base/Element.h +++ b/dom/base/Element.h @@ -1964,6 +1964,10 @@ private: nsIScrollableFrame* GetScrollFrame(nsIFrame **aStyledFrame = nullptr, FlushType aFlushType = FlushType::Layout); + // Prevent people from doing pointless checks/casts on Element instances. + void IsElement() = delete; + void AsElement() = delete; + // Data members EventStates mState; // Per-node data managed by Servo. diff --git a/dom/base/nsDocument.cpp b/dom/base/nsDocument.cpp index c1e76a11660b..be85eb0da53e 100644 --- a/dom/base/nsDocument.cpp +++ b/dom/base/nsDocument.cpp @@ -11263,7 +11263,7 @@ nsIDocument::ApplyFullscreen(const FullscreenRequest& aRequest) break; } nsIDocument* parent = child->GetParentDocument(); - Element* element = parent->FindContentForSubDocument(child)->AsElement(); + Element* element = parent->FindContentForSubDocument(child); if (static_cast(parent)->FullScreenStackPush(element)) { changed.AppendElement(parent); child = parent; diff --git a/dom/html/HTMLSelectElement.cpp b/dom/html/HTMLSelectElement.cpp index be6e58109097..257477d0e0a7 100644 --- a/dom/html/HTMLSelectElement.cpp +++ b/dom/html/HTMLSelectElement.cpp @@ -534,7 +534,7 @@ HTMLSelectElement::GetFirstOptionIndex(nsIContent* aOptions) int32_t listIndex = -1; HTMLOptionElement* optElement = HTMLOptionElement::FromNode(aOptions); if (optElement) { - mOptions->GetOptionIndex(optElement->AsElement(), 0, true, &listIndex); + mOptions->GetOptionIndex(optElement, 0, true, &listIndex); return listIndex; } diff --git a/editor/libeditor/HTMLEditRules.cpp b/editor/libeditor/HTMLEditRules.cpp index ed8ac5f306cf..cb139fe58002 100644 --- a/editor/libeditor/HTMLEditRules.cpp +++ b/editor/libeditor/HTMLEditRules.cpp @@ -5125,7 +5125,7 @@ HTMLEditRules::ConvertListType(Element* aList, } if (aList->IsHTMLElement(aListType)) { - RefPtr list = aList->AsElement(); + RefPtr list = aList; return list.forget(); } @@ -7407,7 +7407,7 @@ HTMLEditRules::SplitParagraph( } // Remove ID attribute on the paragraph from the existing right node. - rv = htmlEditor->RemoveAttributeWithTransaction(*aParentDivOrP.AsElement(), + rv = htmlEditor->RemoveAttributeWithTransaction(aParentDivOrP, *nsGkAtoms::id); NS_ENSURE_SUCCESS(rv, rv); diff --git a/layout/forms/nsNumberControlFrame.cpp b/layout/forms/nsNumberControlFrame.cpp index 675672f76baf..bad885dfca54 100644 --- a/layout/forms/nsNumberControlFrame.cpp +++ b/layout/forms/nsNumberControlFrame.cpp @@ -584,7 +584,7 @@ nsNumberControlFrame::IsFocused() const // Normally this depends on the state of our anonymous text control (which // takes focus for us), but in the case that it does not have a frame we will // have focus ourself. - return mTextField->AsElement()->State().HasState(NS_EVENT_STATE_FOCUS) || + return mTextField->State().HasState(NS_EVENT_STATE_FOCUS) || mContent->AsElement()->State().HasState(NS_EVENT_STATE_FOCUS); } diff --git a/layout/xul/tree/nsTreeBodyFrame.cpp b/layout/xul/tree/nsTreeBodyFrame.cpp index 7f3e3ce156fb..bf5497c97fb4 100644 --- a/layout/xul/tree/nsTreeBodyFrame.cpp +++ b/layout/xul/tree/nsTreeBodyFrame.cpp @@ -2032,17 +2032,15 @@ nsTreeBodyFrame::PrefillPropertyArray(int32_t aRowIndex, nsTreeColumn* aCol) } // Read special properties from attributes on the column content node - if (aCol->mContent->IsElement() && - aCol->mContent->AsElement()->AttrValueIs(kNameSpaceID_None, - nsGkAtoms::insertbefore, - nsGkAtoms::_true, - eCaseMatters)) + if (aCol->mContent->AttrValueIs(kNameSpaceID_None, + nsGkAtoms::insertbefore, + nsGkAtoms::_true, + eCaseMatters)) mScratchArray.AppendElement(nsGkAtoms::insertbefore); - if (aCol->mContent->IsElement() && - aCol->mContent->AsElement()->AttrValueIs(kNameSpaceID_None, - nsGkAtoms::insertafter, - nsGkAtoms::_true, - eCaseMatters)) + if (aCol->mContent->AttrValueIs(kNameSpaceID_None, + nsGkAtoms::insertafter, + nsGkAtoms::_true, + eCaseMatters)) mScratchArray.AppendElement(nsGkAtoms::insertafter); } } diff --git a/layout/xul/tree/nsTreeColumns.cpp b/layout/xul/tree/nsTreeColumns.cpp index b6d6b2dc5c9c..f4dfb8e4b467 100644 --- a/layout/xul/tree/nsTreeColumns.cpp +++ b/layout/xul/tree/nsTreeColumns.cpp @@ -265,7 +265,7 @@ nsTreeColumn::Invalidate() NS_ENSURE_TRUE(frame, NS_ERROR_FAILURE); // Fetch the Id. - mContent->AsElement()->GetAttr(kNameSpaceID_None, nsGkAtoms::id, mId); + mContent->GetAttr(kNameSpaceID_None, nsGkAtoms::id, mId); // If we have an Id, cache the Id as an atom. if (!mId.IsEmpty()) { @@ -273,7 +273,7 @@ nsTreeColumn::Invalidate() } // Cache our index. - nsTreeUtils::GetColumnIndex(mContent->AsElement(), &mIndex); + nsTreeUtils::GetColumnIndex(mContent, &mIndex); const nsStyleVisibility* vis = frame->StyleVisibility(); @@ -294,38 +294,38 @@ nsTreeColumn::Invalidate() // Figure out if we're the primary column (that has to have indentation // and twisties drawn. - mIsPrimary = mContent->AsElement()->AttrValueIs(kNameSpaceID_None, - nsGkAtoms::primary, - nsGkAtoms::_true, - eCaseMatters); + mIsPrimary = mContent->AttrValueIs(kNameSpaceID_None, + nsGkAtoms::primary, + nsGkAtoms::_true, + eCaseMatters); // Figure out if we're a cycling column (one that doesn't cause a selection // to happen). mIsCycler = - mContent->AsElement()->AttrValueIs(kNameSpaceID_None, nsGkAtoms::cycler, - nsGkAtoms::_true, eCaseMatters); + mContent->AttrValueIs(kNameSpaceID_None, nsGkAtoms::cycler, + nsGkAtoms::_true, eCaseMatters); mIsEditable = - mContent->AsElement()->AttrValueIs(kNameSpaceID_None, nsGkAtoms::editable, - nsGkAtoms::_true, eCaseMatters); + mContent->AttrValueIs(kNameSpaceID_None, nsGkAtoms::editable, + nsGkAtoms::_true, eCaseMatters); mIsSelectable = - !mContent->AsElement()->AttrValueIs(kNameSpaceID_None, nsGkAtoms::selectable, - nsGkAtoms::_false, eCaseMatters); + !mContent->AttrValueIs(kNameSpaceID_None, nsGkAtoms::selectable, + nsGkAtoms::_false, eCaseMatters); mOverflow = - mContent->AsElement()->AttrValueIs(kNameSpaceID_None, nsGkAtoms::overflow, - nsGkAtoms::_true, eCaseMatters); + mContent->AttrValueIs(kNameSpaceID_None, nsGkAtoms::overflow, + nsGkAtoms::_true, eCaseMatters); // Figure out our column type. Default type is text. mType = nsITreeColumn::TYPE_TEXT; static Element::AttrValuesArray typestrings[] = {&nsGkAtoms::checkbox, &nsGkAtoms::password, nullptr}; - switch (mContent->AsElement()->FindAttrValueIn(kNameSpaceID_None, - nsGkAtoms::type, - typestrings, - eCaseMatters)) { + switch (mContent->FindAttrValueIn(kNameSpaceID_None, + nsGkAtoms::type, + typestrings, + eCaseMatters)) { case 0: mType = nsITreeColumn::TYPE_CHECKBOX; break; case 1: mType = nsITreeColumn::TYPE_PASSWORD; break; } @@ -334,9 +334,9 @@ nsTreeColumn::Invalidate() mCropStyle = 0; static Element::AttrValuesArray cropstrings[] = {&nsGkAtoms::center, &nsGkAtoms::left, &nsGkAtoms::start, nullptr}; - switch (mContent->AsElement()->FindAttrValueIn(kNameSpaceID_None, - nsGkAtoms::crop, cropstrings, - eCaseMatters)) { + switch (mContent->FindAttrValueIn(kNameSpaceID_None, + nsGkAtoms::crop, cropstrings, + eCaseMatters)) { case 0: mCropStyle = 1; break; @@ -535,11 +535,10 @@ nsTreeColumns::GetKeyColumn() for (nsTreeColumn* currCol = mFirstColumn; currCol; currCol = currCol->GetNext()) { // Skip hidden columns. if (!currCol->mContent || - !currCol->mContent->IsElement() || - currCol->mContent->AsElement()->AttrValueIs(kNameSpaceID_None, - nsGkAtoms::hidden, - nsGkAtoms::_true, - eCaseMatters)) + currCol->mContent->AttrValueIs(kNameSpaceID_None, + nsGkAtoms::hidden, + nsGkAtoms::_true, + eCaseMatters)) continue; // Skip non-text column