2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
// vim:cindent:ts=2:et:sw=2:
|
2012-05-21 04:12:37 -07: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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* used by nsCSSFrameConstructor to determine and iterate the child list
|
|
|
|
* used to construct frames (normal children or something from XBL)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsIContent.h"
|
2009-01-29 11:46:20 -08:00
|
|
|
#include "nsINodeList.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper class for iterating children during frame construction.
|
|
|
|
* This class should always be used in lieu of the straight content
|
|
|
|
* node APIs, since it handles XBL-generated anonymous content as
|
|
|
|
* well.
|
|
|
|
*/
|
2008-07-01 12:16:45 -07:00
|
|
|
class NS_STACK_CLASS ChildIterator
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
protected:
|
2009-02-19 08:55:09 -08:00
|
|
|
nsIContent* mContent;
|
2010-07-14 21:38:21 -07:00
|
|
|
// If mNodes is non-null (so XBLInvolved() is true), mIndex is the
|
|
|
|
// index into mNodes for our current position. Otherwise, mChild is
|
|
|
|
// our current child (which might be null if we're done iterating).
|
|
|
|
union {
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t mIndex;
|
2010-07-14 21:38:21 -07:00
|
|
|
nsIContent* mChild;
|
|
|
|
};
|
2009-02-19 08:55:09 -08:00
|
|
|
nsINodeList* mNodes;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
public:
|
|
|
|
ChildIterator()
|
2012-07-30 07:20:58 -07:00
|
|
|
: mContent(nullptr), mChild(0), mNodes(nullptr) {}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
ChildIterator(const ChildIterator& aOther)
|
|
|
|
: mContent(aOther.mContent),
|
2010-07-14 21:38:21 -07:00
|
|
|
mNodes(aOther.mNodes) {
|
|
|
|
if (XBLInvolved()) {
|
|
|
|
mIndex = aOther.mIndex;
|
|
|
|
} else {
|
|
|
|
mChild = aOther.mChild;
|
|
|
|
}
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
ChildIterator& operator=(const ChildIterator& aOther) {
|
|
|
|
mContent = aOther.mContent;
|
|
|
|
mNodes = aOther.mNodes;
|
2010-07-14 21:38:21 -07:00
|
|
|
if (XBLInvolved()) {
|
|
|
|
mIndex = aOther.mIndex;
|
|
|
|
} else {
|
|
|
|
mChild = aOther.mChild;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
ChildIterator& operator++() {
|
2010-07-14 21:38:21 -07:00
|
|
|
if (XBLInvolved()) {
|
|
|
|
++mIndex;
|
|
|
|
} else {
|
|
|
|
NS_ASSERTION(mChild, "Walking off end of list?");
|
|
|
|
mChild = mChild->GetNextSibling();
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
ChildIterator operator++(int) {
|
|
|
|
ChildIterator result(*this);
|
2010-07-14 21:38:21 -07:00
|
|
|
++(*this);
|
2007-03-22 10:30:00 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
ChildIterator& operator--() {
|
2010-07-14 21:38:21 -07:00
|
|
|
if (XBLInvolved()) {
|
|
|
|
--mIndex;
|
|
|
|
} else if (mChild) {
|
|
|
|
mChild = mChild->GetPreviousSibling();
|
|
|
|
NS_ASSERTION(mChild, "Walking off beginning of list");
|
|
|
|
} else {
|
|
|
|
mChild = mContent->GetLastChild();
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
ChildIterator operator--(int) {
|
|
|
|
ChildIterator result(*this);
|
2010-07-14 21:38:21 -07:00
|
|
|
--(*this);
|
2007-03-22 10:30:00 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-01-29 11:46:20 -08:00
|
|
|
nsIContent* get() const {
|
|
|
|
if (XBLInvolved()) {
|
|
|
|
return mNodes->GetNodeAt(mIndex);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-07-14 21:38:21 -07:00
|
|
|
return mChild;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-01-29 11:46:20 -08:00
|
|
|
nsIContent* operator*() const { return get(); }
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool operator==(const ChildIterator& aOther) const {
|
2010-07-14 21:38:21 -07:00
|
|
|
if (XBLInvolved()) {
|
|
|
|
return mContent == aOther.mContent && mIndex == aOther.mIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mContent == aOther.mContent && mChild == aOther.mChild;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool operator!=(const ChildIterator& aOther) const {
|
2007-03-22 10:30:00 -07:00
|
|
|
return !aOther.operator==(*this);
|
|
|
|
}
|
|
|
|
|
2009-01-29 11:46:20 -08:00
|
|
|
void seek(nsIContent* aContent) {
|
|
|
|
if (XBLInvolved()) {
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t index = mNodes->IndexOf(aContent);
|
2010-07-14 21:38:21 -07:00
|
|
|
// XXXbz I wish we could assert that index != -1, but it seems to not be
|
|
|
|
// the case in some XBL cases with filtered insertion points and no
|
|
|
|
// default insertion point. I will now claim that XBL's management of
|
|
|
|
// its insertion points is broken in those cases, since it's returning an
|
|
|
|
// insertion parent for a node that doesn't actually have the node in its
|
|
|
|
// child list according to ChildIterator. See bug 474324.
|
|
|
|
if (index != -1) {
|
|
|
|
mIndex = index;
|
|
|
|
} else {
|
|
|
|
// If aContent isn't going to get hit by this iterator, just seek to the
|
|
|
|
// end of the list for lack of anything better to do.
|
|
|
|
mIndex = length();
|
|
|
|
}
|
|
|
|
} else if (aContent->GetParent() == mContent) {
|
|
|
|
mChild = aContent;
|
2009-01-29 11:46:20 -08:00
|
|
|
} else {
|
2010-07-14 21:38:21 -07:00
|
|
|
// XXXbz I wish we could assert this doesn't happen, but I think that's
|
|
|
|
// not necessarily the case when called from ContentInserted if
|
|
|
|
// first-letter frames are about.
|
2012-07-30 07:20:58 -07:00
|
|
|
mChild = nullptr;
|
2009-01-29 11:46:20 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
bool XBLInvolved() const { return mNodes != nullptr; }
|
2009-01-29 11:46:20 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/**
|
|
|
|
* Create a pair of ChildIterators for a content node. aFirst will
|
|
|
|
* point to the first child of aContent; aLast will point one past
|
|
|
|
* the last child of aContent.
|
|
|
|
*/
|
|
|
|
static nsresult Init(nsIContent* aContent,
|
|
|
|
ChildIterator* aFirst,
|
|
|
|
ChildIterator* aLast);
|
2009-01-29 11:46:20 -08:00
|
|
|
|
|
|
|
private:
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t length() {
|
2010-07-14 21:38:21 -07:00
|
|
|
NS_PRECONDITION(XBLInvolved(), "Don't call me");
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t l;
|
2010-07-14 21:38:21 -07:00
|
|
|
mNodes->GetLength(&l);
|
2009-01-29 11:46:20 -08:00
|
|
|
return l;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|