diff options
Diffstat (limited to 'dom/base/ChildIterator.cpp')
-rw-r--r-- | dom/base/ChildIterator.cpp | 301 |
1 files changed, 122 insertions, 179 deletions
diff --git a/dom/base/ChildIterator.cpp b/dom/base/ChildIterator.cpp index d8c454ae8..19156aff7 100644 --- a/dom/base/ChildIterator.cpp +++ b/dom/base/ChildIterator.cpp @@ -6,61 +6,27 @@ #include "ChildIterator.h" #include "nsContentUtils.h" +#include "mozilla/dom/HTMLSlotElement.h" #include "mozilla/dom/XBLChildrenElement.h" -#include "mozilla/dom/HTMLContentElement.h" -#include "mozilla/dom/HTMLShadowElement.h" #include "mozilla/dom/ShadowRoot.h" #include "nsIAnonymousContentCreator.h" #include "nsIFrame.h" #include "nsCSSAnonBoxes.h" +#include "nsDocument.h" namespace mozilla { namespace dom { -class MatchedNodes { -public: - explicit MatchedNodes(HTMLContentElement* aInsertionPoint) - : mIsContentElement(true), mContentElement(aInsertionPoint) {} - - explicit MatchedNodes(XBLChildrenElement* aInsertionPoint) - : mIsContentElement(false), mChildrenElement(aInsertionPoint) {} - - uint32_t Length() const - { - return mIsContentElement ? mContentElement->MatchedNodes().Length() - : mChildrenElement->InsertedChildrenLength(); - } - - nsIContent* operator[](int32_t aIndex) const - { - return mIsContentElement ? mContentElement->MatchedNodes()[aIndex] - : mChildrenElement->InsertedChild(aIndex); - } - - bool IsEmpty() const - { - return mIsContentElement ? mContentElement->MatchedNodes().IsEmpty() - : !mChildrenElement->HasInsertedChildren(); - } -protected: - bool mIsContentElement; - union { - HTMLContentElement* mContentElement; - XBLChildrenElement* mChildrenElement; - }; -}; - -static inline MatchedNodes -GetMatchedNodesForPoint(nsIContent* aContent) +ExplicitChildIterator::ExplicitChildIterator(const nsIContent* aParent, + bool aStartAtBeginning) + : mParent(aParent), + mChild(nullptr), + mDefaultChild(nullptr), + mIsFirst(aStartAtBeginning), + mIndexInInserted(0) { - if (aContent->NodeInfo()->Equals(nsGkAtoms::children, kNameSpaceID_XBL)) { - // XBL case - return MatchedNodes(static_cast<XBLChildrenElement*>(aContent)); - } - - // Web components case - MOZ_ASSERT(aContent->IsHTMLElement(nsGkAtoms::content)); - return MatchedNodes(HTMLContentElement::FromContent(aContent)); + mParentAsSlot = nsDocument::IsWebComponentsEnabled(mParent) ? + HTMLSlotElement::FromContent(mParent) : nullptr; } nsIContent* @@ -69,30 +35,29 @@ ExplicitChildIterator::GetNextChild() // If we're already in the inserted-children array, look there first if (mIndexInInserted) { MOZ_ASSERT(mChild); - MOZ_ASSERT(nsContentUtils::IsContentInsertionPoint(mChild)); MOZ_ASSERT(!mDefaultChild); - MatchedNodes assignedChildren = GetMatchedNodesForPoint(mChild); - if (mIndexInInserted < assignedChildren.Length()) { - return assignedChildren[mIndexInInserted++]; - } - mIndexInInserted = 0; - mChild = mChild->GetNextSibling(); - } else if (mShadowIterator) { - // If we're inside of a <shadow> element, look through the - // explicit children of the projected ShadowRoot via - // the mShadowIterator. - nsIContent* nextChild = mShadowIterator->GetNextChild(); - if (nextChild) { - return nextChild; + if (mParentAsSlot) { + const nsTArray<RefPtr<nsINode>>& assignedNodes = + mParentAsSlot->AssignedNodes(); + + mChild = (mIndexInInserted < assignedNodes.Length()) ? + assignedNodes[mIndexInInserted++]->AsContent() : nullptr; + return mChild; } - mShadowIterator = nullptr; + MOZ_ASSERT(mChild->IsActiveChildrenElement()); + auto* childrenElement = + static_cast<XBLChildrenElement*>(mChild); + if (mIndexInInserted < childrenElement->InsertedChildrenLength()) { + return childrenElement->InsertedChild(mIndexInInserted++); + } + mIndexInInserted = 0; mChild = mChild->GetNextSibling(); } else if (mDefaultChild) { // If we're already in default content, check if there are more nodes there MOZ_ASSERT(mChild); - MOZ_ASSERT(nsContentUtils::IsContentInsertionPoint(mChild)); + MOZ_ASSERT(mChild->IsActiveChildrenElement()); mDefaultChild = mDefaultChild->GetNextSibling(); if (mDefaultChild) { @@ -101,6 +66,19 @@ ExplicitChildIterator::GetNextChild() mChild = mChild->GetNextSibling(); } else if (mIsFirst) { // at the beginning of the child list + // For slot parent, iterate over assigned nodes if not empty, otherwise + // fall through and iterate over direct children (fallback content). + if (mParentAsSlot) { + const nsTArray<RefPtr<nsINode>>& assignedNodes = + mParentAsSlot->AssignedNodes(); + if (!assignedNodes.IsEmpty()) { + mIndexInInserted = 1; + mChild = assignedNodes[0]->AsContent(); + mIsFirst = false; + return mChild; + } + } + mChild = mParent->GetFirstChild(); mIsFirst = false; } else if (mChild) { // in the middle of the child list @@ -110,31 +88,16 @@ ExplicitChildIterator::GetNextChild() // Iterate until we find a non-insertion point, or an insertion point with // content. while (mChild) { - // If the current child being iterated is a shadow insertion point then - // the iterator needs to go into the projected ShadowRoot. - if (ShadowRoot::IsShadowInsertionPoint(mChild)) { - // Look for the next child in the projected ShadowRoot for the <shadow> - // element. - HTMLShadowElement* shadowElem = HTMLShadowElement::FromContent(mChild); - ShadowRoot* projectedShadow = shadowElem->GetOlderShadowRoot(); - if (projectedShadow) { - mShadowIterator = new ExplicitChildIterator(projectedShadow); - nsIContent* nextChild = mShadowIterator->GetNextChild(); - if (nextChild) { - return nextChild; - } - mShadowIterator = nullptr; - } - mChild = mChild->GetNextSibling(); - } else if (nsContentUtils::IsContentInsertionPoint(mChild)) { + if (mChild->IsActiveChildrenElement()) { // If the current child being iterated is a content insertion point // then the iterator needs to return the nodes distributed into // the content insertion point. - MatchedNodes assignedChildren = GetMatchedNodesForPoint(mChild); - if (!assignedChildren.IsEmpty()) { + auto* childrenElement = + static_cast<XBLChildrenElement*>(mChild); + if (childrenElement->HasInsertedChildren()) { // Iterate through elements projected on insertion point. mIndexInInserted = 1; - return assignedChildren[0]; + return childrenElement->InsertedChild(0); } // Insertion points inside fallback/default content @@ -192,19 +155,17 @@ FlattenedChildIterator::Init(bool aIgnoreXBL) } bool -ExplicitChildIterator::Seek(nsIContent* aChildToFind) +ExplicitChildIterator::Seek(const nsIContent* aChildToFind) { if (aChildToFind->GetParent() == mParent && !aChildToFind->IsRootOfAnonymousSubtree()) { // Fast path: just point ourselves to aChildToFind, which is a // normal DOM child of ours. - MOZ_ASSERT(!ShadowRoot::IsShadowInsertionPoint(aChildToFind)); - MOZ_ASSERT(!nsContentUtils::IsContentInsertionPoint(aChildToFind)); - mChild = aChildToFind; + mChild = const_cast<nsIContent*>(aChildToFind); mIndexInInserted = 0; - mShadowIterator = nullptr; mDefaultChild = nullptr; mIsFirst = false; + MOZ_ASSERT(!mChild->IsActiveChildrenElement()); return true; } @@ -220,12 +181,18 @@ ExplicitChildIterator::Get() const { MOZ_ASSERT(!mIsFirst); + // When mParentAsSlot is set, mChild is always set to the current child. It + // does not matter whether mChild is an assigned node or a fallback content. + if (mParentAsSlot) { + return mChild; + } + if (mIndexInInserted) { - MatchedNodes assignedChildren = GetMatchedNodesForPoint(mChild); - return assignedChildren[mIndexInInserted - 1]; - } else if (mShadowIterator) { - return mShadowIterator->Get(); + MOZ_ASSERT(mChild->IsActiveChildrenElement()); + auto* childrenElement = static_cast<XBLChildrenElement*>(mChild); + return childrenElement->InsertedChild(mIndexInInserted - 1); } + return mDefaultChild ? mDefaultChild : mChild; } @@ -234,20 +201,28 @@ ExplicitChildIterator::GetPreviousChild() { // If we're already in the inserted-children array, look there first if (mIndexInInserted) { + + if (mParentAsSlot) { + const nsTArray<RefPtr<nsINode>>& assignedNodes = + mParentAsSlot->AssignedNodes(); + + mChild = (--mIndexInInserted) ? + assignedNodes[mIndexInInserted - 1]->AsContent() : nullptr; + + if (!mChild) { + mIsFirst = true; + } + return mChild; + } + // NB: mIndexInInserted points one past the last returned child so we need // to look *two* indices back in order to return the previous child. - MatchedNodes assignedChildren = GetMatchedNodesForPoint(mChild); + MOZ_ASSERT(mChild->IsActiveChildrenElement()); + auto* childrenElement = static_cast<XBLChildrenElement*>(mChild); if (--mIndexInInserted) { - return assignedChildren[mIndexInInserted - 1]; + return childrenElement->InsertedChild(mIndexInInserted - 1); } mChild = mChild->GetPreviousSibling(); - } else if (mShadowIterator) { - nsIContent* previousChild = mShadowIterator->GetPreviousChild(); - if (previousChild) { - return previousChild; - } - mShadowIterator = nullptr; - mChild = mChild->GetPreviousSibling(); } else if (mDefaultChild) { // If we're already in default content, check if there are more nodes there mDefaultChild = mDefaultChild->GetPreviousSibling(); @@ -261,35 +236,32 @@ ExplicitChildIterator::GetPreviousChild() } else if (mChild) { // in the middle of the child list mChild = mChild->GetPreviousSibling(); } else { // at the end of the child list + // For slot parent, iterate over assigned nodes if not empty, otherwise + // fall through and iterate over direct children (fallback content). + if (mParentAsSlot) { + const nsTArray<RefPtr<nsINode>>& assignedNodes = + mParentAsSlot->AssignedNodes(); + if (!assignedNodes.IsEmpty()) { + mIndexInInserted = assignedNodes.Length(); + mChild = assignedNodes[mIndexInInserted - 1]->AsContent(); + return mChild; + } + } + mChild = mParent->GetLastChild(); } // Iterate until we find a non-insertion point, or an insertion point with // content. while (mChild) { - if (ShadowRoot::IsShadowInsertionPoint(mChild)) { - // If the current child being iterated is a shadow insertion point then - // the iterator needs to go into the projected ShadowRoot. - HTMLShadowElement* shadowElem = HTMLShadowElement::FromContent(mChild); - ShadowRoot* projectedShadow = shadowElem->GetOlderShadowRoot(); - if (projectedShadow) { - // Create a ExplicitChildIterator that begins iterating from the end. - mShadowIterator = new ExplicitChildIterator(projectedShadow, false); - nsIContent* previousChild = mShadowIterator->GetPreviousChild(); - if (previousChild) { - return previousChild; - } - mShadowIterator = nullptr; - } - mChild = mChild->GetPreviousSibling(); - } else if (nsContentUtils::IsContentInsertionPoint(mChild)) { + if (mChild->IsActiveChildrenElement()) { // If the current child being iterated is a content insertion point // then the iterator needs to return the nodes distributed into // the content insertion point. - MatchedNodes assignedChildren = GetMatchedNodesForPoint(mChild); - if (!assignedChildren.IsEmpty()) { - mIndexInInserted = assignedChildren.Length(); - return assignedChildren[mIndexInInserted - 1]; + auto* childrenElement = static_cast<XBLChildrenElement*>(mChild); + if (childrenElement->HasInsertedChildren()) { + mIndexInInserted = childrenElement->InsertedChildrenLength(); + return childrenElement->InsertedChild(mIndexInInserted - 1); } mDefaultChild = mChild->GetLastChild(); @@ -317,11 +289,9 @@ AllChildrenIterator::Get() const { switch (mPhase) { case eAtBeforeKid: { - nsIFrame* frame = mOriginalContent->GetPrimaryFrame(); - MOZ_ASSERT(frame, "No frame at eAtBeforeKid phase"); - nsIFrame* beforeFrame = nsLayoutUtils::GetBeforeFrame(frame); - MOZ_ASSERT(beforeFrame, "No content before frame at eAtBeforeKid phase"); - return beforeFrame->GetContent(); + Element* before = nsLayoutUtils::GetBeforePseudo(mOriginalContent); + MOZ_ASSERT(before, "No content before frame at eAtBeforeKid phase"); + return before; } case eAtExplicitKids: @@ -331,11 +301,9 @@ AllChildrenIterator::Get() const return mAnonKids[mAnonKidsIdx]; case eAtAfterKid: { - nsIFrame* frame = mOriginalContent->GetPrimaryFrame(); - MOZ_ASSERT(frame, "No frame at eAtAfterKid phase"); - nsIFrame* afterFrame = nsLayoutUtils::GetAfterFrame(frame); - MOZ_ASSERT(afterFrame, "No content before frame at eAtBeforeKid phase"); - return afterFrame->GetContent(); + Element* after = nsLayoutUtils::GetAfterPseudo(mOriginalContent); + MOZ_ASSERT(after, "No content after frame at eAtAfterKid phase"); + return after; } default: @@ -345,19 +313,14 @@ AllChildrenIterator::Get() const bool -AllChildrenIterator::Seek(nsIContent* aChildToFind) +AllChildrenIterator::Seek(const nsIContent* aChildToFind) { if (mPhase == eAtBegin || mPhase == eAtBeforeKid) { mPhase = eAtExplicitKids; - nsIFrame* frame = mOriginalContent->GetPrimaryFrame(); - if (frame) { - nsIFrame* beforeFrame = nsLayoutUtils::GetBeforeFrame(frame); - if (beforeFrame) { - if (beforeFrame->GetContent() == aChildToFind) { - mPhase = eAtBeforeKid; - return true; - } - } + Element* beforePseudo = nsLayoutUtils::GetBeforePseudo(mOriginalContent); + if (beforePseudo && beforePseudo == aChildToFind) { + mPhase = eAtBeforeKid; + return true; } } @@ -383,12 +346,10 @@ AllChildrenIterator::AppendNativeAnonymousChildren() // The root scroll frame is not the primary frame of the root element. // Detect and handle this case. - if (mOriginalContent == mOriginalContent->OwnerDoc()->GetRootElement()) { - nsIPresShell* presShell = mOriginalContent->OwnerDoc()->GetShell(); - nsIFrame* scrollFrame = presShell ? presShell->GetRootScrollFrame() : nullptr; - if (scrollFrame) { - AppendNativeAnonymousChildrenFromFrame(scrollFrame); - } + if (!(mFlags & nsIContent::eSkipDocumentLevelNativeAnonymousContent) && + mOriginalContent == mOriginalContent->OwnerDoc()->GetRootElement()) { + nsContentUtils::AppendDocumentLevelNativeAnonymousContentTo( + mOriginalContent->OwnerDoc(), mAnonKids); } } @@ -406,13 +367,10 @@ AllChildrenIterator::GetNextChild() { if (mPhase == eAtBegin) { mPhase = eAtExplicitKids; - nsIFrame* frame = mOriginalContent->GetPrimaryFrame(); - if (frame) { - nsIFrame* beforeFrame = nsLayoutUtils::GetBeforeFrame(frame); - if (beforeFrame) { - mPhase = eAtBeforeKid; - return beforeFrame->GetContent(); - } + Element* beforeContent = nsLayoutUtils::GetBeforePseudo(mOriginalContent); + if (beforeContent) { + mPhase = eAtBeforeKid; + return beforeContent; } } @@ -448,13 +406,10 @@ AllChildrenIterator::GetNextChild() return mAnonKids[mAnonKidsIdx]; } - nsIFrame* frame = mOriginalContent->GetPrimaryFrame(); - if (frame) { - nsIFrame* afterFrame = nsLayoutUtils::GetAfterFrame(frame); - if (afterFrame) { - mPhase = eAtAfterKid; - return afterFrame->GetContent(); - } + Element* afterContent = nsLayoutUtils::GetAfterPseudo(mOriginalContent); + if (afterContent) { + mPhase = eAtAfterKid; + return afterContent; } } @@ -468,13 +423,10 @@ AllChildrenIterator::GetPreviousChild() if (mPhase == eAtEnd) { MOZ_ASSERT(mAnonKidsIdx == mAnonKids.Length()); mPhase = eAtAnonKids; - nsIFrame* frame = mOriginalContent->GetPrimaryFrame(); - if (frame) { - nsIFrame* afterFrame = nsLayoutUtils::GetAfterFrame(frame); - if (afterFrame) { - mPhase = eAtAfterKid; - return afterFrame->GetContent(); - } + Element* afterContent = nsLayoutUtils::GetAfterPseudo(mOriginalContent); + if (afterContent) { + mPhase = eAtAfterKid; + return afterContent; } } @@ -503,13 +455,10 @@ AllChildrenIterator::GetPreviousChild() return kid; } - nsIFrame* frame = mOriginalContent->GetPrimaryFrame(); - if (frame) { - nsIFrame* beforeFrame = nsLayoutUtils::GetBeforeFrame(frame); - if (beforeFrame) { - mPhase = eAtBeforeKid; - return beforeFrame->GetContent(); - } + Element* beforeContent = nsLayoutUtils::GetBeforePseudo(mOriginalContent); + if (beforeContent) { + mPhase = eAtBeforeKid; + return beforeContent; } } @@ -585,12 +534,6 @@ StyleChildrenIterator::IsNeeded(const Element* aElement) return true; } - // The root element has a scroll frame that is not the primary frame, so we - // need to do special checking for that case. - if (aElement == aElement->OwnerDoc()->GetRootElement()) { - return true; - } - return false; } |