2015-05-03 22:32:37 +03: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-02 02:50:08 +04: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 06:46:28 +04:00
|
|
|
#ifndef ChildIterator_h
|
|
|
|
#define ChildIterator_h
|
|
|
|
|
2014-07-16 22:41:57 +04:00
|
|
|
#include "nsIContent.h"
|
|
|
|
|
2013-05-02 02:50:08 +04: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 18:44:50 +04:00
|
|
|
#include <stdint.h>
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
|
|
|
|
class nsIContent;
|
2013-05-02 02:50:08 +04: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 10:43:58 +04: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-02 02:50:08 +04:00
|
|
|
class ExplicitChildIterator
|
|
|
|
{
|
|
|
|
public:
|
2016-10-18 07:29:03 +03:00
|
|
|
explicit ExplicitChildIterator(const nsIContent* aParent,
|
2017-12-04 11:06:37 +03:00
|
|
|
bool aStartAtBeginning = true);
|
2013-05-02 02:50:08 +04:00
|
|
|
|
2014-08-06 22:21:28 +04:00
|
|
|
ExplicitChildIterator(const ExplicitChildIterator& aOther)
|
2017-12-04 11:06:37 +03:00
|
|
|
: mParent(aOther.mParent),
|
|
|
|
mParentAsSlot(aOther.mParentAsSlot),
|
|
|
|
mChild(aOther.mChild),
|
2014-08-06 22:21:28 +04:00
|
|
|
mDefaultChild(aOther.mDefaultChild),
|
2017-06-30 08:39:31 +03:00
|
|
|
mIsFirst(aOther.mIsFirst),
|
|
|
|
mIndexInInserted(aOther.mIndexInInserted) {}
|
2014-08-06 22:21:28 +04:00
|
|
|
|
|
|
|
ExplicitChildIterator(ExplicitChildIterator&& aOther)
|
2017-12-04 11:06:37 +03:00
|
|
|
: mParent(aOther.mParent),
|
|
|
|
mParentAsSlot(aOther.mParentAsSlot),
|
|
|
|
mChild(aOther.mChild),
|
2014-08-06 22:21:28 +04:00
|
|
|
mDefaultChild(aOther.mDefaultChild),
|
2017-06-30 08:39:31 +03:00
|
|
|
mIsFirst(aOther.mIsFirst),
|
|
|
|
mIndexInInserted(aOther.mIndexInInserted) {}
|
2014-08-06 22:21:28 +04:00
|
|
|
|
2013-05-02 02:50:08 +04:00
|
|
|
nsIContent* GetNextChild();
|
|
|
|
|
2015-09-09 04:23:55 +03: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).
|
2017-11-01 20:56:13 +03:00
|
|
|
bool Seek(const nsIContent* aChildToFind);
|
2015-09-09 04:23:55 +03:00
|
|
|
|
|
|
|
// Looks for aChildToFind respecting insertion points until aChildToFind is found.
|
2013-12-02 14:26:12 +04: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.
|
2017-11-01 20:56:13 +03:00
|
|
|
bool Seek(const nsIContent* aChildToFind, nsIContent* aBound)
|
2013-12-02 14:26:12 +04: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 04:23:55 +03:00
|
|
|
// We can't easily take shortcuts here because we'd have to have a way to
|
|
|
|
// compare aChildToFind to aBound.
|
2013-12-02 14:26:12 +04:00
|
|
|
nsIContent* child;
|
|
|
|
do {
|
|
|
|
child = GetNextChild();
|
|
|
|
} while (child && child != aChildToFind && child != aBound);
|
|
|
|
|
|
|
|
return child == aChildToFind;
|
|
|
|
}
|
|
|
|
|
2013-12-21 10:43:58 +04: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 23:54:46 +03:00
|
|
|
nsIContent* Get() const;
|
2013-12-21 10:43:58 +04:00
|
|
|
|
|
|
|
// The inverse of GetNextChild. Properly steps in and out of insertion
|
|
|
|
// points.
|
|
|
|
nsIContent* GetPreviousChild();
|
|
|
|
|
2013-05-02 02:50:08 +04: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 07:29:03 +03:00
|
|
|
const nsIContent* mParent;
|
2013-05-02 02:50:08 +04:00
|
|
|
|
2017-12-04 11:06:37 +03:00
|
|
|
// If parent is a slot element, this points to the parent as HTMLSlotElement,
|
|
|
|
// otherwise, it's null.
|
|
|
|
const HTMLSlotElement* mParentAsSlot;
|
|
|
|
|
2013-12-21 10:43:58 +04:00
|
|
|
// The current child. When we encounter an insertion point,
|
2013-05-02 02:50:08 +04: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 10:43:58 +04:00
|
|
|
// nsXBLChildrenElement or HTMLContentElement). Once this transitions back
|
|
|
|
// to null, we continue iterating at mChild's next sibling.
|
2013-05-02 02:50:08 +04:00
|
|
|
nsIContent* mDefaultChild;
|
|
|
|
|
2017-06-30 08:39:31 +03:00
|
|
|
// A flag to let us know that we haven't started iterating yet.
|
|
|
|
bool mIsFirst;
|
|
|
|
|
2013-05-02 02:50:08 +04: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;
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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 17:05:16 +04: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-02 02:50:08 +04:00
|
|
|
class FlattenedChildIterator : public ExplicitChildIterator
|
|
|
|
{
|
|
|
|
public:
|
2016-10-18 07:29:03 +03:00
|
|
|
explicit FlattenedChildIterator(const nsIContent* aParent,
|
|
|
|
bool aStartAtBeginning = true)
|
2017-11-01 20:56:13 +03:00
|
|
|
: ExplicitChildIterator(aParent, aStartAtBeginning)
|
|
|
|
, mOriginalContent(aParent)
|
2014-07-16 22:41:57 +04:00
|
|
|
{
|
|
|
|
Init(false);
|
|
|
|
}
|
2013-05-02 02:50:08 +04:00
|
|
|
|
2014-08-06 22:21:28 +04:00
|
|
|
FlattenedChildIterator(FlattenedChildIterator&& aOther)
|
2017-11-01 20:56:13 +03:00
|
|
|
: ExplicitChildIterator(Move(aOther))
|
2018-02-28 13:25:31 +03:00
|
|
|
, mOriginalContent(aOther.mOriginalContent)
|
2018-02-25 20:55:29 +03:00
|
|
|
, mXBLInvolved(aOther.mXBLInvolved)
|
2017-11-01 20:56:13 +03:00
|
|
|
{}
|
2014-08-06 22:21:28 +04:00
|
|
|
|
|
|
|
FlattenedChildIterator(const FlattenedChildIterator& aOther)
|
2017-11-01 20:56:13 +03:00
|
|
|
: ExplicitChildIterator(aOther)
|
2018-02-28 13:25:31 +03:00
|
|
|
, mOriginalContent(aOther.mOriginalContent)
|
2018-02-25 20:55:29 +03:00
|
|
|
, mXBLInvolved(aOther.mXBLInvolved)
|
2017-11-01 20:56:13 +03:00
|
|
|
{}
|
2014-08-06 22:21:28 +04:00
|
|
|
|
2018-02-25 20:55:29 +03:00
|
|
|
bool XBLInvolved() {
|
|
|
|
if (mXBLInvolved.isNothing()) {
|
|
|
|
mXBLInvolved = Some(ComputeWhetherXBLIsInvolved());
|
|
|
|
}
|
|
|
|
return *mXBLInvolved;
|
|
|
|
}
|
2013-05-02 02:50:08 +04:00
|
|
|
|
2017-11-01 20:56:13 +03:00
|
|
|
const nsIContent* Parent() const { return mOriginalContent; }
|
|
|
|
|
2018-02-25 20:55:29 +03:00
|
|
|
private:
|
|
|
|
bool ComputeWhetherXBLIsInvolved() const;
|
|
|
|
|
|
|
|
void Init(bool aIgnoreXBL);
|
|
|
|
|
2014-07-16 22:41:57 +04:00
|
|
|
protected:
|
|
|
|
/**
|
|
|
|
* This constructor is a hack to help AllChildrenIterator which sometimes
|
|
|
|
* doesn't want to consider XBL.
|
|
|
|
*/
|
2016-10-18 07:29:03 +03:00
|
|
|
FlattenedChildIterator(const nsIContent* aParent, uint32_t aFlags,
|
|
|
|
bool aStartAtBeginning = true)
|
2017-11-01 20:56:13 +03:00
|
|
|
: ExplicitChildIterator(aParent, aStartAtBeginning)
|
|
|
|
, mOriginalContent(aParent)
|
2014-07-16 22:41:57 +04:00
|
|
|
{
|
2014-10-02 17:05:16 +04:00
|
|
|
bool ignoreXBL = aFlags & nsIContent::eAllButXBL;
|
|
|
|
Init(ignoreXBL);
|
2014-07-16 22:41:57 +04:00
|
|
|
}
|
|
|
|
|
2018-02-28 13:25:31 +03:00
|
|
|
const nsIContent* mOriginalContent;
|
2018-02-25 20:55:29 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
// For certain optimizations, nsCSSFrameConstructor needs to know if the child
|
|
|
|
// list of the element that we're iterating matches its .childNodes.
|
|
|
|
//
|
|
|
|
// This is lazily computed when asked for it.
|
|
|
|
Maybe<bool> mXBLInvolved;
|
2013-05-02 02:50:08 +04:00
|
|
|
};
|
|
|
|
|
2014-07-16 22:41:57 +04:00
|
|
|
/**
|
2016-03-08 23:54:46 +03: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 22:41:57 +04:00
|
|
|
*/
|
|
|
|
class AllChildrenIterator : private FlattenedChildIterator
|
|
|
|
{
|
|
|
|
public:
|
2016-10-18 07:29:03 +03:00
|
|
|
AllChildrenIterator(const nsIContent* aNode, uint32_t aFlags,
|
|
|
|
bool aStartAtBeginning = true) :
|
2014-10-02 17:05:16 +04:00
|
|
|
FlattenedChildIterator(aNode, aFlags, aStartAtBeginning),
|
2017-11-01 20:56:13 +03:00
|
|
|
mAnonKidsIdx(aStartAtBeginning ? UINT32_MAX : 0),
|
2016-03-06 00:11:21 +03:00
|
|
|
mFlags(aFlags), mPhase(aStartAtBeginning ? eAtBegin : eAtEnd) { }
|
2014-07-16 22:41:57 +04:00
|
|
|
|
2014-08-06 22:21:28 +04:00
|
|
|
AllChildrenIterator(AllChildrenIterator&& aOther)
|
|
|
|
: FlattenedChildIterator(Move(aOther)),
|
2016-03-06 00:11:21 +03:00
|
|
|
mAnonKids(Move(aOther.mAnonKids)), mAnonKidsIdx(aOther.mAnonKidsIdx),
|
|
|
|
mFlags(aOther.mFlags), mPhase(aOther.mPhase)
|
2014-08-06 22:21:28 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
, mMutationGuard(aOther.mMutationGuard)
|
|
|
|
#endif
|
|
|
|
{}
|
|
|
|
|
2014-07-16 22:41:57 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
~AllChildrenIterator() { MOZ_ASSERT(!mMutationGuard.Mutated(0)); }
|
|
|
|
#endif
|
|
|
|
|
2016-03-08 23:54:46 +03: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.
|
2017-11-01 20:56:13 +03:00
|
|
|
bool Seek(const nsIContent* aChildToFind);
|
2016-02-11 16:22:39 +03:00
|
|
|
|
2014-07-16 22:41:57 +04:00
|
|
|
nsIContent* GetNextChild();
|
2016-03-06 00:11:21 +03:00
|
|
|
nsIContent* GetPreviousChild();
|
2014-07-16 22:41:57 +04:00
|
|
|
|
|
|
|
enum IteratorPhase
|
|
|
|
{
|
2016-03-06 00:11:21 +03:00
|
|
|
eAtBegin,
|
|
|
|
eAtBeforeKid,
|
|
|
|
eAtExplicitKids,
|
|
|
|
eAtAnonKids,
|
|
|
|
eAtAfterKid,
|
|
|
|
eAtEnd
|
2014-07-16 22:41:57 +04:00
|
|
|
};
|
2016-03-06 00:11:34 +03:00
|
|
|
IteratorPhase Phase() const { return mPhase; }
|
2014-07-16 22:41:57 +04:00
|
|
|
|
2016-03-06 00:11:34 +03:00
|
|
|
private:
|
2016-08-24 04:19:10 +03:00
|
|
|
// Helpers.
|
|
|
|
void AppendNativeAnonymousChildren();
|
2016-03-06 00:11:21 +03: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 22:41:57 +04:00
|
|
|
nsTArray<nsIContent*> mAnonKids;
|
2016-03-06 00:11:21 +03:00
|
|
|
uint32_t mAnonKidsIdx;
|
|
|
|
|
2014-07-16 22:41:57 +04: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-11 02:56:33 +03:00
|
|
|
/**
|
|
|
|
* StyleChildrenIterator traverses the children of the element from the
|
2017-04-22 23:33:16 +03:00
|
|
|
* perspective of the style system, particularly the children we need to
|
|
|
|
* traverse during restyle.
|
|
|
|
*
|
|
|
|
* At present, this is identical to AllChildrenIterator with
|
|
|
|
* (eAllChildren | eSkipDocumentLevelNativeAnonymousContent). We used to have
|
|
|
|
* detect and skip any native anonymous children that are used to implement some
|
|
|
|
* special magic in here that went away, but we keep the separate class so
|
|
|
|
* we can reintroduce special magic back if needed.
|
2016-08-11 02:56:33 +03:00
|
|
|
*
|
|
|
|
* 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.
|
2017-06-28 09:56:13 +03:00
|
|
|
*
|
|
|
|
* We require this to be memmovable since Rust code can create and move
|
|
|
|
* StyleChildrenIterators.
|
2016-08-11 02:56:33 +03:00
|
|
|
*/
|
2017-06-28 09:56:13 +03:00
|
|
|
class MOZ_NEEDS_MEMMOVABLE_MEMBERS StyleChildrenIterator : private AllChildrenIterator
|
2016-12-01 06:34:57 +03:00
|
|
|
{
|
2016-08-11 02:56:33 +03:00
|
|
|
public:
|
2016-10-18 07:29:03 +03:00
|
|
|
explicit StyleChildrenIterator(const nsIContent* aContent)
|
2016-12-01 06:34:57 +03:00
|
|
|
: AllChildrenIterator(aContent,
|
|
|
|
nsIContent::eAllChildren |
|
|
|
|
nsIContent::eSkipDocumentLevelNativeAnonymousContent)
|
2016-08-11 02:56:33 +03:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(StyleChildrenIterator);
|
|
|
|
}
|
|
|
|
~StyleChildrenIterator() { MOZ_COUNT_DTOR(StyleChildrenIterator); }
|
|
|
|
|
|
|
|
nsIContent* GetNextChild();
|
|
|
|
};
|
|
|
|
|
2013-05-02 02:50:08 +04:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
2013-11-15 06:46:28 +04:00
|
|
|
|
|
|
|
#endif
|