2015-05-03 19:32:37 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2013-05-01 22:50:08 +00:00
|
|
|
/* 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/. */
|
|
|
|
|
2013-11-15 02:46:28 +00:00
|
|
|
#ifndef ChildIterator_h
|
|
|
|
#define ChildIterator_h
|
|
|
|
|
2014-07-16 18:41:57 +00:00
|
|
|
#include "nsIContent.h"
|
|
|
|
|
2013-05-01 22:50:08 +00:00
|
|
|
/**
|
|
|
|
* Iterates over the children on a node. If a child is an insertion point,
|
|
|
|
* iterates over the children inserted there instead, or the default content
|
|
|
|
* if no children are inserted there.
|
|
|
|
*
|
|
|
|
* The FlattenedChildIterator expands any anonymous content bound from an XBL
|
|
|
|
* binding's <xbl:content> element.
|
|
|
|
*/
|
|
|
|
|
2014-08-18 14:44:50 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
|
|
|
|
class nsIContent;
|
2013-05-01 22:50:08 +00:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
// This class iterates normal DOM child nodes of a given DOM node with
|
|
|
|
// <xbl:children> nodes replaced by the elements that have been filtered into that
|
|
|
|
// insertion point. Any bindings on the given element are ignored for purposes
|
2013-12-21 06:43:58 +00:00
|
|
|
// of determining which insertion point children are filtered into. The iterator
|
|
|
|
// can be initialized to start at the end by providing false for aStartAtBeginning
|
|
|
|
// in order to start iterating in reverse from the last child.
|
2013-05-01 22:50:08 +00:00
|
|
|
class ExplicitChildIterator
|
|
|
|
{
|
|
|
|
public:
|
2016-10-18 04:29:03 +00:00
|
|
|
explicit ExplicitChildIterator(const nsIContent* aParent,
|
|
|
|
bool aStartAtBeginning = true)
|
2013-05-01 22:50:08 +00:00
|
|
|
: mParent(aParent),
|
|
|
|
mChild(nullptr),
|
|
|
|
mDefaultChild(nullptr),
|
|
|
|
mIndexInInserted(0),
|
2013-12-21 06:43:58 +00:00
|
|
|
mIsFirst(aStartAtBeginning)
|
2013-05-01 22:50:08 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-08-06 18:21:28 +00:00
|
|
|
ExplicitChildIterator(const ExplicitChildIterator& aOther)
|
|
|
|
: mParent(aOther.mParent), mChild(aOther.mChild),
|
|
|
|
mDefaultChild(aOther.mDefaultChild),
|
|
|
|
mShadowIterator(aOther.mShadowIterator ?
|
|
|
|
new ExplicitChildIterator(*aOther.mShadowIterator) :
|
|
|
|
nullptr),
|
|
|
|
mIndexInInserted(aOther.mIndexInInserted), mIsFirst(aOther.mIsFirst) {}
|
|
|
|
|
|
|
|
ExplicitChildIterator(ExplicitChildIterator&& aOther)
|
|
|
|
: mParent(aOther.mParent), mChild(aOther.mChild),
|
|
|
|
mDefaultChild(aOther.mDefaultChild),
|
|
|
|
mShadowIterator(Move(aOther.mShadowIterator)),
|
|
|
|
mIndexInInserted(aOther.mIndexInInserted), mIsFirst(aOther.mIsFirst) {}
|
|
|
|
|
2013-05-01 22:50:08 +00:00
|
|
|
nsIContent* GetNextChild();
|
|
|
|
|
2015-09-09 01:23:55 +00:00
|
|
|
// Looks for aChildToFind respecting insertion points until aChildToFind is
|
|
|
|
// found. This version can take shortcuts that the two-argument version
|
|
|
|
// can't, so can be faster (and in fact can be O(1) instead of O(N) in many
|
|
|
|
// cases).
|
2016-02-11 13:22:39 +00:00
|
|
|
bool Seek(nsIContent* aChildToFind);
|
2015-09-09 01:23:55 +00:00
|
|
|
|
|
|
|
// Looks for aChildToFind respecting insertion points until aChildToFind is found.
|
2013-12-02 10:26:12 +00:00
|
|
|
// or aBound is found. If aBound is nullptr then the seek is unbounded. Returns
|
|
|
|
// whether aChildToFind was found as an explicit child prior to encountering
|
|
|
|
// aBound.
|
2015-09-09 01:23:55 +00:00
|
|
|
bool Seek(nsIContent* aChildToFind, nsIContent* aBound)
|
2013-12-02 10:26:12 +00:00
|
|
|
{
|
|
|
|
// It would be nice to assert that we find aChildToFind, but bz thinks that
|
|
|
|
// we might not find aChildToFind when called from ContentInserted
|
|
|
|
// if first-letter frames are about.
|
|
|
|
|
2015-09-09 01:23:55 +00:00
|
|
|
// We can't easily take shortcuts here because we'd have to have a way to
|
|
|
|
// compare aChildToFind to aBound.
|
2013-12-02 10:26:12 +00:00
|
|
|
nsIContent* child;
|
|
|
|
do {
|
|
|
|
child = GetNextChild();
|
|
|
|
} while (child && child != aChildToFind && child != aBound);
|
|
|
|
|
|
|
|
return child == aChildToFind;
|
|
|
|
}
|
|
|
|
|
2013-12-21 06:43:58 +00:00
|
|
|
// Returns the current target of this iterator (which might be an explicit
|
|
|
|
// child of the node, fallback content of an insertion point or
|
|
|
|
// a node distributed to an insertion point.
|
2016-03-08 20:54:46 +00:00
|
|
|
nsIContent* Get() const;
|
2013-12-21 06:43:58 +00:00
|
|
|
|
|
|
|
// The inverse of GetNextChild. Properly steps in and out of insertion
|
|
|
|
// points.
|
|
|
|
nsIContent* GetPreviousChild();
|
|
|
|
|
2013-05-01 22:50:08 +00:00
|
|
|
protected:
|
|
|
|
// The parent of the children being iterated. For the FlattenedChildIterator,
|
|
|
|
// if there is a binding attached to the original parent, mParent points to
|
|
|
|
// the <xbl:content> element for the binding.
|
2016-10-18 04:29:03 +00:00
|
|
|
const nsIContent* mParent;
|
2013-05-01 22:50:08 +00:00
|
|
|
|
2013-12-21 06:43:58 +00:00
|
|
|
// The current child. When we encounter an insertion point,
|
2013-05-01 22:50:08 +00:00
|
|
|
// mChild remains as the insertion point whose content we're iterating (and
|
|
|
|
// our state is controled by mDefaultChild or mIndexInInserted depending on
|
|
|
|
// whether the insertion point expands to its default content or not).
|
|
|
|
nsIContent* mChild;
|
|
|
|
|
|
|
|
// If non-null, this points to the current default content for the current
|
|
|
|
// insertion point that we're iterating (i.e. mChild, which must be an
|
2013-12-21 06:43:58 +00:00
|
|
|
// nsXBLChildrenElement or HTMLContentElement). Once this transitions back
|
|
|
|
// to null, we continue iterating at mChild's next sibling.
|
2013-05-01 22:50:08 +00:00
|
|
|
nsIContent* mDefaultChild;
|
|
|
|
|
2013-12-21 06:43:58 +00:00
|
|
|
// If non-null, this points to an iterator of the explicit children of
|
|
|
|
// the ShadowRoot projected by the current shadow element that we're
|
|
|
|
// iterating.
|
|
|
|
nsAutoPtr<ExplicitChildIterator> mShadowIterator;
|
|
|
|
|
2013-05-01 22:50:08 +00:00
|
|
|
// If not zero, we're iterating inserted children for an insertion point. This
|
|
|
|
// is an index into mChild's inserted children array (mChild must be an
|
|
|
|
// nsXBLChildrenElement). The index is one past the "current" child (as
|
|
|
|
// opposed to mChild which represents the "current" child).
|
|
|
|
uint32_t mIndexInInserted;
|
|
|
|
|
|
|
|
// A flag to let us know that we haven't started iterating yet.
|
|
|
|
bool mIsFirst;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Iterates over the flattened children of a node, which accounts for anonymous
|
|
|
|
// children and nodes moved by insertion points. If a node has anonymous
|
2014-10-02 13:05:16 +00:00
|
|
|
// children, those are iterated over. The iterator can be initialized to start
|
|
|
|
// at the end by providing false for aStartAtBeginning in order to start
|
|
|
|
// iterating in reverse from the last child.
|
2013-05-01 22:50:08 +00:00
|
|
|
class FlattenedChildIterator : public ExplicitChildIterator
|
|
|
|
{
|
|
|
|
public:
|
2016-10-18 04:29:03 +00:00
|
|
|
explicit FlattenedChildIterator(const nsIContent* aParent,
|
|
|
|
bool aStartAtBeginning = true)
|
2014-10-02 13:05:16 +00:00
|
|
|
: ExplicitChildIterator(aParent, aStartAtBeginning), mXBLInvolved(false)
|
2014-07-16 18:41:57 +00:00
|
|
|
{
|
|
|
|
Init(false);
|
|
|
|
}
|
2013-05-01 22:50:08 +00:00
|
|
|
|
2014-08-06 18:21:28 +00:00
|
|
|
FlattenedChildIterator(FlattenedChildIterator&& aOther)
|
|
|
|
: ExplicitChildIterator(Move(aOther)), mXBLInvolved(aOther.mXBLInvolved) {}
|
|
|
|
|
|
|
|
FlattenedChildIterator(const FlattenedChildIterator& aOther)
|
|
|
|
: ExplicitChildIterator(aOther), mXBLInvolved(aOther.mXBLInvolved) {}
|
|
|
|
|
2013-05-01 22:50:08 +00:00
|
|
|
bool XBLInvolved() { return mXBLInvolved; }
|
|
|
|
|
2014-07-16 18:41:57 +00:00
|
|
|
protected:
|
|
|
|
/**
|
|
|
|
* This constructor is a hack to help AllChildrenIterator which sometimes
|
|
|
|
* doesn't want to consider XBL.
|
|
|
|
*/
|
2016-10-18 04:29:03 +00:00
|
|
|
FlattenedChildIterator(const nsIContent* aParent, uint32_t aFlags,
|
|
|
|
bool aStartAtBeginning = true)
|
2014-10-02 13:05:16 +00:00
|
|
|
: ExplicitChildIterator(aParent, aStartAtBeginning), mXBLInvolved(false)
|
2014-07-16 18:41:57 +00:00
|
|
|
{
|
2014-10-02 13:05:16 +00:00
|
|
|
bool ignoreXBL = aFlags & nsIContent::eAllButXBL;
|
|
|
|
Init(ignoreXBL);
|
2014-07-16 18:41:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Init(bool aIgnoreXBL);
|
|
|
|
|
2013-05-01 22:50:08 +00:00
|
|
|
// For certain optimizations, nsCSSFrameConstructor needs to know if the
|
|
|
|
// child list of the element that we're iterating matches its .childNodes.
|
|
|
|
bool mXBLInvolved;
|
|
|
|
};
|
|
|
|
|
2014-07-16 18:41:57 +00:00
|
|
|
/**
|
2016-03-08 20:54:46 +00:00
|
|
|
* AllChildrenIterator traverses the children of an element including before /
|
|
|
|
* after content and optionally XBL children. The iterator can be initialized
|
|
|
|
* to start at the end by providing false for aStartAtBeginning in order to
|
|
|
|
* start iterating in reverse from the last child.
|
|
|
|
*
|
|
|
|
* Note: it assumes that no mutation of the DOM or frame tree takes place during
|
|
|
|
* iteration, and will break horribly if that is not true.
|
2014-07-16 18:41:57 +00:00
|
|
|
*/
|
|
|
|
class AllChildrenIterator : private FlattenedChildIterator
|
|
|
|
{
|
|
|
|
public:
|
2016-10-18 04:29:03 +00:00
|
|
|
AllChildrenIterator(const nsIContent* aNode, uint32_t aFlags,
|
|
|
|
bool aStartAtBeginning = true) :
|
2014-10-02 13:05:16 +00:00
|
|
|
FlattenedChildIterator(aNode, aFlags, aStartAtBeginning),
|
2016-03-05 21:11:21 +00:00
|
|
|
mOriginalContent(aNode), mAnonKidsIdx(aStartAtBeginning ? UINT32_MAX : 0),
|
|
|
|
mFlags(aFlags), mPhase(aStartAtBeginning ? eAtBegin : eAtEnd) { }
|
2014-07-16 18:41:57 +00:00
|
|
|
|
2014-08-06 18:21:28 +00:00
|
|
|
AllChildrenIterator(AllChildrenIterator&& aOther)
|
|
|
|
: FlattenedChildIterator(Move(aOther)),
|
|
|
|
mOriginalContent(aOther.mOriginalContent),
|
2016-03-05 21:11:21 +00:00
|
|
|
mAnonKids(Move(aOther.mAnonKids)), mAnonKidsIdx(aOther.mAnonKidsIdx),
|
|
|
|
mFlags(aOther.mFlags), mPhase(aOther.mPhase)
|
2014-08-06 18:21:28 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
, mMutationGuard(aOther.mMutationGuard)
|
|
|
|
#endif
|
|
|
|
{}
|
|
|
|
|
2014-07-16 18:41:57 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
~AllChildrenIterator() { MOZ_ASSERT(!mMutationGuard.Mutated(0)); }
|
|
|
|
#endif
|
|
|
|
|
2016-03-08 20:54:46 +00:00
|
|
|
// Returns the current target the iterator is at, or null if the iterator
|
|
|
|
// doesn't point to any child node (either eAtBegin or eAtEnd phase).
|
|
|
|
nsIContent* Get() const;
|
|
|
|
|
|
|
|
// Seeks the given node in children of a parent element, starting from
|
|
|
|
// the current iterator's position, and sets the iterator at the given child
|
|
|
|
// node if it was found.
|
2016-02-11 13:22:39 +00:00
|
|
|
bool Seek(nsIContent* aChildToFind);
|
|
|
|
|
2014-07-16 18:41:57 +00:00
|
|
|
nsIContent* GetNextChild();
|
2016-03-05 21:11:21 +00:00
|
|
|
nsIContent* GetPreviousChild();
|
2016-10-18 04:29:03 +00:00
|
|
|
const nsIContent* Parent() const { return mOriginalContent; }
|
2014-07-16 18:41:57 +00:00
|
|
|
|
|
|
|
enum IteratorPhase
|
|
|
|
{
|
2016-03-05 21:11:21 +00:00
|
|
|
eAtBegin,
|
|
|
|
eAtBeforeKid,
|
|
|
|
eAtExplicitKids,
|
|
|
|
eAtAnonKids,
|
|
|
|
eAtAfterKid,
|
|
|
|
eAtEnd
|
2014-07-16 18:41:57 +00:00
|
|
|
};
|
2016-03-05 21:11:34 +00:00
|
|
|
IteratorPhase Phase() const { return mPhase; }
|
2014-07-16 18:41:57 +00:00
|
|
|
|
2016-03-05 21:11:34 +00:00
|
|
|
private:
|
2016-08-24 01:19:10 +00:00
|
|
|
// Helpers.
|
|
|
|
void AppendNativeAnonymousChildren();
|
|
|
|
void AppendNativeAnonymousChildrenFromFrame(nsIFrame* aFrame);
|
|
|
|
|
2016-10-18 04:29:03 +00:00
|
|
|
const nsIContent* mOriginalContent;
|
2016-03-05 21:11:21 +00:00
|
|
|
|
|
|
|
// mAnonKids is an array of native anonymous children, mAnonKidsIdx is index
|
|
|
|
// in the array. If mAnonKidsIdx < mAnonKids.Length() and mPhase is
|
|
|
|
// eAtAnonKids then the iterator points at a child at mAnonKidsIdx index. If
|
|
|
|
// mAnonKidsIdx == mAnonKids.Length() then the iterator is somewhere after
|
|
|
|
// the last native anon child. If mAnonKidsIdx == UINT32_MAX then the iterator
|
|
|
|
// is somewhere before the first native anon child.
|
2014-07-16 18:41:57 +00:00
|
|
|
nsTArray<nsIContent*> mAnonKids;
|
2016-03-05 21:11:21 +00:00
|
|
|
uint32_t mAnonKidsIdx;
|
|
|
|
|
2014-07-16 18:41:57 +00:00
|
|
|
uint32_t mFlags;
|
|
|
|
IteratorPhase mPhase;
|
|
|
|
#ifdef DEBUG
|
|
|
|
// XXX we should really assert there are no frame tree changes as well, but
|
|
|
|
// there's no easy way to do that.
|
|
|
|
nsMutationGuard mMutationGuard;
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2016-08-10 23:56:33 +00:00
|
|
|
/**
|
|
|
|
* StyleChildrenIterator traverses the children of the element from the
|
|
|
|
* perspective of the style system, particularly the children we need to traverse
|
|
|
|
* during restyle. This is identical to AllChildrenIterator with eAllChildren,
|
|
|
|
* _except_ that we detect and skip any native anonymous children that are used
|
|
|
|
* to implement pseudo-elements (since the style system needs to cascade those
|
|
|
|
* using different algorithms).
|
|
|
|
*
|
|
|
|
* Note: it assumes that no mutation of the DOM or frame tree takes place during
|
|
|
|
* iteration, and will break horribly if that is not true.
|
|
|
|
*/
|
|
|
|
class StyleChildrenIterator : private AllChildrenIterator {
|
|
|
|
public:
|
2016-10-18 04:29:03 +00:00
|
|
|
explicit StyleChildrenIterator(const nsIContent* aContent)
|
2016-08-10 23:56:33 +00:00
|
|
|
: AllChildrenIterator(aContent, nsIContent::eAllChildren)
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(StyleChildrenIterator);
|
|
|
|
}
|
|
|
|
~StyleChildrenIterator() { MOZ_COUNT_DTOR(StyleChildrenIterator); }
|
|
|
|
|
|
|
|
nsIContent* GetNextChild();
|
|
|
|
|
|
|
|
// Returns true if we cannot find all the children we need to style by
|
|
|
|
// traversing the siblings of the first child.
|
2016-10-18 04:29:03 +00:00
|
|
|
static bool IsNeeded(const Element* aParent);
|
2016-08-10 23:56:33 +00:00
|
|
|
};
|
|
|
|
|
2013-05-01 22:50:08 +00:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
2013-11-15 02:46:28 +00:00
|
|
|
|
|
|
|
#endif
|