2013-05-02 02:50:08 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=2 sw=2 et 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 "ChildIterator.h"
|
2013-12-02 14:26:12 +04:00
|
|
|
#include "nsContentUtils.h"
|
2013-07-02 02:09:37 +04:00
|
|
|
#include "mozilla/dom/XBLChildrenElement.h"
|
2013-12-02 14:26:12 +04:00
|
|
|
#include "mozilla/dom/HTMLContentElement.h"
|
2013-12-21 10:43:58 +04:00
|
|
|
#include "mozilla/dom/HTMLShadowElement.h"
|
|
|
|
#include "mozilla/dom/ShadowRoot.h"
|
2014-07-16 22:41:57 +04:00
|
|
|
#include "nsIAnonymousContentCreator.h"
|
|
|
|
#include "nsIFrame.h"
|
2013-05-02 02:50:08 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2013-12-02 14:26:12 +04:00
|
|
|
class MatchedNodes {
|
|
|
|
public:
|
2014-09-02 04:49:25 +04:00
|
|
|
explicit MatchedNodes(HTMLContentElement* aInsertionPoint)
|
2013-12-02 14:26:12 +04:00
|
|
|
: mIsContentElement(true), mContentElement(aInsertionPoint) {}
|
|
|
|
|
2014-09-02 04:49:25 +04:00
|
|
|
explicit MatchedNodes(XBLChildrenElement* aInsertionPoint)
|
2013-12-02 14:26:12 +04:00
|
|
|
: mIsContentElement(false), mChildrenElement(aInsertionPoint) {}
|
|
|
|
|
|
|
|
uint32_t Length() const
|
|
|
|
{
|
|
|
|
return mIsContentElement ? mContentElement->MatchedNodes().Length()
|
2014-06-22 15:53:01 +04:00
|
|
|
: mChildrenElement->InsertedChildrenLength();
|
2013-12-02 14:26:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsIContent* operator[](int32_t aIndex) const
|
|
|
|
{
|
|
|
|
return mIsContentElement ? mContentElement->MatchedNodes()[aIndex]
|
2014-06-22 15:53:01 +04:00
|
|
|
: mChildrenElement->InsertedChild(aIndex);
|
2013-12-02 14:26:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IsEmpty() const
|
|
|
|
{
|
|
|
|
return mIsContentElement ? mContentElement->MatchedNodes().IsEmpty()
|
2014-06-22 15:53:01 +04:00
|
|
|
: !mChildrenElement->HasInsertedChildren();
|
2013-12-02 14:26:12 +04:00
|
|
|
}
|
|
|
|
protected:
|
|
|
|
bool mIsContentElement;
|
|
|
|
union {
|
|
|
|
HTMLContentElement* mContentElement;
|
|
|
|
XBLChildrenElement* mChildrenElement;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
static inline MatchedNodes
|
|
|
|
GetMatchedNodesForPoint(nsIContent* aContent)
|
|
|
|
{
|
|
|
|
if (aContent->NodeInfo()->Equals(nsGkAtoms::children, kNameSpaceID_XBL)) {
|
|
|
|
// XBL case
|
|
|
|
return MatchedNodes(static_cast<XBLChildrenElement*>(aContent));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Web components case
|
2015-03-03 14:08:59 +03:00
|
|
|
MOZ_ASSERT(aContent->IsHTMLElement(nsGkAtoms::content));
|
2013-12-02 14:26:12 +04:00
|
|
|
return MatchedNodes(static_cast<HTMLContentElement*>(aContent));
|
|
|
|
}
|
|
|
|
|
2013-05-02 02:50:08 +04:00
|
|
|
nsIContent*
|
|
|
|
ExplicitChildIterator::GetNextChild()
|
|
|
|
{
|
|
|
|
// If we're already in the inserted-children array, look there first
|
|
|
|
if (mIndexInInserted) {
|
|
|
|
MOZ_ASSERT(mChild);
|
2013-12-02 14:26:12 +04:00
|
|
|
MOZ_ASSERT(nsContentUtils::IsContentInsertionPoint(mChild));
|
2013-05-02 02:50:08 +04:00
|
|
|
MOZ_ASSERT(!mDefaultChild);
|
|
|
|
|
2013-12-02 14:26:12 +04:00
|
|
|
MatchedNodes assignedChildren = GetMatchedNodesForPoint(mChild);
|
|
|
|
if (mIndexInInserted < assignedChildren.Length()) {
|
|
|
|
return assignedChildren[mIndexInInserted++];
|
2013-05-02 02:50:08 +04:00
|
|
|
}
|
|
|
|
mIndexInInserted = 0;
|
|
|
|
mChild = mChild->GetNextSibling();
|
2013-12-21 10:43:58 +04:00
|
|
|
} 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
mShadowIterator = nullptr;
|
|
|
|
mChild = mChild->GetNextSibling();
|
2013-05-02 02:50:08 +04:00
|
|
|
} else if (mDefaultChild) {
|
|
|
|
// If we're already in default content, check if there are more nodes there
|
|
|
|
MOZ_ASSERT(mChild);
|
2013-12-02 14:26:12 +04:00
|
|
|
MOZ_ASSERT(nsContentUtils::IsContentInsertionPoint(mChild));
|
2013-05-02 02:50:08 +04:00
|
|
|
|
|
|
|
mDefaultChild = mDefaultChild->GetNextSibling();
|
|
|
|
if (mDefaultChild) {
|
|
|
|
return mDefaultChild;
|
|
|
|
}
|
|
|
|
|
|
|
|
mChild = mChild->GetNextSibling();
|
|
|
|
} else if (mIsFirst) { // at the beginning of the child list
|
|
|
|
mChild = mParent->GetFirstChild();
|
|
|
|
mIsFirst = false;
|
|
|
|
} else if (mChild) { // in the middle of the child list
|
|
|
|
mChild = mChild->GetNextSibling();
|
|
|
|
}
|
|
|
|
|
2013-12-02 14:26:12 +04:00
|
|
|
// Iterate until we find a non-insertion point, or an insertion point with
|
|
|
|
// content.
|
2013-12-21 10:43:58 +04:00
|
|
|
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 = static_cast<HTMLShadowElement*>(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 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()) {
|
|
|
|
// Iterate through elements projected on insertion point.
|
|
|
|
mIndexInInserted = 1;
|
|
|
|
return assignedChildren[0];
|
|
|
|
}
|
2013-05-02 02:50:08 +04:00
|
|
|
|
2013-12-21 10:43:58 +04:00
|
|
|
// Insertion points inside fallback/default content
|
|
|
|
// are considered inactive and do not get assigned nodes.
|
|
|
|
mDefaultChild = mChild->GetFirstChild();
|
|
|
|
if (mDefaultChild) {
|
|
|
|
return mDefaultChild;
|
|
|
|
}
|
2013-05-02 02:50:08 +04:00
|
|
|
|
2013-12-21 10:43:58 +04:00
|
|
|
// If we have an insertion point with no assigned nodes and
|
|
|
|
// no default content, move on to the next node.
|
|
|
|
mChild = mChild->GetNextSibling();
|
|
|
|
} else {
|
|
|
|
// mChild is not an insertion point, thus it is the next node to
|
|
|
|
// return from this iterator.
|
|
|
|
break;
|
|
|
|
}
|
2013-05-02 02:50:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return mChild;
|
|
|
|
}
|
|
|
|
|
2014-07-16 22:41:57 +04:00
|
|
|
void
|
|
|
|
FlattenedChildIterator::Init(bool aIgnoreXBL)
|
2013-05-02 02:50:08 +04:00
|
|
|
{
|
2014-07-16 22:41:57 +04:00
|
|
|
if (aIgnoreXBL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-05-02 02:50:08 +04:00
|
|
|
nsXBLBinding* binding =
|
2014-07-16 22:41:57 +04:00
|
|
|
mParent->OwnerDoc()->BindingManager()->GetBindingWithContent(mParent);
|
2013-05-02 02:50:08 +04:00
|
|
|
|
|
|
|
if (binding) {
|
|
|
|
nsIContent* anon = binding->GetAnonymousContent();
|
|
|
|
if (anon) {
|
|
|
|
mParent = anon;
|
|
|
|
mXBLInvolved = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We set mXBLInvolved to true if either:
|
|
|
|
// - The node we're iterating has a binding with content attached to it.
|
|
|
|
// - The node is generated XBL content and has an <xbl:children> child.
|
2014-07-16 22:41:57 +04:00
|
|
|
if (!mXBLInvolved && mParent->GetBindingParent()) {
|
|
|
|
for (nsIContent* child = mParent->GetFirstChild();
|
2013-05-02 02:50:08 +04:00
|
|
|
child;
|
|
|
|
child = child->GetNextSibling()) {
|
|
|
|
if (child->NodeInfo()->Equals(nsGkAtoms::children, kNameSpaceID_XBL)) {
|
2013-07-12 01:52:49 +04:00
|
|
|
MOZ_ASSERT(child->GetBindingParent());
|
2013-05-02 02:50:08 +04:00
|
|
|
mXBLInvolved = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-21 10:43:58 +04:00
|
|
|
nsIContent*
|
|
|
|
ExplicitChildIterator::Get()
|
2013-05-02 02:50:08 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mIsFirst);
|
|
|
|
|
|
|
|
if (mIndexInInserted) {
|
2014-06-05 22:39:25 +04:00
|
|
|
MatchedNodes assignedChildren = GetMatchedNodesForPoint(mChild);
|
|
|
|
return assignedChildren[mIndexInInserted - 1];
|
2013-12-21 10:43:58 +04:00
|
|
|
} else if (mShadowIterator) {
|
|
|
|
return mShadowIterator->Get();
|
2013-05-02 02:50:08 +04:00
|
|
|
}
|
|
|
|
return mDefaultChild ? mDefaultChild : mChild;
|
|
|
|
}
|
|
|
|
|
2013-12-21 10:43:58 +04:00
|
|
|
nsIContent*
|
|
|
|
ExplicitChildIterator::GetPreviousChild()
|
2013-05-02 02:50:08 +04:00
|
|
|
{
|
|
|
|
// If we're already in the inserted-children array, look there first
|
|
|
|
if (mIndexInInserted) {
|
|
|
|
// NB: mIndexInInserted points one past the last returned child so we need
|
|
|
|
// to look *two* indices back in order to return the previous child.
|
2013-12-02 14:26:12 +04:00
|
|
|
MatchedNodes assignedChildren = GetMatchedNodesForPoint(mChild);
|
2013-05-02 02:50:08 +04:00
|
|
|
if (--mIndexInInserted) {
|
2013-12-02 14:26:12 +04:00
|
|
|
return assignedChildren[mIndexInInserted - 1];
|
2013-05-02 02:50:08 +04:00
|
|
|
}
|
|
|
|
mChild = mChild->GetPreviousSibling();
|
2013-12-21 10:43:58 +04:00
|
|
|
} else if (mShadowIterator) {
|
|
|
|
nsIContent* previousChild = mShadowIterator->GetPreviousChild();
|
|
|
|
if (previousChild) {
|
|
|
|
return previousChild;
|
|
|
|
}
|
|
|
|
mShadowIterator = nullptr;
|
|
|
|
mChild = mChild->GetPreviousSibling();
|
2013-05-02 02:50:08 +04:00
|
|
|
} else if (mDefaultChild) {
|
|
|
|
// If we're already in default content, check if there are more nodes there
|
|
|
|
mDefaultChild = mDefaultChild->GetPreviousSibling();
|
|
|
|
if (mDefaultChild) {
|
|
|
|
return mDefaultChild;
|
|
|
|
}
|
|
|
|
|
|
|
|
mChild = mChild->GetPreviousSibling();
|
|
|
|
} else if (mIsFirst) { // at the beginning of the child list
|
|
|
|
return nullptr;
|
|
|
|
} else if (mChild) { // in the middle of the child list
|
|
|
|
mChild = mChild->GetPreviousSibling();
|
|
|
|
} else { // at the end of the child list
|
|
|
|
mChild = mParent->GetLastChild();
|
|
|
|
}
|
|
|
|
|
2013-12-02 14:26:12 +04:00
|
|
|
// Iterate until we find a non-insertion point, or an insertion point with
|
|
|
|
// content.
|
2013-12-21 10:43:58 +04:00
|
|
|
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 = static_cast<HTMLShadowElement*>(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 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];
|
|
|
|
}
|
2013-05-02 02:50:08 +04:00
|
|
|
|
2013-12-21 10:43:58 +04:00
|
|
|
mDefaultChild = mChild->GetLastChild();
|
|
|
|
if (mDefaultChild) {
|
|
|
|
return mDefaultChild;
|
|
|
|
}
|
2013-05-02 02:50:08 +04:00
|
|
|
|
2013-12-21 10:43:58 +04:00
|
|
|
mChild = mChild->GetPreviousSibling();
|
|
|
|
} else {
|
|
|
|
// mChild is not an insertion point, thus it is the next node to
|
|
|
|
// return from this iterator.
|
|
|
|
break;
|
|
|
|
}
|
2013-05-02 02:50:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!mChild) {
|
|
|
|
mIsFirst = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mChild;
|
|
|
|
}
|
|
|
|
|
2014-07-16 22:41:57 +04:00
|
|
|
nsIContent*
|
|
|
|
AllChildrenIterator::GetNextChild()
|
|
|
|
{
|
|
|
|
if (mPhase == eNeedBeforeKid) {
|
|
|
|
mPhase = eNeedExplicitKids;
|
|
|
|
nsIFrame* frame = mOriginalContent->GetPrimaryFrame();
|
|
|
|
if (frame) {
|
|
|
|
nsIFrame* beforeFrame = nsLayoutUtils::GetBeforeFrame(frame);
|
|
|
|
if (beforeFrame) {
|
|
|
|
return beforeFrame->GetContent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mPhase == eNeedExplicitKids) {
|
|
|
|
nsIContent* kid = ExplicitChildIterator::GetNextChild();
|
|
|
|
if (kid) {
|
|
|
|
return kid;
|
|
|
|
}
|
|
|
|
|
|
|
|
mPhase = eNeedAnonKids;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mPhase == eNeedAnonKids) {
|
|
|
|
if (mAnonKids.IsEmpty()) {
|
|
|
|
nsIAnonymousContentCreator* ac =
|
|
|
|
do_QueryFrame(mOriginalContent->GetPrimaryFrame());
|
|
|
|
if (ac) {
|
|
|
|
ac->AppendAnonymousContentTo(mAnonKids, mFlags);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mAnonKids.IsEmpty()) {
|
|
|
|
nsIContent* nextKid = mAnonKids[0];
|
|
|
|
mAnonKids.RemoveElementAt(0);
|
|
|
|
if (mAnonKids.IsEmpty()) {
|
|
|
|
mPhase = eNeedAfterKid;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nextKid;
|
|
|
|
}
|
|
|
|
|
|
|
|
mPhase = eNeedAfterKid;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mPhase == eNeedAfterKid) {
|
|
|
|
mPhase = eDone;
|
|
|
|
nsIFrame* frame = mOriginalContent->GetPrimaryFrame();
|
|
|
|
if (frame) {
|
|
|
|
nsIFrame* afterFrame = nsLayoutUtils::GetAfterFrame(frame);
|
|
|
|
if (afterFrame) {
|
|
|
|
return afterFrame->GetContent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
2013-05-02 02:50:08 +04:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|