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"
|
2018-07-03 09:33:09 +03:00
|
|
|
#include "nsIContentInlines.h"
|
|
|
|
#include <stdint.h>
|
2014-07-16 22:41:57 +04:00
|
|
|
|
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
|
|
|
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
|
2013-12-21 10:43:58 +04:00
|
|
|
// purposes 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
|
|
|
|
|
|
|
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
|
2013-12-02 14:26:12 +04:00
|
|
|
// found. 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
|
|
|
|
2020-01-08 18:08:06 +03:00
|
|
|
bool ShadowDOMInvolved() { return mShadowDOMInvolved; }
|
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:
|
|
|
|
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-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.
|
2020-01-08 18:08:06 +03:00
|
|
|
bool mShadowDOMInvolved = false;
|
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:
|
2018-07-03 09:33:09 +03:00
|
|
|
AllChildrenIterator(const nsIContent* aNode, uint32_t aFlags,
|
|
|
|
bool aStartAtBeginning = true)
|
|
|
|
: FlattenedChildIterator(aNode, aFlags, aStartAtBeginning),
|
|
|
|
mAnonKidsIdx(aStartAtBeginning ? UINT32_MAX : 0),
|
|
|
|
mFlags(aFlags),
|
|
|
|
mPhase(aStartAtBeginning ? eAtBegin : eAtEnd) {}
|
2014-07-16 22:41:57 +04:00
|
|
|
|
2014-08-06 22:21:28 +04:00
|
|
|
#ifdef DEBUG
|
2020-02-21 13:41:47 +03:00
|
|
|
AllChildrenIterator(AllChildrenIterator&&) = default;
|
2018-07-03 09:33:09 +03:00
|
|
|
|
|
|
|
AllChildrenIterator& operator=(AllChildrenIterator&& aOther) {
|
2020-02-21 13:41:47 +03:00
|
|
|
// Explicitly call the destructor to ensure the assertion in the destructor
|
|
|
|
// is checked.
|
2018-07-03 09:33:09 +03:00
|
|
|
this->~AllChildrenIterator();
|
|
|
|
new (this) AllChildrenIterator(std::move(aOther));
|
|
|
|
return *this;
|
|
|
|
}
|
2014-08-06 22:21:28 +04:00
|
|
|
|
2014-07-16 22:41:57 +04:00
|
|
|
~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,
|
2019-03-25 01:13:53 +03:00
|
|
|
eAtMarkerKid,
|
2016-03-06 00:11:21 +03:00
|
|
|
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-08-11 02:56:33 +03:00
|
|
|
public:
|
2018-07-03 09:33:09 +03:00
|
|
|
static nsIContent* GetParent(const nsIContent& aContent) {
|
|
|
|
nsINode* node = aContent.GetFlattenedTreeParentNodeForStyle();
|
|
|
|
return node && node->IsContent() ? node->AsContent() : nullptr;
|
|
|
|
}
|
|
|
|
|
2018-07-06 08:32:05 +03:00
|
|
|
explicit StyleChildrenIterator(const nsIContent* aContent,
|
|
|
|
bool aStartAtBeginning = true)
|
2016-12-01 06:34:57 +03:00
|
|
|
: AllChildrenIterator(
|
|
|
|
aContent,
|
|
|
|
nsIContent::eAllChildren |
|
2018-07-03 09:33:09 +03:00
|
|
|
nsIContent::eSkipDocumentLevelNativeAnonymousContent,
|
|
|
|
aStartAtBeginning) {
|
|
|
|
MOZ_COUNT_CTOR(StyleChildrenIterator);
|
|
|
|
}
|
|
|
|
|
|
|
|
StyleChildrenIterator(StyleChildrenIterator&& aOther)
|
|
|
|
: AllChildrenIterator(std::move(aOther)) {
|
2016-08-11 02:56:33 +03:00
|
|
|
MOZ_COUNT_CTOR(StyleChildrenIterator);
|
|
|
|
}
|
2018-07-03 09:33:09 +03:00
|
|
|
|
2020-02-21 13:41:47 +03:00
|
|
|
StyleChildrenIterator& operator=(StyleChildrenIterator&& aOther) = default;
|
2018-07-03 09:33:09 +03:00
|
|
|
|
2020-02-20 14:40:14 +03:00
|
|
|
MOZ_COUNTED_DTOR(StyleChildrenIterator)
|
2016-08-11 02:56:33 +03:00
|
|
|
|
2018-07-03 09:33:09 +03:00
|
|
|
using AllChildrenIterator::GetNextChild;
|
|
|
|
using AllChildrenIterator::GetPreviousChild;
|
|
|
|
using AllChildrenIterator::Seek;
|
2016-08-11 02:56:33 +03:00
|
|
|
};
|
|
|
|
|
2013-05-02 02:50:08 +04:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
2013-11-15 06:46:28 +04:00
|
|
|
|
|
|
|
#endif
|