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: */
|
2012-05-21 15:12:37 +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/. */
|
1998-12-15 02:16:31 +03:00
|
|
|
|
2016-06-23 12:58:22 +03:00
|
|
|
#include "mozilla/DebugOnly.h"
|
1999-02-12 08:28:12 +03:00
|
|
|
#include "nsISupports.h"
|
1999-07-03 10:19:11 +04:00
|
|
|
#include "nsIDOMNodeList.h"
|
1999-02-12 08:28:12 +03:00
|
|
|
#include "nsIContentIterator.h"
|
1999-02-16 18:48:13 +03:00
|
|
|
#include "nsRange.h"
|
1998-12-15 02:16:31 +03:00
|
|
|
#include "nsIContent.h"
|
1999-02-12 08:28:12 +03:00
|
|
|
#include "nsCOMPtr.h"
|
2009-03-20 11:15:35 +03:00
|
|
|
#include "nsTArray.h"
|
2002-06-26 00:03:06 +04:00
|
|
|
#include "nsContentUtils.h"
|
2006-10-21 05:30:54 +04:00
|
|
|
#include "nsINode.h"
|
2010-06-02 22:33:47 +04:00
|
|
|
#include "nsCycleCollectionParticipant.h"
|
1998-12-15 02:16:31 +03:00
|
|
|
|
1999-07-03 10:19:11 +04:00
|
|
|
// couple of utility static functs
|
|
|
|
|
2002-11-21 18:07:49 +03:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2008-10-15 13:40:28 +04:00
|
|
|
// NodeToParentOffset: returns the node's parent and offset.
|
2002-11-21 18:07:49 +03:00
|
|
|
//
|
2004-01-24 03:46:17 +03:00
|
|
|
|
2008-10-15 13:40:28 +04:00
|
|
|
static nsINode*
|
2012-08-22 19:56:38 +04:00
|
|
|
NodeToParentOffset(nsINode* aNode, int32_t* aOffset)
|
2002-11-21 18:07:49 +03:00
|
|
|
{
|
2012-06-14 10:47:03 +04:00
|
|
|
*aOffset = 0;
|
2002-11-21 18:07:49 +03:00
|
|
|
|
2012-10-09 16:31:24 +04:00
|
|
|
nsINode* parent = aNode->GetParentNode();
|
2002-11-21 18:07:49 +03:00
|
|
|
|
2008-03-17 07:56:22 +03:00
|
|
|
if (parent) {
|
2008-10-15 13:40:28 +04:00
|
|
|
*aOffset = parent->IndexOf(aNode);
|
2015-11-10 05:49:04 +03:00
|
|
|
NS_WARN_IF(*aOffset < 0);
|
2008-03-17 07:56:22 +03:00
|
|
|
}
|
2012-06-14 10:47:03 +04:00
|
|
|
|
2008-03-17 07:56:22 +03:00
|
|
|
return parent;
|
2002-11-21 18:07:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2008-10-15 13:40:28 +04:00
|
|
|
// NodeIsInTraversalRange: returns true if content is visited during
|
2002-11-21 18:07:49 +03:00
|
|
|
// the traversal of the range in the specified mode.
|
|
|
|
//
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool
|
2012-06-14 10:47:03 +04:00
|
|
|
NodeIsInTraversalRange(nsINode* aNode, bool aIsPreMode,
|
2012-08-22 19:56:38 +04:00
|
|
|
nsINode* aStartNode, int32_t aStartOffset,
|
|
|
|
nsINode* aEndNode, int32_t aEndOffset)
|
2002-11-21 18:07:49 +03:00
|
|
|
{
|
2015-11-10 05:49:04 +03:00
|
|
|
if (NS_WARN_IF(!aStartNode) || NS_WARN_IF(!aEndNode) || NS_WARN_IF(!aNode)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2012-06-14 10:47:03 +04:00
|
|
|
}
|
2002-11-21 18:07:49 +03:00
|
|
|
|
2015-10-20 19:45:03 +03:00
|
|
|
// If a leaf node contains an end point of the traversal range, it is
|
2012-06-14 10:47:03 +04:00
|
|
|
// always in the traversal range.
|
2015-10-20 19:45:03 +03:00
|
|
|
if (aNode == aStartNode || aNode == aEndNode) {
|
|
|
|
if (aNode->IsNodeOfType(nsINode::eDATA_NODE)) {
|
|
|
|
return true; // text node or something
|
|
|
|
}
|
|
|
|
if (!aNode->HasChildren()) {
|
|
|
|
MOZ_ASSERT(aNode != aStartNode || !aStartOffset,
|
|
|
|
"aStartNode doesn't have children and not a data node, "
|
|
|
|
"aStartOffset should be 0");
|
|
|
|
MOZ_ASSERT(aNode != aEndNode || !aEndOffset,
|
|
|
|
"aStartNode doesn't have children and not a data node, "
|
|
|
|
"aStartOffset should be 0");
|
|
|
|
return true;
|
|
|
|
}
|
2002-12-19 22:36:18 +03:00
|
|
|
}
|
|
|
|
|
2012-10-09 16:31:24 +04:00
|
|
|
nsINode* parent = aNode->GetParentNode();
|
2012-06-14 10:47:03 +04:00
|
|
|
if (!parent) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2012-06-14 10:47:03 +04:00
|
|
|
}
|
2002-11-21 18:07:49 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t indx = parent->IndexOf(aNode);
|
2015-11-10 05:49:04 +03:00
|
|
|
NS_WARN_IF(indx == -1);
|
2006-10-21 05:30:54 +04:00
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
if (!aIsPreMode) {
|
2002-11-21 18:07:49 +03:00
|
|
|
++indx;
|
2012-06-14 10:47:03 +04:00
|
|
|
}
|
2002-11-21 18:07:49 +03:00
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
return nsContentUtils::ComparePoints(aStartNode, aStartOffset,
|
|
|
|
parent, indx) <= 0 &&
|
|
|
|
nsContentUtils::ComparePoints(aEndNode, aEndOffset,
|
|
|
|
parent, indx) >= 0;
|
2002-11-21 18:07:49 +03:00
|
|
|
}
|
1999-07-03 10:19:11 +04:00
|
|
|
|
|
|
|
|
1999-02-12 08:28:12 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* A simple iterator class for traversing the content in "close tag" order
|
|
|
|
*/
|
2012-06-14 10:47:03 +04:00
|
|
|
class nsContentIterator : public nsIContentIterator
|
1998-12-15 02:16:31 +03:00
|
|
|
{
|
1999-02-12 08:28:12 +03:00
|
|
|
public:
|
2010-06-02 22:33:47 +04:00
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
|
|
|
NS_DECL_CYCLE_COLLECTION_CLASS(nsContentIterator)
|
1999-02-12 08:28:12 +03:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
explicit nsContentIterator(bool aPre);
|
1999-02-12 08:28:12 +03:00
|
|
|
|
|
|
|
// nsIContentIterator interface methods ------------------------------
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual nsresult Init(nsINode* aRoot) override;
|
1999-02-12 08:28:12 +03:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual nsresult Init(nsIDOMRange* aRange) override;
|
1999-02-12 08:28:12 +03:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual void First() override;
|
1999-02-12 08:28:12 +03:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual void Last() override;
|
2012-06-14 10:47:03 +04:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual void Next() override;
|
1999-02-12 08:28:12 +03:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual void Prev() override;
|
1999-02-12 08:28:12 +03:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual nsINode* GetCurrentNode() override;
|
1999-02-12 08:28:12 +03:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual bool IsDone() override;
|
1999-02-12 08:28:12 +03:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual nsresult PositionAt(nsINode* aCurNode) override;
|
1999-03-23 13:30:24 +03:00
|
|
|
|
1999-02-12 08:28:12 +03:00
|
|
|
protected:
|
2014-06-25 06:09:15 +04:00
|
|
|
virtual ~nsContentIterator();
|
1999-02-12 08:28:12 +03:00
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
// Recursively get the deepest first/last child of aRoot. This will return
|
|
|
|
// aRoot itself if it has no children.
|
2012-06-14 10:47:03 +04:00
|
|
|
nsINode* GetDeepFirstChild(nsINode* aRoot,
|
2012-08-22 19:56:38 +04:00
|
|
|
nsTArray<int32_t>* aIndexes = nullptr);
|
2012-06-14 10:47:03 +04:00
|
|
|
nsIContent* GetDeepFirstChild(nsIContent* aRoot,
|
2012-08-22 19:56:38 +04:00
|
|
|
nsTArray<int32_t>* aIndexes = nullptr);
|
2012-06-14 10:47:03 +04:00
|
|
|
nsINode* GetDeepLastChild(nsINode* aRoot,
|
2012-08-22 19:56:38 +04:00
|
|
|
nsTArray<int32_t>* aIndexes = nullptr);
|
2012-06-14 10:47:03 +04:00
|
|
|
nsIContent* GetDeepLastChild(nsIContent* aRoot,
|
2012-08-22 19:56:38 +04:00
|
|
|
nsTArray<int32_t>* aIndexes = nullptr);
|
2004-01-24 03:46:17 +03:00
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
// Get the next/previous sibling of aNode, or its parent's, or grandparent's,
|
|
|
|
// etc. Returns null if aNode and all its ancestors have no next/previous
|
|
|
|
// sibling.
|
|
|
|
nsIContent* GetNextSibling(nsINode* aNode,
|
2012-08-22 19:56:38 +04:00
|
|
|
nsTArray<int32_t>* aIndexes = nullptr);
|
2012-06-14 10:47:03 +04:00
|
|
|
nsIContent* GetPrevSibling(nsINode* aNode,
|
2012-08-22 19:56:38 +04:00
|
|
|
nsTArray<int32_t>* aIndexes = nullptr);
|
2004-01-24 03:46:17 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
nsINode* NextNode(nsINode* aNode, nsTArray<int32_t>* aIndexes = nullptr);
|
|
|
|
nsINode* PrevNode(nsINode* aNode, nsTArray<int32_t>* aIndexes = nullptr);
|
2001-09-24 10:13:03 +04:00
|
|
|
|
|
|
|
// WARNING: This function is expensive
|
|
|
|
nsresult RebuildIndexStack();
|
1999-02-12 08:28:12 +03:00
|
|
|
|
1999-03-01 11:17:18 +03:00
|
|
|
void MakeEmpty();
|
2012-06-14 10:47:03 +04:00
|
|
|
|
2013-07-09 21:30:58 +04:00
|
|
|
virtual void LastRelease();
|
|
|
|
|
2008-10-15 13:40:28 +04:00
|
|
|
nsCOMPtr<nsINode> mCurNode;
|
|
|
|
nsCOMPtr<nsINode> mFirst;
|
|
|
|
nsCOMPtr<nsINode> mLast;
|
2006-11-02 10:41:45 +03:00
|
|
|
nsCOMPtr<nsINode> mCommonParent;
|
1999-02-12 08:28:12 +03:00
|
|
|
|
2001-09-24 10:13:03 +04:00
|
|
|
// used by nsContentIterator to cache indices
|
2016-02-02 18:36:30 +03:00
|
|
|
AutoTArray<int32_t, 8> mIndexes;
|
2001-09-24 10:13:03 +04:00
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
// used by nsSubtreeIterator to cache indices. Why put them in the base
|
|
|
|
// class? Because otherwise I have to duplicate the routines GetNextSibling
|
|
|
|
// etc across both classes, with slight variations for caching. Or
|
|
|
|
// alternately, create a base class for the cache itself and have all the
|
|
|
|
// cache manipulation go through a vptr. I think this is the best space and
|
|
|
|
// speed combo, even though it's ugly.
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t mCachedIndex;
|
2012-06-14 10:47:03 +04:00
|
|
|
// another note about mCachedIndex: why should the subtree iterator use a
|
|
|
|
// trivial cached index instead of the mre robust array of indicies (which is
|
|
|
|
// what the basic content iterator uses)? The reason is that subtree
|
|
|
|
// iterators do not do much transitioning between parents and children. They
|
|
|
|
// tend to stay at the same level. In fact, you can prove (though I won't
|
|
|
|
// attempt it here) that they change levels at most n+m times, where n is the
|
|
|
|
// height of the parent hierarchy from the range start to the common
|
|
|
|
// ancestor, and m is the the height of the parent hierarchy from the range
|
|
|
|
// end to the common ancestor. If we used the index array, we would pay the
|
|
|
|
// price up front for n, and then pay the cost for m on the fly later on.
|
|
|
|
// With the simple cache, we only "pay as we go". Either way, we call
|
|
|
|
// IndexOf() once for each change of level in the hierarchy. Since a trivial
|
|
|
|
// index is much simpler, we use it for the subtree iterator.
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mIsDone;
|
|
|
|
bool mPre;
|
2012-06-14 10:47:03 +04:00
|
|
|
|
1999-02-12 08:28:12 +03:00
|
|
|
private:
|
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
// no copies or assigns FIX ME
|
1999-02-12 08:28:12 +03:00
|
|
|
nsContentIterator(const nsContentIterator&);
|
|
|
|
nsContentIterator& operator=(const nsContentIterator&);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
1999-02-14 12:15:13 +03:00
|
|
|
|
1999-02-12 08:28:12 +03:00
|
|
|
/******************************************************
|
|
|
|
* repository cruft
|
|
|
|
******************************************************/
|
|
|
|
|
2012-06-04 21:42:00 +04:00
|
|
|
already_AddRefed<nsIContentIterator>
|
|
|
|
NS_NewContentIterator()
|
1999-02-12 08:28:12 +03:00
|
|
|
{
|
2012-06-04 21:42:00 +04:00
|
|
|
nsCOMPtr<nsIContentIterator> iter = new nsContentIterator(false);
|
|
|
|
return iter.forget();
|
1998-12-15 02:16:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-04 21:42:00 +04:00
|
|
|
already_AddRefed<nsIContentIterator>
|
|
|
|
NS_NewPreContentIterator()
|
2002-11-21 18:07:49 +03:00
|
|
|
{
|
2012-06-04 21:42:00 +04:00
|
|
|
nsCOMPtr<nsIContentIterator> iter = new nsContentIterator(true);
|
|
|
|
return iter.forget();
|
2002-11-21 18:07:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-02-12 08:28:12 +03:00
|
|
|
/******************************************************
|
|
|
|
* XPCOM cruft
|
|
|
|
******************************************************/
|
2012-06-14 10:47:03 +04:00
|
|
|
|
2010-06-02 22:33:47 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsContentIterator)
|
2013-07-09 21:30:58 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE_WITH_LAST_RELEASE(nsContentIterator,
|
|
|
|
LastRelease())
|
2010-06-02 22:33:47 +04:00
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN(nsContentIterator)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIContentIterator)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIContentIterator)
|
|
|
|
NS_INTERFACE_MAP_ENTRIES_CYCLE_COLLECTION(nsContentIterator)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2014-04-25 20:49:00 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION(nsContentIterator,
|
|
|
|
mCurNode,
|
|
|
|
mFirst,
|
|
|
|
mLast,
|
|
|
|
mCommonParent)
|
1999-02-12 08:28:12 +03:00
|
|
|
|
2013-07-09 21:30:58 +04:00
|
|
|
void
|
|
|
|
nsContentIterator::LastRelease()
|
|
|
|
{
|
|
|
|
mCurNode = nullptr;
|
|
|
|
mFirst = nullptr;
|
|
|
|
mLast = nullptr;
|
|
|
|
mCommonParent = nullptr;
|
|
|
|
}
|
|
|
|
|
1999-02-12 08:28:12 +03:00
|
|
|
/******************************************************
|
|
|
|
* constructor/destructor
|
|
|
|
******************************************************/
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
nsContentIterator::nsContentIterator(bool aPre) :
|
2012-06-14 10:47:03 +04:00
|
|
|
// don't need to explicitly initialize |nsCOMPtr|s, they will automatically
|
2013-04-03 05:14:24 +04:00
|
|
|
// be nullptr
|
2011-10-17 18:59:28 +04:00
|
|
|
mCachedIndex(0), mIsDone(false), mPre(aPre)
|
1999-02-12 08:28:12 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsContentIterator::~nsContentIterator()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************
|
|
|
|
* Init routines
|
|
|
|
******************************************************/
|
|
|
|
|
|
|
|
|
2004-01-24 03:46:17 +03:00
|
|
|
nsresult
|
2008-10-15 13:40:28 +04:00
|
|
|
nsContentIterator::Init(nsINode* aRoot)
|
1999-02-12 08:28:12 +03:00
|
|
|
{
|
2015-11-10 05:49:04 +03:00
|
|
|
if (NS_WARN_IF(!aRoot)) {
|
2012-06-14 10:47:03 +04:00
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
2008-10-15 13:40:28 +04:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mIsDone = false;
|
2001-09-24 10:13:03 +04:00
|
|
|
mIndexes.Clear();
|
2012-06-14 10:47:03 +04:00
|
|
|
|
|
|
|
if (mPre) {
|
2004-01-24 03:46:17 +03:00
|
|
|
mFirst = aRoot;
|
2012-06-14 10:47:03 +04:00
|
|
|
mLast = GetDeepLastChild(aRoot);
|
2015-11-10 05:49:04 +03:00
|
|
|
NS_WARN_IF(!mLast);
|
2012-06-14 10:47:03 +04:00
|
|
|
} else {
|
2012-06-14 10:47:03 +04:00
|
|
|
mFirst = GetDeepFirstChild(aRoot);
|
2015-11-10 05:49:04 +03:00
|
|
|
NS_WARN_IF(!mFirst);
|
2004-01-24 03:46:17 +03:00
|
|
|
mLast = aRoot;
|
2002-11-21 18:07:49 +03:00
|
|
|
}
|
|
|
|
|
2004-01-24 03:46:17 +03:00
|
|
|
mCommonParent = aRoot;
|
1998-12-15 02:16:31 +03:00
|
|
|
mCurNode = mFirst;
|
2002-11-21 18:07:49 +03:00
|
|
|
RebuildIndexStack();
|
1999-02-12 08:28:12 +03:00
|
|
|
return NS_OK;
|
1998-12-15 02:16:31 +03:00
|
|
|
}
|
|
|
|
|
2004-01-24 03:46:17 +03:00
|
|
|
nsresult
|
2012-01-10 18:19:54 +04:00
|
|
|
nsContentIterator::Init(nsIDOMRange* aDOMRange)
|
1998-12-15 02:16:31 +03:00
|
|
|
{
|
2015-11-10 05:49:04 +03:00
|
|
|
if (NS_WARN_IF(!aDOMRange)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
2012-01-10 18:19:54 +04:00
|
|
|
nsRange* range = static_cast<nsRange*>(aDOMRange);
|
2004-01-24 03:46:17 +03:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mIsDone = false;
|
1999-10-09 03:34:07 +04:00
|
|
|
|
1999-03-01 11:17:18 +03:00
|
|
|
// get common content parent
|
2012-01-10 18:19:54 +04:00
|
|
|
mCommonParent = range->GetCommonAncestor();
|
2015-11-10 05:49:04 +03:00
|
|
|
if (NS_WARN_IF(!mCommonParent)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
1999-03-01 11:17:18 +03:00
|
|
|
|
2006-11-02 10:41:45 +03:00
|
|
|
// get the start node and offset
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t startIndx = range->StartOffset();
|
2015-11-10 05:49:04 +03:00
|
|
|
NS_WARN_IF(startIndx < 0);
|
2012-01-10 18:19:54 +04:00
|
|
|
nsINode* startNode = range->GetStartParent();
|
2015-11-10 05:49:04 +03:00
|
|
|
if (NS_WARN_IF(!startNode)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2006-10-21 05:30:54 +04:00
|
|
|
|
2006-11-02 10:41:45 +03:00
|
|
|
// get the end node and offset
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t endIndx = range->EndOffset();
|
2015-11-10 05:49:04 +03:00
|
|
|
NS_WARN_IF(endIndx < 0);
|
2012-01-10 18:19:54 +04:00
|
|
|
nsINode* endNode = range->GetEndParent();
|
2015-11-10 05:49:04 +03:00
|
|
|
if (NS_WARN_IF(!endNode)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
1999-05-18 15:11:14 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool startIsData = startNode->IsNodeOfType(nsINode::eDATA_NODE);
|
2002-11-21 18:07:49 +03:00
|
|
|
|
1999-05-18 15:11:14 +04:00
|
|
|
// short circuit when start node == end node
|
2012-06-14 10:47:03 +04:00
|
|
|
if (startNode == endNode) {
|
|
|
|
// Check to see if we have a collapsed range, if so, there is nothing to
|
|
|
|
// iterate over.
|
2002-11-21 18:07:49 +03:00
|
|
|
//
|
2012-06-14 10:47:03 +04:00
|
|
|
// XXX: CharacterDataNodes (text nodes) are currently an exception, since
|
|
|
|
// we always want to be able to iterate text nodes at the end points
|
|
|
|
// of a range.
|
2002-11-21 18:07:49 +03:00
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
if (!startIsData && startIndx == endIndx) {
|
2002-11-21 18:07:49 +03:00
|
|
|
MakeEmpty();
|
1999-05-18 15:11:14 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2002-11-21 18:07:49 +03:00
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
if (startIsData) {
|
|
|
|
// It's a character data node.
|
|
|
|
mFirst = startNode->AsContent();
|
2006-11-02 10:41:45 +03:00
|
|
|
mLast = mFirst;
|
|
|
|
mCurNode = mFirst;
|
2002-11-21 18:07:49 +03:00
|
|
|
|
2015-11-10 05:49:04 +03:00
|
|
|
nsresult rv = RebuildIndexStack();
|
|
|
|
NS_WARN_IF(NS_FAILED(rv));
|
2002-11-21 18:07:49 +03:00
|
|
|
return NS_OK;
|
1999-05-18 15:11:14 +04:00
|
|
|
}
|
|
|
|
}
|
2012-06-14 10:47:03 +04:00
|
|
|
|
2002-11-21 18:07:49 +03:00
|
|
|
// Find first node in range.
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
nsIContent* cChild = nullptr;
|
2002-11-21 18:07:49 +03:00
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
if (!startIsData && startNode->HasChildren()) {
|
2006-11-02 10:41:45 +03:00
|
|
|
cChild = startNode->GetChildAt(startIndx);
|
2015-11-10 05:49:04 +03:00
|
|
|
NS_WARN_IF(!cChild);
|
2012-06-14 10:47:03 +04:00
|
|
|
}
|
2002-11-21 18:07:49 +03:00
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
if (!cChild) {
|
|
|
|
// no children, must be a text node
|
|
|
|
//
|
2006-11-02 10:41:45 +03:00
|
|
|
// XXXbz no children might also just mean no children. So I'm not
|
|
|
|
// sure what that comment above is talking about.
|
2012-06-14 10:47:03 +04:00
|
|
|
|
|
|
|
if (mPre) {
|
2002-11-21 18:07:49 +03:00
|
|
|
// XXX: In the future, if start offset is after the last
|
|
|
|
// character in the cdata node, should we set mFirst to
|
|
|
|
// the next sibling?
|
|
|
|
|
2015-10-20 19:45:03 +03:00
|
|
|
// If the node has no child, the child may be <br> or something.
|
|
|
|
// So, we shouldn't skip the empty node if the start offset is 0.
|
|
|
|
// In other words, if the offset is 1, the node should be ignored.
|
|
|
|
if (!startIsData && startIndx) {
|
2012-06-14 10:47:03 +04:00
|
|
|
mFirst = GetNextSibling(startNode);
|
2015-11-10 05:49:04 +03:00
|
|
|
NS_WARN_IF(!mFirst);
|
2002-11-21 18:07:49 +03:00
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
// Does mFirst node really intersect the range? The range could be
|
|
|
|
// 'degenerate', i.e., not collapsed but still contain no content.
|
2015-11-10 05:49:04 +03:00
|
|
|
if (mFirst &&
|
|
|
|
NS_WARN_IF(!NodeIsInTraversalRange(mFirst, mPre, startNode,
|
|
|
|
startIndx, endNode, endIndx))) {
|
2012-07-30 18:20:58 +04:00
|
|
|
mFirst = nullptr;
|
2006-11-02 10:41:45 +03:00
|
|
|
}
|
2012-06-14 10:47:03 +04:00
|
|
|
} else {
|
|
|
|
mFirst = startNode->AsContent();
|
2006-11-02 10:41:45 +03:00
|
|
|
}
|
2012-06-14 10:47:03 +04:00
|
|
|
} else {
|
2006-11-02 10:41:45 +03:00
|
|
|
// post-order
|
2015-11-10 05:49:04 +03:00
|
|
|
if (NS_WARN_IF(!startNode->IsContent())) {
|
2006-11-02 10:41:45 +03:00
|
|
|
// What else can we do?
|
2012-07-30 18:20:58 +04:00
|
|
|
mFirst = nullptr;
|
2015-11-10 05:49:04 +03:00
|
|
|
} else {
|
|
|
|
mFirst = startNode->AsContent();
|
2002-11-21 18:07:49 +03:00
|
|
|
}
|
|
|
|
}
|
2012-06-14 10:47:03 +04:00
|
|
|
} else {
|
|
|
|
if (mPre) {
|
2002-11-21 18:07:49 +03:00
|
|
|
mFirst = cChild;
|
2012-06-14 10:47:03 +04:00
|
|
|
} else {
|
|
|
|
// post-order
|
2012-06-14 10:47:03 +04:00
|
|
|
mFirst = GetDeepFirstChild(cChild);
|
2015-11-10 05:49:04 +03:00
|
|
|
NS_WARN_IF(!mFirst);
|
2002-11-21 18:07:49 +03:00
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
// Does mFirst node really intersect the range? The range could be
|
|
|
|
// 'degenerate', i.e., not collapsed but still contain no content.
|
|
|
|
|
2015-11-10 05:49:04 +03:00
|
|
|
if (mFirst &&
|
2015-12-17 02:58:25 +03:00
|
|
|
!NodeIsInTraversalRange(mFirst, mPre, startNode, startIndx,
|
|
|
|
endNode, endIndx)) {
|
2012-07-30 18:20:58 +04:00
|
|
|
mFirst = nullptr;
|
2012-06-14 10:47:03 +04:00
|
|
|
}
|
1999-02-16 18:48:13 +03:00
|
|
|
}
|
|
|
|
}
|
1999-02-14 12:15:13 +03:00
|
|
|
|
2002-11-21 18:07:49 +03:00
|
|
|
|
|
|
|
// Find last node in range.
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool endIsData = endNode->IsNodeOfType(nsINode::eDATA_NODE);
|
2002-11-21 18:07:49 +03:00
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
if (endIsData || !endNode->HasChildren() || endIndx == 0) {
|
2006-11-02 10:41:45 +03:00
|
|
|
if (mPre) {
|
2015-11-10 05:49:04 +03:00
|
|
|
if (NS_WARN_IF(!endNode->IsContent())) {
|
|
|
|
// Not much else to do here...
|
|
|
|
mLast = nullptr;
|
|
|
|
} else {
|
2015-10-21 17:26:20 +03:00
|
|
|
// If the end node is an empty element and the end offset is 0,
|
|
|
|
// the last element should be the previous node (i.e., shouldn't
|
|
|
|
// include the end node in the range).
|
|
|
|
if (!endIsData && !endNode->HasChildren() && !endIndx) {
|
|
|
|
mLast = GetPrevSibling(endNode);
|
2015-11-10 05:49:04 +03:00
|
|
|
NS_WARN_IF(!mLast);
|
|
|
|
if (NS_WARN_IF(!NodeIsInTraversalRange(mLast, mPre,
|
|
|
|
startNode, startIndx,
|
|
|
|
endNode, endIndx))) {
|
2015-10-21 17:26:20 +03:00
|
|
|
mLast = nullptr;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
mLast = endNode->AsContent();
|
|
|
|
}
|
2006-11-02 10:41:45 +03:00
|
|
|
}
|
2012-06-14 10:47:03 +04:00
|
|
|
} else {
|
|
|
|
// post-order
|
|
|
|
//
|
|
|
|
// XXX: In the future, if end offset is before the first character in the
|
|
|
|
// cdata node, should we set mLast to the prev sibling?
|
|
|
|
|
|
|
|
if (!endIsData) {
|
2012-06-14 10:47:03 +04:00
|
|
|
mLast = GetPrevSibling(endNode);
|
2015-11-10 05:49:04 +03:00
|
|
|
NS_WARN_IF(!mLast);
|
2002-11-21 18:07:49 +03:00
|
|
|
|
2015-12-17 02:58:25 +03:00
|
|
|
if (!NodeIsInTraversalRange(mLast, mPre,
|
|
|
|
startNode, startIndx,
|
|
|
|
endNode, endIndx)) {
|
2012-07-30 18:20:58 +04:00
|
|
|
mLast = nullptr;
|
2012-06-14 10:47:03 +04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
mLast = endNode->AsContent();
|
2006-11-02 10:41:45 +03:00
|
|
|
}
|
2002-11-21 18:07:49 +03:00
|
|
|
}
|
2012-06-14 10:47:03 +04:00
|
|
|
} else {
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t indx = endIndx;
|
2001-11-03 06:05:05 +03:00
|
|
|
|
2006-11-02 10:41:45 +03:00
|
|
|
cChild = endNode->GetChildAt(--indx);
|
2002-11-21 18:07:49 +03:00
|
|
|
|
2015-11-10 05:49:04 +03:00
|
|
|
if (NS_WARN_IF(!cChild)) {
|
2012-06-14 10:47:03 +04:00
|
|
|
// No child at offset!
|
2002-11-21 18:07:49 +03:00
|
|
|
NS_NOTREACHED("nsContentIterator::nsContentIterator");
|
2012-06-14 10:47:03 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
1998-12-15 02:16:31 +03:00
|
|
|
}
|
2002-11-21 18:07:49 +03:00
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
if (mPre) {
|
2012-06-14 10:47:03 +04:00
|
|
|
mLast = GetDeepLastChild(cChild);
|
2015-11-10 05:49:04 +03:00
|
|
|
NS_WARN_IF(!mLast);
|
2002-11-21 18:07:49 +03:00
|
|
|
|
2015-11-10 05:49:04 +03:00
|
|
|
if (NS_WARN_IF(!NodeIsInTraversalRange(mLast, mPre,
|
|
|
|
startNode, startIndx,
|
|
|
|
endNode, endIndx))) {
|
2012-07-30 18:20:58 +04:00
|
|
|
mLast = nullptr;
|
2006-11-02 10:41:45 +03:00
|
|
|
}
|
2012-06-14 10:47:03 +04:00
|
|
|
} else {
|
|
|
|
// post-order
|
2002-11-21 18:07:49 +03:00
|
|
|
mLast = cChild;
|
2006-11-02 10:41:45 +03:00
|
|
|
}
|
2002-11-21 18:07:49 +03:00
|
|
|
}
|
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
// If either first or last is null, they both have to be null!
|
2002-11-21 18:07:49 +03:00
|
|
|
|
2015-12-17 02:58:25 +03:00
|
|
|
if (!mFirst || !mLast) {
|
2012-07-30 18:20:58 +04:00
|
|
|
mFirst = nullptr;
|
|
|
|
mLast = nullptr;
|
1998-12-15 02:16:31 +03:00
|
|
|
}
|
2012-06-14 10:47:03 +04:00
|
|
|
|
1998-12-15 02:16:31 +03:00
|
|
|
mCurNode = mFirst;
|
2002-11-21 18:07:49 +03:00
|
|
|
mIsDone = !mCurNode;
|
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
if (!mCurNode) {
|
2002-11-21 18:07:49 +03:00
|
|
|
mIndexes.Clear();
|
2012-06-14 10:47:03 +04:00
|
|
|
} else {
|
2015-11-10 05:49:04 +03:00
|
|
|
nsresult rv = RebuildIndexStack();
|
|
|
|
NS_WARN_IF(NS_FAILED(rv));
|
2012-06-14 10:47:03 +04:00
|
|
|
}
|
2002-11-21 18:07:49 +03:00
|
|
|
|
1999-02-12 08:28:12 +03:00
|
|
|
return NS_OK;
|
1998-12-15 02:16:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-02-12 08:28:12 +03:00
|
|
|
/******************************************************
|
1999-02-14 12:15:13 +03:00
|
|
|
* Helper routines
|
1999-02-12 08:28:12 +03:00
|
|
|
******************************************************/
|
2001-09-24 10:13:03 +04:00
|
|
|
// WARNING: This function is expensive
|
2012-06-14 10:47:03 +04:00
|
|
|
nsresult
|
|
|
|
nsContentIterator::RebuildIndexStack()
|
2001-09-24 10:13:03 +04:00
|
|
|
{
|
|
|
|
// Make sure we start at the right indexes on the stack! Build array up
|
|
|
|
// to common parent of start and end. Perhaps it's too many entries, but
|
2005-11-25 11:16:51 +03:00
|
|
|
// that's far better than too few.
|
2008-10-15 13:40:28 +04:00
|
|
|
nsINode* parent;
|
|
|
|
nsINode* current;
|
2001-09-24 10:13:03 +04:00
|
|
|
|
|
|
|
mIndexes.Clear();
|
|
|
|
current = mCurNode;
|
2003-07-29 01:09:56 +04:00
|
|
|
if (!current) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
while (current != mCommonParent) {
|
2012-10-09 16:31:24 +04:00
|
|
|
parent = current->GetParentNode();
|
2012-06-14 10:47:03 +04:00
|
|
|
|
2015-11-10 05:49:04 +03:00
|
|
|
if (NS_WARN_IF(!parent)) {
|
2001-09-24 10:13:03 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2012-06-14 10:47:03 +04:00
|
|
|
}
|
|
|
|
|
2009-03-20 11:15:35 +03:00
|
|
|
mIndexes.InsertElementAt(0, parent->IndexOf(current));
|
2001-09-24 10:13:03 +04:00
|
|
|
|
|
|
|
current = parent;
|
|
|
|
}
|
2012-06-14 10:47:03 +04:00
|
|
|
|
2001-09-24 10:13:03 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
1999-02-12 08:28:12 +03:00
|
|
|
|
2004-01-24 03:46:17 +03:00
|
|
|
void
|
|
|
|
nsContentIterator::MakeEmpty()
|
1999-03-01 11:17:18 +03:00
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
mCurNode = nullptr;
|
|
|
|
mFirst = nullptr;
|
|
|
|
mLast = nullptr;
|
|
|
|
mCommonParent = nullptr;
|
2011-10-17 18:59:28 +04:00
|
|
|
mIsDone = true;
|
2001-09-24 10:13:03 +04:00
|
|
|
mIndexes.Clear();
|
1999-03-01 11:17:18 +03:00
|
|
|
}
|
|
|
|
|
2008-10-15 13:40:28 +04:00
|
|
|
nsINode*
|
2012-06-14 10:47:03 +04:00
|
|
|
nsContentIterator::GetDeepFirstChild(nsINode* aRoot,
|
2012-08-22 19:56:38 +04:00
|
|
|
nsTArray<int32_t>* aIndexes)
|
2012-06-14 10:47:03 +04:00
|
|
|
{
|
2015-11-10 05:49:04 +03:00
|
|
|
if (NS_WARN_IF(!aRoot) || !aRoot->HasChildren()) {
|
2012-06-14 10:47:03 +04:00
|
|
|
return aRoot;
|
|
|
|
}
|
|
|
|
// We can't pass aRoot itself to the full GetDeepFirstChild, because that
|
|
|
|
// will only take nsIContent and aRoot might be a document. Pass aRoot's
|
|
|
|
// child, but be sure to preserve aIndexes.
|
|
|
|
if (aIndexes) {
|
|
|
|
aIndexes->AppendElement(0);
|
|
|
|
}
|
|
|
|
return GetDeepFirstChild(aRoot->GetFirstChild(), aIndexes);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIContent*
|
|
|
|
nsContentIterator::GetDeepFirstChild(nsIContent* aRoot,
|
2012-08-22 19:56:38 +04:00
|
|
|
nsTArray<int32_t>* aIndexes)
|
1998-12-15 02:16:31 +03:00
|
|
|
{
|
2015-11-10 05:49:04 +03:00
|
|
|
if (NS_WARN_IF(!aRoot)) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2003-09-27 08:18:26 +04:00
|
|
|
}
|
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
nsIContent* node = aRoot;
|
|
|
|
nsIContent* child = node->GetFirstChild();
|
2003-09-27 08:18:26 +04:00
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
while (child) {
|
|
|
|
if (aIndexes) {
|
2003-09-27 08:18:26 +04:00
|
|
|
// Add this node to the stack of indexes
|
2009-03-20 11:15:35 +03:00
|
|
|
aIndexes->AppendElement(0);
|
1999-03-09 22:21:30 +03:00
|
|
|
}
|
2012-06-14 10:47:03 +04:00
|
|
|
node = child;
|
|
|
|
child = node->GetFirstChild();
|
1999-02-12 08:28:12 +03:00
|
|
|
}
|
2003-09-27 08:18:26 +04:00
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
return node;
|
1998-12-15 02:16:31 +03:00
|
|
|
}
|
|
|
|
|
2008-10-15 13:40:28 +04:00
|
|
|
nsINode*
|
2012-06-14 10:47:03 +04:00
|
|
|
nsContentIterator::GetDeepLastChild(nsINode* aRoot,
|
2012-08-22 19:56:38 +04:00
|
|
|
nsTArray<int32_t>* aIndexes)
|
2012-06-14 10:47:03 +04:00
|
|
|
{
|
2015-11-10 05:49:04 +03:00
|
|
|
if (NS_WARN_IF(!aRoot) || !aRoot->HasChildren()) {
|
2012-06-14 10:47:03 +04:00
|
|
|
return aRoot;
|
|
|
|
}
|
|
|
|
// We can't pass aRoot itself to the full GetDeepLastChild, because that will
|
|
|
|
// only take nsIContent and aRoot might be a document. Pass aRoot's child,
|
|
|
|
// but be sure to preserve aIndexes.
|
|
|
|
if (aIndexes) {
|
|
|
|
aIndexes->AppendElement(aRoot->GetChildCount() - 1);
|
|
|
|
}
|
|
|
|
return GetDeepLastChild(aRoot->GetLastChild(), aIndexes);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIContent*
|
|
|
|
nsContentIterator::GetDeepLastChild(nsIContent* aRoot,
|
2012-08-22 19:56:38 +04:00
|
|
|
nsTArray<int32_t>* aIndexes)
|
1999-03-01 11:17:18 +03:00
|
|
|
{
|
2015-11-10 05:49:04 +03:00
|
|
|
if (NS_WARN_IF(!aRoot)) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2003-09-27 08:18:26 +04:00
|
|
|
}
|
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
nsIContent* node = aRoot;
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t numChildren = node->GetChildCount();
|
2003-09-27 08:18:26 +04:00
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
while (numChildren) {
|
2012-06-14 10:47:03 +04:00
|
|
|
nsIContent* child = node->GetChildAt(--numChildren);
|
2003-09-27 08:18:26 +04:00
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
if (aIndexes) {
|
2003-09-27 08:18:26 +04:00
|
|
|
// Add this node to the stack of indexes
|
2009-03-20 11:15:35 +03:00
|
|
|
aIndexes->AppendElement(numChildren);
|
1999-03-01 11:17:18 +03:00
|
|
|
}
|
2012-06-14 10:47:03 +04:00
|
|
|
numChildren = child->GetChildCount();
|
|
|
|
node = child;
|
1999-03-01 11:17:18 +03:00
|
|
|
}
|
2003-09-27 08:18:26 +04:00
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
return node;
|
1999-03-01 11:17:18 +03:00
|
|
|
}
|
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
// Get the next sibling, or parent's next sibling, or grandpa's next sibling...
|
|
|
|
nsIContent*
|
2012-06-14 10:47:03 +04:00
|
|
|
nsContentIterator::GetNextSibling(nsINode* aNode,
|
2012-08-22 19:56:38 +04:00
|
|
|
nsTArray<int32_t>* aIndexes)
|
1999-03-01 11:17:18 +03:00
|
|
|
{
|
2015-11-10 05:49:04 +03:00
|
|
|
if (NS_WARN_IF(!aNode)) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2012-06-14 10:47:03 +04:00
|
|
|
}
|
2003-07-29 01:09:56 +04:00
|
|
|
|
2012-10-09 16:31:24 +04:00
|
|
|
nsINode* parent = aNode->GetParentNode();
|
2015-11-10 05:49:04 +03:00
|
|
|
if (NS_WARN_IF(!parent)) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2012-06-14 10:47:03 +04:00
|
|
|
}
|
2004-01-24 03:46:17 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t indx = 0;
|
1999-03-08 04:20:02 +03:00
|
|
|
|
2009-03-20 11:15:35 +03:00
|
|
|
NS_ASSERTION(!aIndexes || !aIndexes->IsEmpty(),
|
|
|
|
"ContentIterator stack underflow");
|
2012-06-14 10:47:03 +04:00
|
|
|
if (aIndexes && !aIndexes->IsEmpty()) {
|
2001-09-24 10:13:03 +04:00
|
|
|
// use the last entry on the Indexes array for the current index
|
2009-03-20 11:15:35 +03:00
|
|
|
indx = (*aIndexes)[aIndexes->Length()-1];
|
2012-06-14 10:47:03 +04:00
|
|
|
} else {
|
2004-01-24 03:46:17 +03:00
|
|
|
indx = mCachedIndex;
|
2012-06-14 10:47:03 +04:00
|
|
|
}
|
2015-11-10 05:49:04 +03:00
|
|
|
NS_WARN_IF(indx < 0);
|
2004-01-24 03:46:17 +03:00
|
|
|
|
2001-09-24 10:13:03 +04:00
|
|
|
// reverify that the index of the current node hasn't changed.
|
|
|
|
// not super cheap, but a lot cheaper than IndexOf(), and still O(1).
|
|
|
|
// ignore result this time - the index may now be out of range.
|
2012-06-14 10:47:03 +04:00
|
|
|
nsIContent* sib = parent->GetChildAt(indx);
|
2012-06-14 10:47:03 +04:00
|
|
|
if (sib != aNode) {
|
2001-09-24 10:13:03 +04:00
|
|
|
// someone changed our index - find the new index the painful way
|
2003-09-27 08:18:26 +04:00
|
|
|
indx = parent->IndexOf(aNode);
|
2015-11-10 05:49:04 +03:00
|
|
|
NS_WARN_IF(indx < 0);
|
2001-09-24 10:13:03 +04:00
|
|
|
}
|
1999-03-01 11:17:18 +03:00
|
|
|
|
2001-09-24 10:13:03 +04:00
|
|
|
// indx is now canonically correct
|
2012-06-14 10:47:03 +04:00
|
|
|
if ((sib = parent->GetChildAt(++indx))) {
|
2001-09-24 10:13:03 +04:00
|
|
|
// update index cache
|
2012-06-14 10:47:03 +04:00
|
|
|
if (aIndexes && !aIndexes->IsEmpty()) {
|
2009-03-20 11:15:35 +03:00
|
|
|
aIndexes->ElementAt(aIndexes->Length()-1) = indx;
|
2012-06-14 10:47:03 +04:00
|
|
|
} else {
|
|
|
|
mCachedIndex = indx;
|
2001-09-24 10:13:03 +04:00
|
|
|
}
|
2012-06-14 10:47:03 +04:00
|
|
|
} else {
|
|
|
|
if (parent != mCommonParent) {
|
|
|
|
if (aIndexes) {
|
2004-03-10 02:57:56 +03:00
|
|
|
// pop node off the stack, go up one level and return parent or fail.
|
|
|
|
// Don't leave the index empty, especially if we're
|
2013-04-03 05:14:24 +04:00
|
|
|
// returning nullptr. This confuses other parts of the code.
|
2012-06-14 10:47:03 +04:00
|
|
|
if (aIndexes->Length() > 1) {
|
2009-03-20 11:15:35 +03:00
|
|
|
aIndexes->RemoveElementAt(aIndexes->Length()-1);
|
2012-06-14 10:47:03 +04:00
|
|
|
}
|
2004-03-10 02:57:56 +03:00
|
|
|
}
|
2001-09-24 10:13:03 +04:00
|
|
|
}
|
2004-03-10 02:57:56 +03:00
|
|
|
|
|
|
|
// ok to leave cache out of date here if parent == mCommonParent?
|
|
|
|
sib = GetNextSibling(parent, aIndexes);
|
1999-03-01 11:17:18 +03:00
|
|
|
}
|
2012-06-14 10:47:03 +04:00
|
|
|
|
2004-01-24 03:46:17 +03:00
|
|
|
return sib;
|
1999-03-01 11:17:18 +03:00
|
|
|
}
|
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
// Get the prev sibling, or parent's prev sibling, or grandpa's prev sibling...
|
|
|
|
nsIContent*
|
2012-06-14 10:47:03 +04:00
|
|
|
nsContentIterator::GetPrevSibling(nsINode* aNode,
|
2012-08-22 19:56:38 +04:00
|
|
|
nsTArray<int32_t>* aIndexes)
|
1999-03-01 11:17:18 +03:00
|
|
|
{
|
2015-11-10 05:49:04 +03:00
|
|
|
if (NS_WARN_IF(!aNode)) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2012-06-14 10:47:03 +04:00
|
|
|
}
|
2003-07-29 01:09:56 +04:00
|
|
|
|
2012-10-09 16:31:24 +04:00
|
|
|
nsINode* parent = aNode->GetParentNode();
|
2015-11-10 05:49:04 +03:00
|
|
|
if (NS_WARN_IF(!parent)) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2012-06-14 10:47:03 +04:00
|
|
|
}
|
2004-01-24 03:46:17 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t indx = 0;
|
1999-03-08 04:20:02 +03:00
|
|
|
|
2009-03-20 11:15:35 +03:00
|
|
|
NS_ASSERTION(!aIndexes || !aIndexes->IsEmpty(),
|
|
|
|
"ContentIterator stack underflow");
|
2012-06-14 10:47:03 +04:00
|
|
|
if (aIndexes && !aIndexes->IsEmpty()) {
|
2001-09-24 10:13:03 +04:00
|
|
|
// use the last entry on the Indexes array for the current index
|
2009-03-20 11:15:35 +03:00
|
|
|
indx = (*aIndexes)[aIndexes->Length()-1];
|
2012-06-14 10:47:03 +04:00
|
|
|
} else {
|
2004-01-24 03:46:17 +03:00
|
|
|
indx = mCachedIndex;
|
2012-06-14 10:47:03 +04:00
|
|
|
}
|
2004-01-24 03:46:17 +03:00
|
|
|
|
2001-09-24 10:13:03 +04:00
|
|
|
// reverify that the index of the current node hasn't changed
|
|
|
|
// ignore result this time - the index may now be out of range.
|
2012-06-14 10:47:03 +04:00
|
|
|
nsIContent* sib = parent->GetChildAt(indx);
|
2012-06-14 10:47:03 +04:00
|
|
|
if (sib != aNode) {
|
2001-09-24 10:13:03 +04:00
|
|
|
// someone changed our index - find the new index the painful way
|
2003-09-27 08:18:26 +04:00
|
|
|
indx = parent->IndexOf(aNode);
|
2015-11-10 05:49:04 +03:00
|
|
|
NS_WARN_IF(indx < 0);
|
2001-09-24 10:13:03 +04:00
|
|
|
}
|
1999-03-01 11:17:18 +03:00
|
|
|
|
2001-09-24 10:13:03 +04:00
|
|
|
// indx is now canonically correct
|
2012-06-14 10:47:03 +04:00
|
|
|
if (indx > 0 && (sib = parent->GetChildAt(--indx))) {
|
2001-09-24 10:13:03 +04:00
|
|
|
// update index cache
|
2012-06-14 10:47:03 +04:00
|
|
|
if (aIndexes && !aIndexes->IsEmpty()) {
|
2009-03-20 11:15:35 +03:00
|
|
|
aIndexes->ElementAt(aIndexes->Length()-1) = indx;
|
2012-06-14 10:47:03 +04:00
|
|
|
} else {
|
|
|
|
mCachedIndex = indx;
|
2001-09-24 10:13:03 +04:00
|
|
|
}
|
2012-06-14 10:47:03 +04:00
|
|
|
} else if (parent != mCommonParent) {
|
|
|
|
if (aIndexes && !aIndexes->IsEmpty()) {
|
2001-09-24 10:13:03 +04:00
|
|
|
// pop node off the stack, go up one level and try again.
|
2009-03-20 11:15:35 +03:00
|
|
|
aIndexes->RemoveElementAt(aIndexes->Length()-1);
|
2001-09-24 10:13:03 +04:00
|
|
|
}
|
2004-01-24 03:46:17 +03:00
|
|
|
return GetPrevSibling(parent, aIndexes);
|
1999-03-01 11:17:18 +03:00
|
|
|
}
|
2001-09-24 10:13:03 +04:00
|
|
|
|
2004-01-24 03:46:17 +03:00
|
|
|
return sib;
|
1999-03-01 11:17:18 +03:00
|
|
|
}
|
|
|
|
|
2008-10-15 13:40:28 +04:00
|
|
|
nsINode*
|
2012-08-22 19:56:38 +04:00
|
|
|
nsContentIterator::NextNode(nsINode* aNode, nsTArray<int32_t>* aIndexes)
|
1999-02-16 18:48:13 +03:00
|
|
|
{
|
2012-06-14 10:47:03 +04:00
|
|
|
nsINode* node = aNode;
|
2001-09-24 10:13:03 +04:00
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
// if we are a Pre-order iterator, use pre-order
|
|
|
|
if (mPre) {
|
1999-03-23 13:30:24 +03:00
|
|
|
// if it has children then next node is first child
|
2012-06-14 10:47:03 +04:00
|
|
|
if (node->HasChildren()) {
|
2012-06-14 10:47:03 +04:00
|
|
|
nsIContent* firstChild = node->GetFirstChild();
|
2015-11-10 05:49:04 +03:00
|
|
|
MOZ_ASSERT(firstChild);
|
2003-09-27 08:18:26 +04:00
|
|
|
|
2001-09-24 10:13:03 +04:00
|
|
|
// update cache
|
2012-06-14 10:47:03 +04:00
|
|
|
if (aIndexes) {
|
2001-09-24 10:13:03 +04:00
|
|
|
// push an entry on the index stack
|
2009-03-20 11:15:35 +03:00
|
|
|
aIndexes->AppendElement(0);
|
2012-06-14 10:47:03 +04:00
|
|
|
} else {
|
|
|
|
mCachedIndex = 0;
|
2001-09-24 10:13:03 +04:00
|
|
|
}
|
2012-06-14 10:47:03 +04:00
|
|
|
|
|
|
|
return firstChild;
|
1999-03-23 13:30:24 +03:00
|
|
|
}
|
2004-01-24 03:46:17 +03:00
|
|
|
|
1999-03-23 13:30:24 +03:00
|
|
|
// else next sibling is next
|
2012-06-14 10:47:03 +04:00
|
|
|
return GetNextSibling(node, aIndexes);
|
1999-02-16 18:48:13 +03:00
|
|
|
}
|
2003-07-29 01:09:56 +04:00
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
// post-order
|
2012-10-09 16:31:24 +04:00
|
|
|
nsINode* parent = node->GetParentNode();
|
2016-03-31 09:00:50 +03:00
|
|
|
if (NS_WARN_IF(!parent)) {
|
|
|
|
MOZ_ASSERT(parent, "The node is the root node but not the last node");
|
|
|
|
mIsDone = true;
|
|
|
|
return node;
|
|
|
|
}
|
2012-07-30 18:20:58 +04:00
|
|
|
nsIContent* sibling = nullptr;
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t indx = 0;
|
2001-11-03 06:05:05 +03:00
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
// get the cached index
|
|
|
|
NS_ASSERTION(!aIndexes || !aIndexes->IsEmpty(),
|
|
|
|
"ContentIterator stack underflow");
|
|
|
|
if (aIndexes && !aIndexes->IsEmpty()) {
|
|
|
|
// use the last entry on the Indexes array for the current index
|
|
|
|
indx = (*aIndexes)[aIndexes->Length()-1];
|
|
|
|
} else {
|
|
|
|
indx = mCachedIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
// reverify that the index of the current node hasn't changed. not super
|
|
|
|
// cheap, but a lot cheaper than IndexOf(), and still O(1). ignore result
|
|
|
|
// this time - the index may now be out of range.
|
|
|
|
if (indx >= 0) {
|
|
|
|
sibling = parent->GetChildAt(indx);
|
|
|
|
}
|
|
|
|
if (sibling != node) {
|
|
|
|
// someone changed our index - find the new index the painful way
|
|
|
|
indx = parent->IndexOf(node);
|
2015-11-10 05:49:04 +03:00
|
|
|
NS_WARN_IF(indx < 0);
|
2012-06-14 10:47:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// indx is now canonically correct
|
|
|
|
sibling = parent->GetChildAt(++indx);
|
|
|
|
if (sibling) {
|
2001-09-24 10:13:03 +04:00
|
|
|
// update cache
|
2012-06-14 10:47:03 +04:00
|
|
|
if (aIndexes && !aIndexes->IsEmpty()) {
|
|
|
|
// replace an entry on the index stack
|
|
|
|
aIndexes->ElementAt(aIndexes->Length()-1) = indx;
|
|
|
|
} else {
|
|
|
|
mCachedIndex = indx;
|
|
|
|
}
|
|
|
|
|
|
|
|
// next node is sibling's "deep left" child
|
|
|
|
return GetDeepFirstChild(sibling, aIndexes);
|
|
|
|
}
|
|
|
|
|
|
|
|
// else it's the parent, update cache
|
|
|
|
if (aIndexes) {
|
|
|
|
// Pop an entry off the index stack. Don't leave the index empty,
|
2013-04-03 05:14:24 +04:00
|
|
|
// especially if we're returning nullptr. This confuses other parts of the
|
2012-06-14 10:47:03 +04:00
|
|
|
// code.
|
|
|
|
if (aIndexes->Length() > 1) {
|
|
|
|
aIndexes->RemoveElementAt(aIndexes->Length()-1);
|
2001-09-24 10:13:03 +04:00
|
|
|
}
|
2012-06-14 10:47:03 +04:00
|
|
|
} else {
|
|
|
|
// this might be wrong, but we are better off guessing
|
|
|
|
mCachedIndex = 0;
|
1999-03-23 13:30:24 +03:00
|
|
|
}
|
2004-01-24 03:46:17 +03:00
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
return parent;
|
1999-02-16 18:48:13 +03:00
|
|
|
}
|
|
|
|
|
2008-10-15 13:40:28 +04:00
|
|
|
nsINode*
|
2012-08-22 19:56:38 +04:00
|
|
|
nsContentIterator::PrevNode(nsINode* aNode, nsTArray<int32_t>* aIndexes)
|
1999-02-16 18:48:13 +03:00
|
|
|
{
|
2012-06-14 10:47:03 +04:00
|
|
|
nsINode* node = aNode;
|
|
|
|
|
|
|
|
// if we are a Pre-order iterator, use pre-order
|
|
|
|
if (mPre) {
|
2012-10-09 16:31:24 +04:00
|
|
|
nsINode* parent = node->GetParentNode();
|
2016-03-31 09:00:50 +03:00
|
|
|
if (NS_WARN_IF(!parent)) {
|
|
|
|
MOZ_ASSERT(parent, "The node is the root node but not the first node");
|
|
|
|
mIsDone = true;
|
|
|
|
return aNode;
|
|
|
|
}
|
2012-07-30 18:20:58 +04:00
|
|
|
nsIContent* sibling = nullptr;
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t indx = 0;
|
2001-09-24 10:13:03 +04:00
|
|
|
|
|
|
|
// get the cached index
|
2009-03-20 11:15:35 +03:00
|
|
|
NS_ASSERTION(!aIndexes || !aIndexes->IsEmpty(),
|
|
|
|
"ContentIterator stack underflow");
|
2012-06-14 10:47:03 +04:00
|
|
|
if (aIndexes && !aIndexes->IsEmpty()) {
|
2001-09-24 10:13:03 +04:00
|
|
|
// use the last entry on the Indexes array for the current index
|
2009-03-20 11:15:35 +03:00
|
|
|
indx = (*aIndexes)[aIndexes->Length()-1];
|
2012-06-14 10:47:03 +04:00
|
|
|
} else {
|
|
|
|
indx = mCachedIndex;
|
1999-05-11 03:29:01 +04:00
|
|
|
}
|
2001-09-24 10:13:03 +04:00
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
// reverify that the index of the current node hasn't changed. not super
|
|
|
|
// cheap, but a lot cheaper than IndexOf(), and still O(1). ignore result
|
|
|
|
// this time - the index may now be out of range.
|
|
|
|
if (indx >= 0) {
|
|
|
|
sibling = parent->GetChildAt(indx);
|
2015-11-10 05:49:04 +03:00
|
|
|
NS_WARN_IF(!sibling);
|
2012-06-14 10:47:03 +04:00
|
|
|
}
|
2003-09-27 08:18:26 +04:00
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
if (sibling != node) {
|
2001-11-03 06:05:05 +03:00
|
|
|
// someone changed our index - find the new index the painful way
|
2012-06-14 10:47:03 +04:00
|
|
|
indx = parent->IndexOf(node);
|
2015-11-10 05:49:04 +03:00
|
|
|
NS_WARN_IF(indx < 0);
|
2001-11-03 06:05:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// indx is now canonically correct
|
2012-06-14 10:47:03 +04:00
|
|
|
if (indx && (sibling = parent->GetChildAt(--indx))) {
|
2001-09-24 10:13:03 +04:00
|
|
|
// update cache
|
2012-06-14 10:47:03 +04:00
|
|
|
if (aIndexes && !aIndexes->IsEmpty()) {
|
2001-09-24 10:13:03 +04:00
|
|
|
// replace an entry on the index stack
|
2009-03-20 11:15:35 +03:00
|
|
|
aIndexes->ElementAt(aIndexes->Length()-1) = indx;
|
2012-06-14 10:47:03 +04:00
|
|
|
} else {
|
|
|
|
mCachedIndex = indx;
|
2001-09-24 10:13:03 +04:00
|
|
|
}
|
2012-06-14 10:47:03 +04:00
|
|
|
|
|
|
|
// prev node is sibling's "deep right" child
|
|
|
|
return GetDeepLastChild(sibling, aIndexes);
|
1999-03-23 13:30:24 +03:00
|
|
|
}
|
2012-06-14 10:47:03 +04:00
|
|
|
|
|
|
|
// else it's the parent, update cache
|
|
|
|
if (aIndexes && !aIndexes->IsEmpty()) {
|
2001-09-24 10:13:03 +04:00
|
|
|
// pop an entry off the index stack
|
2009-03-20 11:15:35 +03:00
|
|
|
aIndexes->RemoveElementAt(aIndexes->Length()-1);
|
2012-06-14 10:47:03 +04:00
|
|
|
} else {
|
|
|
|
// this might be wrong, but we are better off guessing
|
|
|
|
mCachedIndex = 0;
|
2001-09-24 10:13:03 +04:00
|
|
|
}
|
2012-06-14 10:47:03 +04:00
|
|
|
return parent;
|
1999-02-16 18:48:13 +03:00
|
|
|
}
|
2001-09-24 10:13:03 +04:00
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
// post-order
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t numChildren = node->GetChildCount();
|
2015-11-10 05:49:04 +03:00
|
|
|
NS_WARN_IF(numChildren < 0);
|
2012-06-14 10:47:03 +04:00
|
|
|
|
|
|
|
// if it has children then prev node is last child
|
|
|
|
if (numChildren) {
|
2012-06-14 10:47:03 +04:00
|
|
|
nsIContent* lastChild = node->GetLastChild();
|
2015-11-10 05:49:04 +03:00
|
|
|
NS_WARN_IF(!lastChild);
|
2012-06-14 10:47:03 +04:00
|
|
|
numChildren--;
|
|
|
|
|
|
|
|
// update cache
|
|
|
|
if (aIndexes) {
|
|
|
|
// push an entry on the index stack
|
|
|
|
aIndexes->AppendElement(numChildren);
|
|
|
|
} else {
|
|
|
|
mCachedIndex = numChildren;
|
1999-03-23 13:30:24 +03:00
|
|
|
}
|
2004-01-24 03:46:17 +03:00
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
return lastChild;
|
1999-03-23 13:30:24 +03:00
|
|
|
}
|
2004-01-24 03:46:17 +03:00
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
// else prev sibling is previous
|
|
|
|
return GetPrevSibling(node, aIndexes);
|
1999-02-16 18:48:13 +03:00
|
|
|
}
|
|
|
|
|
1999-02-12 08:28:12 +03:00
|
|
|
/******************************************************
|
|
|
|
* ContentIterator routines
|
|
|
|
******************************************************/
|
1998-12-15 02:16:31 +03:00
|
|
|
|
2004-01-24 03:46:17 +03:00
|
|
|
void
|
|
|
|
nsContentIterator::First()
|
1998-12-15 02:16:31 +03:00
|
|
|
{
|
2004-01-24 03:46:17 +03:00
|
|
|
if (mFirst) {
|
2016-06-23 12:58:22 +03:00
|
|
|
mozilla::DebugOnly<nsresult> rv = PositionAt(mFirst);
|
2004-01-24 03:46:17 +03:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "Failed to position iterator!");
|
|
|
|
}
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
mIsDone = mFirst == nullptr;
|
1998-12-15 02:16:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-01-24 03:46:17 +03:00
|
|
|
void
|
|
|
|
nsContentIterator::Last()
|
1998-12-15 02:16:31 +03:00
|
|
|
{
|
2004-01-24 03:46:17 +03:00
|
|
|
NS_ASSERTION(mLast, "No last node!");
|
2002-11-21 18:07:49 +03:00
|
|
|
|
2004-01-24 03:46:17 +03:00
|
|
|
if (mLast) {
|
2016-06-23 12:58:22 +03:00
|
|
|
mozilla::DebugOnly<nsresult> rv = PositionAt(mLast);
|
2004-01-24 03:46:17 +03:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "Failed to position iterator!");
|
|
|
|
}
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
mIsDone = mLast == nullptr;
|
1998-12-15 02:16:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-01-24 03:46:17 +03:00
|
|
|
void
|
|
|
|
nsContentIterator::Next()
|
1998-12-15 02:16:31 +03:00
|
|
|
{
|
2015-11-10 05:49:04 +03:00
|
|
|
if (mIsDone || NS_WARN_IF(!mCurNode)) {
|
2004-01-24 03:46:17 +03:00
|
|
|
return;
|
2012-06-14 10:47:03 +04:00
|
|
|
}
|
2004-01-24 03:46:17 +03:00
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
if (mCurNode == mLast) {
|
2011-10-17 18:59:28 +04:00
|
|
|
mIsDone = true;
|
2004-01-24 03:46:17 +03:00
|
|
|
return;
|
1999-01-29 02:55:53 +03:00
|
|
|
}
|
2004-01-24 03:46:17 +03:00
|
|
|
|
|
|
|
mCurNode = NextNode(mCurNode, &mIndexes);
|
1998-12-15 02:16:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-01-24 03:46:17 +03:00
|
|
|
void
|
|
|
|
nsContentIterator::Prev()
|
1998-12-15 02:16:31 +03:00
|
|
|
{
|
2015-11-10 05:49:04 +03:00
|
|
|
if (NS_WARN_IF(mIsDone) || NS_WARN_IF(!mCurNode)) {
|
2004-01-24 03:46:17 +03:00
|
|
|
return;
|
2012-06-14 10:47:03 +04:00
|
|
|
}
|
2004-01-24 03:46:17 +03:00
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
if (mCurNode == mFirst) {
|
2011-10-17 18:59:28 +04:00
|
|
|
mIsDone = true;
|
2004-01-24 03:46:17 +03:00
|
|
|
return;
|
1999-03-01 11:17:18 +03:00
|
|
|
}
|
2004-01-24 03:46:17 +03:00
|
|
|
|
|
|
|
mCurNode = PrevNode(mCurNode, &mIndexes);
|
1998-12-15 02:16:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2004-01-24 03:46:17 +03:00
|
|
|
nsContentIterator::IsDone()
|
1999-02-12 08:28:12 +03:00
|
|
|
{
|
2004-01-24 03:46:17 +03:00
|
|
|
return mIsDone;
|
1999-02-12 08:28:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-09-24 10:13:03 +04:00
|
|
|
// Keeping arrays of indexes for the stack of nodes makes PositionAt
|
|
|
|
// interesting...
|
2004-01-24 03:46:17 +03:00
|
|
|
nsresult
|
2008-10-15 13:40:28 +04:00
|
|
|
nsContentIterator::PositionAt(nsINode* aCurNode)
|
1999-03-23 13:30:24 +03:00
|
|
|
{
|
2015-11-10 05:49:04 +03:00
|
|
|
if (NS_WARN_IF(!aCurNode)) {
|
1999-03-23 13:30:24 +03:00
|
|
|
return NS_ERROR_NULL_POINTER;
|
2012-06-14 10:47:03 +04:00
|
|
|
}
|
2004-01-24 03:46:17 +03:00
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
nsINode* newCurNode = aCurNode;
|
|
|
|
nsINode* tempNode = mCurNode;
|
2004-01-24 03:46:17 +03:00
|
|
|
|
|
|
|
mCurNode = aCurNode;
|
2001-09-24 10:13:03 +04:00
|
|
|
// take an early out if this doesn't actually change the position
|
2012-06-14 10:47:03 +04:00
|
|
|
if (mCurNode == tempNode) {
|
2011-10-17 18:59:28 +04:00
|
|
|
mIsDone = false; // paranoia
|
2001-09-24 10:13:03 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2002-11-21 18:07:49 +03:00
|
|
|
// Check to see if the node falls within the traversal range.
|
|
|
|
|
2008-10-15 13:40:28 +04:00
|
|
|
nsINode* firstNode = mFirst;
|
|
|
|
nsINode* lastNode = mLast;
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t firstOffset = 0, lastOffset = 0;
|
2002-11-21 18:07:49 +03:00
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
if (firstNode && lastNode) {
|
|
|
|
if (mPre) {
|
2008-10-15 13:40:28 +04:00
|
|
|
firstNode = NodeToParentOffset(mFirst, &firstOffset);
|
2015-11-10 05:49:04 +03:00
|
|
|
NS_WARN_IF(!firstNode);
|
|
|
|
NS_WARN_IF(firstOffset < 0);
|
2002-11-21 18:07:49 +03:00
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
if (lastNode->GetChildCount()) {
|
2002-11-21 18:07:49 +03:00
|
|
|
lastOffset = 0;
|
2012-06-14 10:47:03 +04:00
|
|
|
} else {
|
2008-10-15 13:40:28 +04:00
|
|
|
lastNode = NodeToParentOffset(mLast, &lastOffset);
|
2015-11-10 05:49:04 +03:00
|
|
|
NS_WARN_IF(!lastNode);
|
|
|
|
NS_WARN_IF(lastOffset < 0);
|
2002-11-21 18:07:49 +03:00
|
|
|
++lastOffset;
|
|
|
|
}
|
2012-06-14 10:47:03 +04:00
|
|
|
} else {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t numChildren = firstNode->GetChildCount();
|
2008-03-20 02:23:59 +03:00
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
if (numChildren) {
|
2002-11-21 18:07:49 +03:00
|
|
|
firstOffset = numChildren;
|
2015-11-10 05:49:04 +03:00
|
|
|
NS_WARN_IF(firstOffset < 0);
|
2012-06-14 10:47:03 +04:00
|
|
|
} else {
|
2008-10-15 13:40:28 +04:00
|
|
|
firstNode = NodeToParentOffset(mFirst, &firstOffset);
|
2015-11-10 05:49:04 +03:00
|
|
|
NS_WARN_IF(!firstNode);
|
|
|
|
NS_WARN_IF(firstOffset < 0);
|
2012-06-14 10:47:03 +04:00
|
|
|
}
|
2004-01-24 03:46:17 +03:00
|
|
|
|
2008-10-15 13:40:28 +04:00
|
|
|
lastNode = NodeToParentOffset(mLast, &lastOffset);
|
2015-11-10 05:49:04 +03:00
|
|
|
NS_WARN_IF(!lastNode);
|
|
|
|
NS_WARN_IF(lastOffset < 0);
|
2002-11-21 18:07:49 +03:00
|
|
|
++lastOffset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
// The end positions are always in the range even if it has no parent. We
|
|
|
|
// need to allow that or 'iter->Init(root)' would assert in Last() or First()
|
|
|
|
// for example, bug 327694.
|
2009-08-27 22:05:23 +04:00
|
|
|
if (mFirst != mCurNode && mLast != mCurNode &&
|
2015-11-10 05:49:04 +03:00
|
|
|
(NS_WARN_IF(!firstNode) || NS_WARN_IF(!lastNode) ||
|
|
|
|
NS_WARN_IF(!NodeIsInTraversalRange(mCurNode, mPre,
|
|
|
|
firstNode, firstOffset,
|
|
|
|
lastNode, lastOffset)))) {
|
2011-10-17 18:59:28 +04:00
|
|
|
mIsDone = true;
|
2002-11-21 18:07:49 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
// We can be at ANY node in the sequence. Need to regenerate the array of
|
|
|
|
// indexes back to the root or common parent!
|
2016-02-02 18:36:30 +03:00
|
|
|
AutoTArray<nsINode*, 8> oldParentStack;
|
|
|
|
AutoTArray<int32_t, 8> newIndexes;
|
2001-09-24 10:13:03 +04:00
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
// Get a list of the parents up to the root, then compare the new node with
|
|
|
|
// entries in that array until we find a match (lowest common ancestor). If
|
|
|
|
// no match, use IndexOf, take the parent, and repeat. This avoids using
|
|
|
|
// IndexOf() N times on possibly large arrays. We still end up doing it a
|
|
|
|
// fair bit. It's better to use Clone() if possible.
|
2001-09-24 10:13:03 +04:00
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
// we know the depth we're down (though we may not have started at the top).
|
2013-03-21 22:52:48 +04:00
|
|
|
oldParentStack.SetCapacity(mIndexes.Length() + 1);
|
2001-09-24 10:13:03 +04:00
|
|
|
|
2009-03-20 11:15:35 +03:00
|
|
|
// We want to loop mIndexes.Length() + 1 times here, because we want to make
|
2005-01-12 20:53:16 +03:00
|
|
|
// sure we include mCommonParent in the oldParentStack, for use in the next
|
|
|
|
// for loop, and mIndexes only has entries for nodes from tempNode up through
|
|
|
|
// an ancestor of tempNode that's a child of mCommonParent.
|
2012-08-22 19:56:38 +04:00
|
|
|
for (int32_t i = mIndexes.Length() + 1; i > 0 && tempNode; i--) {
|
2001-09-24 10:13:03 +04:00
|
|
|
// Insert at head since we're walking up
|
2009-03-20 11:15:35 +03:00
|
|
|
oldParentStack.InsertElementAt(0, tempNode);
|
2001-09-24 10:13:03 +04:00
|
|
|
|
2012-10-09 16:31:24 +04:00
|
|
|
nsINode* parent = tempNode->GetParentNode();
|
2001-09-24 10:13:03 +04:00
|
|
|
|
2015-11-10 05:49:04 +03:00
|
|
|
if (NS_WARN_IF(!parent)) {
|
2012-06-14 10:47:03 +04:00
|
|
|
// this node has no parent, and thus no index
|
2001-09-24 10:13:03 +04:00
|
|
|
break;
|
2012-06-14 10:47:03 +04:00
|
|
|
}
|
2001-09-24 10:13:03 +04:00
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
if (parent == mCurNode) {
|
|
|
|
// The position was moved to a parent of the current position. All we
|
|
|
|
// need to do is drop some indexes. Shortcut here.
|
2009-03-20 11:15:35 +03:00
|
|
|
mIndexes.RemoveElementsAt(mIndexes.Length() - oldParentStack.Length(),
|
|
|
|
oldParentStack.Length());
|
2011-10-17 18:59:28 +04:00
|
|
|
mIsDone = false;
|
2001-09-24 10:13:03 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
tempNode = parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ok. We have the array of old parents. Look for a match.
|
2012-06-14 10:47:03 +04:00
|
|
|
while (newCurNode) {
|
2012-10-09 16:31:24 +04:00
|
|
|
nsINode* parent = newCurNode->GetParentNode();
|
2004-01-24 03:46:17 +03:00
|
|
|
|
2015-11-10 05:49:04 +03:00
|
|
|
if (NS_WARN_IF(!parent)) {
|
2012-06-14 10:47:03 +04:00
|
|
|
// this node has no parent, and thus no index
|
2001-09-24 10:13:03 +04:00
|
|
|
break;
|
2012-06-14 10:47:03 +04:00
|
|
|
}
|
2001-09-24 10:13:03 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t indx = parent->IndexOf(newCurNode);
|
2015-11-10 05:49:04 +03:00
|
|
|
NS_WARN_IF(indx < 0);
|
2001-09-24 10:13:03 +04:00
|
|
|
|
|
|
|
// insert at the head!
|
2009-03-20 11:15:35 +03:00
|
|
|
newIndexes.InsertElementAt(0, indx);
|
2001-09-24 10:13:03 +04:00
|
|
|
|
|
|
|
// look to see if the parent is in the stack
|
|
|
|
indx = oldParentStack.IndexOf(parent);
|
2012-06-14 10:47:03 +04:00
|
|
|
if (indx >= 0) {
|
|
|
|
// ok, the parent IS on the old stack! Rework things. We want
|
|
|
|
// newIndexes to replace all nodes equal to or below the match. Note
|
|
|
|
// that index oldParentStack.Length() - 1 is the last node, which is one
|
|
|
|
// BELOW the last index in the mIndexes stack. In other words, we want
|
|
|
|
// to remove elements starting at index (indx + 1).
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t numToDrop = oldParentStack.Length() - (1 + indx);
|
2012-06-14 10:47:03 +04:00
|
|
|
if (numToDrop > 0) {
|
2009-03-20 11:15:35 +03:00
|
|
|
mIndexes.RemoveElementsAt(mIndexes.Length() - numToDrop, numToDrop);
|
2012-06-14 10:47:03 +04:00
|
|
|
}
|
2005-01-12 20:53:16 +03:00
|
|
|
mIndexes.AppendElements(newIndexes);
|
2001-09-24 10:13:03 +04:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
newCurNode = parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
// phew!
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mIsDone = false;
|
1999-03-23 13:30:24 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-10-15 13:40:28 +04:00
|
|
|
nsINode*
|
2004-01-24 03:46:17 +03:00
|
|
|
nsContentIterator::GetCurrentNode()
|
1998-12-15 02:16:31 +03:00
|
|
|
{
|
2004-01-24 03:46:17 +03:00
|
|
|
if (mIsDone) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2002-12-11 17:24:49 +03:00
|
|
|
}
|
|
|
|
|
2004-01-24 03:46:17 +03:00
|
|
|
NS_ASSERTION(mCurNode, "Null current node in an iterator that's not done!");
|
|
|
|
|
|
|
|
return mCurNode;
|
1998-12-15 02:16:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-02-14 12:15:13 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*====================================================================================*/
|
|
|
|
/*====================================================================================*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************
|
|
|
|
* nsContentSubtreeIterator
|
|
|
|
******************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A simple iterator class for traversing the content in "top subtree" order
|
|
|
|
*/
|
2012-06-12 12:45:09 +04:00
|
|
|
class nsContentSubtreeIterator : public nsContentIterator
|
1999-02-14 12:15:13 +03:00
|
|
|
{
|
|
|
|
public:
|
2011-10-17 18:59:28 +04:00
|
|
|
nsContentSubtreeIterator() : nsContentIterator(false) {}
|
1999-02-14 12:15:13 +03:00
|
|
|
|
2010-06-08 08:31:44 +04:00
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(nsContentSubtreeIterator, nsContentIterator)
|
|
|
|
|
1999-02-14 12:15:13 +03:00
|
|
|
// nsContentIterator overrides ------------------------------
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual nsresult Init(nsINode* aRoot) override;
|
1999-02-14 12:15:13 +03:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual nsresult Init(nsIDOMRange* aRange) override;
|
1999-02-14 12:15:13 +03:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual void Next() override;
|
1999-02-14 12:15:13 +03:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual void Prev() override;
|
1999-02-14 12:15:13 +03:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual nsresult PositionAt(nsINode* aCurNode) override;
|
1999-03-23 13:30:24 +03:00
|
|
|
|
2001-09-28 01:49:40 +04:00
|
|
|
// Must override these because we don't do PositionAt
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual void First() override;
|
2001-09-28 01:49:40 +04:00
|
|
|
|
|
|
|
// Must override these because we don't do PositionAt
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual void Last() override;
|
2001-09-28 01:49:40 +04:00
|
|
|
|
1999-03-01 11:17:18 +03:00
|
|
|
protected:
|
2014-07-09 01:23:16 +04:00
|
|
|
virtual ~nsContentSubtreeIterator() {}
|
1999-02-14 12:15:13 +03:00
|
|
|
|
2012-06-12 12:45:09 +04:00
|
|
|
// Returns the highest inclusive ancestor of aNode that's in the range
|
|
|
|
// (possibly aNode itself). Returns null if aNode is null, or is not itself
|
2012-06-14 10:47:03 +04:00
|
|
|
// in the range. A node is in the range if (node, 0) comes strictly after
|
|
|
|
// the range endpoint, and (node, node.length) comes strictly before it, so
|
|
|
|
// the range's start and end nodes will never be considered "in" it.
|
|
|
|
nsIContent* GetTopAncestorInRange(nsINode* aNode);
|
2004-01-24 03:46:17 +03:00
|
|
|
|
1999-02-14 12:15:13 +03:00
|
|
|
// no copy's or assigns FIX ME
|
|
|
|
nsContentSubtreeIterator(const nsContentSubtreeIterator&);
|
|
|
|
nsContentSubtreeIterator& operator=(const nsContentSubtreeIterator&);
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual void LastRelease() override;
|
2013-07-09 21:30:58 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsRange> mRange;
|
2004-03-10 02:57:56 +03:00
|
|
|
|
2012-06-12 12:45:09 +04:00
|
|
|
// these arrays all typically are used and have elements
|
2016-02-02 18:36:30 +03:00
|
|
|
AutoTArray<nsIContent*, 8> mEndNodes;
|
|
|
|
AutoTArray<int32_t, 8> mEndOffsets;
|
1999-02-14 12:15:13 +03:00
|
|
|
};
|
|
|
|
|
2010-06-08 08:31:44 +04:00
|
|
|
NS_IMPL_ADDREF_INHERITED(nsContentSubtreeIterator, nsContentIterator)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(nsContentSubtreeIterator, nsContentIterator)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(nsContentSubtreeIterator)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(nsContentIterator)
|
|
|
|
|
2014-04-25 20:49:00 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED(nsContentSubtreeIterator, nsContentIterator,
|
|
|
|
mRange)
|
2010-06-08 08:31:44 +04:00
|
|
|
|
2013-07-09 21:30:58 +04:00
|
|
|
void
|
|
|
|
nsContentSubtreeIterator::LastRelease()
|
|
|
|
{
|
|
|
|
mRange = nullptr;
|
|
|
|
nsContentIterator::LastRelease();
|
|
|
|
}
|
1999-03-01 11:17:18 +03:00
|
|
|
|
1999-02-14 12:15:13 +03:00
|
|
|
/******************************************************
|
|
|
|
* repository cruft
|
|
|
|
******************************************************/
|
|
|
|
|
2012-06-04 21:42:00 +04:00
|
|
|
already_AddRefed<nsIContentIterator>
|
|
|
|
NS_NewContentSubtreeIterator()
|
1999-02-14 12:15:13 +03:00
|
|
|
{
|
2012-06-04 21:42:00 +04:00
|
|
|
nsCOMPtr<nsIContentIterator> iter = new nsContentSubtreeIterator();
|
|
|
|
return iter.forget();
|
1999-02-14 12:15:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
1999-02-12 08:28:12 +03:00
|
|
|
/******************************************************
|
1999-02-14 12:15:13 +03:00
|
|
|
* Init routines
|
1999-02-12 08:28:12 +03:00
|
|
|
******************************************************/
|
|
|
|
|
1999-02-14 12:15:13 +03:00
|
|
|
|
2012-06-12 12:45:09 +04:00
|
|
|
nsresult
|
|
|
|
nsContentSubtreeIterator::Init(nsINode* aRoot)
|
1999-02-14 12:15:13 +03:00
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-12 12:45:09 +04:00
|
|
|
nsresult
|
|
|
|
nsContentSubtreeIterator::Init(nsIDOMRange* aRange)
|
1998-12-15 02:16:31 +03:00
|
|
|
{
|
2012-06-12 12:45:09 +04:00
|
|
|
MOZ_ASSERT(aRange);
|
1999-02-16 18:48:13 +03:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mIsDone = false;
|
1999-10-09 03:34:07 +04:00
|
|
|
|
2012-01-10 18:19:54 +04:00
|
|
|
mRange = static_cast<nsRange*>(aRange);
|
1999-03-01 11:17:18 +03:00
|
|
|
|
2012-06-12 12:45:09 +04:00
|
|
|
// get the start node and offset, convert to nsINode
|
|
|
|
mCommonParent = mRange->GetCommonAncestor();
|
|
|
|
nsINode* startParent = mRange->GetStartParent();
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t startOffset = mRange->StartOffset();
|
2012-06-12 12:45:09 +04:00
|
|
|
nsINode* endParent = mRange->GetEndParent();
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t endOffset = mRange->EndOffset();
|
2012-06-12 12:45:09 +04:00
|
|
|
MOZ_ASSERT(mCommonParent && startParent && endParent);
|
2012-07-05 11:45:08 +04:00
|
|
|
// Bug 767169
|
2012-08-22 19:56:38 +04:00
|
|
|
MOZ_ASSERT(uint32_t(startOffset) <= startParent->Length() &&
|
|
|
|
uint32_t(endOffset) <= endParent->Length());
|
2005-08-24 00:59:54 +04:00
|
|
|
|
1999-06-02 04:41:45 +04:00
|
|
|
// short circuit when start node == end node
|
2012-06-12 12:45:09 +04:00
|
|
|
if (startParent == endParent) {
|
|
|
|
nsINode* child = startParent->GetFirstChild();
|
|
|
|
|
|
|
|
if (!child || startOffset == endOffset) {
|
|
|
|
// Text node, empty container, or collapsed
|
1999-06-02 04:41:45 +04:00
|
|
|
MakeEmpty();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
2012-06-12 12:45:09 +04:00
|
|
|
|
2001-02-15 16:19:14 +03:00
|
|
|
// cache ancestors
|
2012-06-12 12:45:09 +04:00
|
|
|
nsContentUtils::GetAncestorsAndOffsets(endParent->AsDOMNode(), endOffset,
|
2002-06-26 00:03:06 +04:00
|
|
|
&mEndNodes, &mEndOffsets);
|
2001-02-15 16:19:14 +03:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
nsIContent* firstCandidate = nullptr;
|
|
|
|
nsIContent* lastCandidate = nullptr;
|
2008-03-17 07:56:22 +03:00
|
|
|
|
2012-06-12 12:45:09 +04:00
|
|
|
// find first node in range
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t offset = mRange->StartOffset();
|
2012-06-12 12:45:09 +04:00
|
|
|
|
|
|
|
nsINode* node;
|
|
|
|
if (!startParent->GetChildCount()) {
|
|
|
|
// no children, start at the node itself
|
|
|
|
node = startParent;
|
|
|
|
} else {
|
2012-06-14 10:47:03 +04:00
|
|
|
nsIContent* child = startParent->GetChildAt(offset);
|
2012-06-12 12:45:09 +04:00
|
|
|
if (!child) {
|
|
|
|
// offset after last child
|
|
|
|
node = startParent;
|
|
|
|
} else {
|
|
|
|
firstCandidate = child;
|
1999-02-16 18:48:13 +03:00
|
|
|
}
|
|
|
|
}
|
2004-01-24 03:46:17 +03:00
|
|
|
|
2012-06-12 12:45:09 +04:00
|
|
|
if (!firstCandidate) {
|
|
|
|
// then firstCandidate is next node after node
|
2012-06-14 10:47:03 +04:00
|
|
|
firstCandidate = GetNextSibling(node);
|
2012-06-12 12:45:09 +04:00
|
|
|
|
|
|
|
if (!firstCandidate) {
|
1999-03-01 11:17:18 +03:00
|
|
|
MakeEmpty();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
1999-02-16 18:48:13 +03:00
|
|
|
}
|
2012-06-12 12:45:09 +04:00
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
firstCandidate = GetDeepFirstChild(firstCandidate);
|
2012-06-12 12:45:09 +04:00
|
|
|
|
|
|
|
// confirm that this first possible contained node is indeed contained. Else
|
|
|
|
// we have a range that does not fully contain any node.
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool nodeBefore, nodeAfter;
|
2016-03-28 20:28:15 +03:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(
|
|
|
|
nsRange::CompareNodeToRange(firstCandidate, mRange, &nodeBefore, &nodeAfter));
|
2003-01-15 02:05:52 +03:00
|
|
|
|
2012-06-12 12:45:09 +04:00
|
|
|
if (nodeBefore || nodeAfter) {
|
1999-03-01 11:17:18 +03:00
|
|
|
MakeEmpty();
|
1999-02-16 18:48:13 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-06-12 12:45:09 +04:00
|
|
|
// cool, we have the first node in the range. Now we walk up its ancestors
|
|
|
|
// to find the most senior that is still in the range. That's the real first
|
|
|
|
// node.
|
|
|
|
mFirst = GetTopAncestorInRange(firstCandidate);
|
2004-01-24 03:46:17 +03:00
|
|
|
|
1999-03-01 11:17:18 +03:00
|
|
|
// now to find the last node
|
2012-06-12 12:45:09 +04:00
|
|
|
offset = mRange->EndOffset();
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t numChildren = endParent->GetChildCount();
|
1999-07-25 09:30:15 +04:00
|
|
|
|
2012-06-12 12:45:09 +04:00
|
|
|
if (offset > numChildren) {
|
2012-07-05 11:45:08 +04:00
|
|
|
// Can happen for text nodes
|
2012-06-12 12:45:09 +04:00
|
|
|
offset = numChildren;
|
1999-02-16 18:48:13 +03:00
|
|
|
}
|
2012-06-28 15:29:56 +04:00
|
|
|
if (!offset || !numChildren) {
|
2012-06-12 12:45:09 +04:00
|
|
|
node = endParent;
|
|
|
|
} else {
|
|
|
|
lastCandidate = endParent->GetChildAt(--offset);
|
|
|
|
NS_ASSERTION(lastCandidate,
|
|
|
|
"tree traversal trouble in nsContentSubtreeIterator::Init");
|
1999-02-16 18:48:13 +03:00
|
|
|
}
|
2012-06-12 12:45:09 +04:00
|
|
|
|
|
|
|
if (!lastCandidate) {
|
|
|
|
// then lastCandidate is prev node before node
|
2012-06-14 10:47:03 +04:00
|
|
|
lastCandidate = GetPrevSibling(node);
|
1999-03-01 11:17:18 +03:00
|
|
|
}
|
2012-06-12 12:45:09 +04:00
|
|
|
|
2012-06-28 15:29:56 +04:00
|
|
|
if (!lastCandidate) {
|
|
|
|
MakeEmpty();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
lastCandidate = GetDeepLastChild(lastCandidate);
|
2003-01-15 02:05:52 +03:00
|
|
|
|
2012-06-12 12:45:09 +04:00
|
|
|
// confirm that this last possible contained node is indeed contained. Else
|
|
|
|
// we have a range that does not fully contain any node.
|
|
|
|
|
2016-03-28 20:28:15 +03:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(
|
|
|
|
nsRange::CompareNodeToRange(lastCandidate, mRange, &nodeBefore, &nodeAfter));
|
2012-06-12 12:45:09 +04:00
|
|
|
|
|
|
|
if (nodeBefore || nodeAfter) {
|
1999-03-01 11:17:18 +03:00
|
|
|
MakeEmpty();
|
|
|
|
return NS_OK;
|
1999-02-16 18:48:13 +03:00
|
|
|
}
|
|
|
|
|
2012-06-12 12:45:09 +04:00
|
|
|
// cool, we have the last node in the range. Now we walk up its ancestors to
|
|
|
|
// find the most senior that is still in the range. That's the real first
|
|
|
|
// node.
|
|
|
|
mLast = GetTopAncestorInRange(lastCandidate);
|
|
|
|
|
1999-02-16 18:48:13 +03:00
|
|
|
mCurNode = mFirst;
|
1999-03-01 11:17:18 +03:00
|
|
|
|
1999-02-16 18:48:13 +03:00
|
|
|
return NS_OK;
|
1998-12-15 02:16:31 +03:00
|
|
|
}
|
1999-02-14 12:15:13 +03:00
|
|
|
|
|
|
|
/****************************************************************
|
|
|
|
* nsContentSubtreeIterator overrides of ContentIterator routines
|
|
|
|
****************************************************************/
|
|
|
|
|
2001-09-28 01:49:40 +04:00
|
|
|
// we can't call PositionAt in a subtree iterator...
|
2004-01-24 03:46:17 +03:00
|
|
|
void
|
|
|
|
nsContentSubtreeIterator::First()
|
2001-09-28 01:49:40 +04:00
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
mIsDone = mFirst == nullptr;
|
2004-01-24 03:46:17 +03:00
|
|
|
|
2001-09-28 01:49:40 +04:00
|
|
|
mCurNode = mFirst;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we can't call PositionAt in a subtree iterator...
|
2004-01-24 03:46:17 +03:00
|
|
|
void
|
|
|
|
nsContentSubtreeIterator::Last()
|
2001-09-28 01:49:40 +04:00
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
mIsDone = mLast == nullptr;
|
2004-01-24 03:46:17 +03:00
|
|
|
|
2001-09-28 01:49:40 +04:00
|
|
|
mCurNode = mLast;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-01-24 03:46:17 +03:00
|
|
|
void
|
|
|
|
nsContentSubtreeIterator::Next()
|
1999-02-14 12:15:13 +03:00
|
|
|
{
|
2012-06-12 12:45:09 +04:00
|
|
|
if (mIsDone || !mCurNode) {
|
2004-01-24 03:46:17 +03:00
|
|
|
return;
|
2012-06-12 12:45:09 +04:00
|
|
|
}
|
2004-01-24 03:46:17 +03:00
|
|
|
|
2012-06-12 12:45:09 +04:00
|
|
|
if (mCurNode == mLast) {
|
2011-10-17 18:59:28 +04:00
|
|
|
mIsDone = true;
|
2004-01-24 03:46:17 +03:00
|
|
|
return;
|
1999-03-01 11:17:18 +03:00
|
|
|
}
|
2004-03-10 02:57:56 +03:00
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
nsINode* nextNode = GetNextSibling(mCurNode);
|
2004-03-10 02:57:56 +03:00
|
|
|
NS_ASSERTION(nextNode, "No next sibling!?! This could mean deadlock!");
|
2004-01-24 03:46:17 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t i = mEndNodes.IndexOf(nextNode);
|
2012-06-12 12:45:09 +04:00
|
|
|
while (i != -1) {
|
2001-02-15 16:19:14 +03:00
|
|
|
// as long as we are finding ancestors of the endpoint of the range,
|
|
|
|
// dive down into their children
|
2011-09-27 11:54:58 +04:00
|
|
|
nextNode = nextNode->GetFirstChild();
|
2004-03-11 02:32:05 +03:00
|
|
|
NS_ASSERTION(nextNode, "Iterator error, expected a child node!");
|
2004-01-24 03:46:17 +03:00
|
|
|
|
2004-03-11 02:32:05 +03:00
|
|
|
// should be impossible to get a null pointer. If we went all the way
|
2012-06-12 12:45:09 +04:00
|
|
|
// down the child chain to the bottom without finding an interior node,
|
2001-02-15 16:19:14 +03:00
|
|
|
// then the previous node should have been the last, which was
|
|
|
|
// was tested at top of routine.
|
2004-03-10 02:57:56 +03:00
|
|
|
i = mEndNodes.IndexOf(nextNode);
|
2001-02-15 16:19:14 +03:00
|
|
|
}
|
|
|
|
|
2004-01-24 03:46:17 +03:00
|
|
|
mCurNode = nextNode;
|
|
|
|
|
2004-03-10 02:57:56 +03:00
|
|
|
// This shouldn't be needed, but since our selection code can put us
|
|
|
|
// in a situation where mLast is in generated content, we need this
|
|
|
|
// to stop the iterator when we've walked past past the last node!
|
2012-07-30 18:20:58 +04:00
|
|
|
mIsDone = mCurNode == nullptr;
|
1999-02-14 12:15:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-01-24 03:46:17 +03:00
|
|
|
void
|
|
|
|
nsContentSubtreeIterator::Prev()
|
1999-02-14 12:15:13 +03:00
|
|
|
{
|
2004-01-24 03:46:17 +03:00
|
|
|
// Prev should be optimized to use the mStartNodes, just as Next
|
|
|
|
// uses mEndNodes.
|
2012-06-12 12:45:09 +04:00
|
|
|
if (mIsDone || !mCurNode) {
|
2004-01-24 03:46:17 +03:00
|
|
|
return;
|
2012-06-12 12:45:09 +04:00
|
|
|
}
|
2004-01-24 03:46:17 +03:00
|
|
|
|
2012-06-12 12:45:09 +04:00
|
|
|
if (mCurNode == mFirst) {
|
2011-10-17 18:59:28 +04:00
|
|
|
mIsDone = true;
|
2004-01-24 03:46:17 +03:00
|
|
|
return;
|
1999-03-01 11:17:18 +03:00
|
|
|
}
|
2004-01-24 03:46:17 +03:00
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
// If any of these function calls return null, so will all succeeding ones,
|
|
|
|
// so mCurNode will wind up set to null.
|
2012-06-14 10:47:03 +04:00
|
|
|
nsINode* prevNode = GetDeepFirstChild(mCurNode);
|
2012-06-12 12:45:09 +04:00
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
prevNode = PrevNode(prevNode);
|
2004-01-24 03:46:17 +03:00
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
prevNode = GetDeepLastChild(prevNode);
|
2012-06-12 12:45:09 +04:00
|
|
|
|
|
|
|
mCurNode = GetTopAncestorInRange(prevNode);
|
2004-03-10 02:57:56 +03:00
|
|
|
|
|
|
|
// This shouldn't be needed, but since our selection code can put us
|
|
|
|
// in a situation where mFirst is in generated content, we need this
|
|
|
|
// to stop the iterator when we've walked past past the first node!
|
2012-07-30 18:20:58 +04:00
|
|
|
mIsDone = mCurNode == nullptr;
|
1999-02-14 12:15:13 +03:00
|
|
|
}
|
|
|
|
|
2004-01-24 03:46:17 +03:00
|
|
|
|
|
|
|
nsresult
|
2008-10-15 13:40:28 +04:00
|
|
|
nsContentSubtreeIterator::PositionAt(nsINode* aCurNode)
|
1999-03-23 13:30:24 +03:00
|
|
|
{
|
2004-01-24 03:46:17 +03:00
|
|
|
NS_ERROR("Not implemented!");
|
|
|
|
|
1999-03-23 13:30:24 +03:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
1999-03-01 11:17:18 +03:00
|
|
|
/****************************************************************
|
|
|
|
* nsContentSubtreeIterator helper routines
|
|
|
|
****************************************************************/
|
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
nsIContent*
|
2012-06-12 12:45:09 +04:00
|
|
|
nsContentSubtreeIterator::GetTopAncestorInRange(nsINode* aNode)
|
1999-03-01 11:17:18 +03:00
|
|
|
{
|
2012-10-09 16:31:24 +04:00
|
|
|
if (!aNode || !aNode->GetParentNode()) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2012-06-12 12:45:09 +04:00
|
|
|
}
|
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
// aNode has a parent, so it must be content.
|
|
|
|
nsIContent* content = aNode->AsContent();
|
|
|
|
|
1999-03-01 11:17:18 +03:00
|
|
|
// sanity check: aNode is itself in the range
|
2011-09-29 10:19:26 +04:00
|
|
|
bool nodeBefore, nodeAfter;
|
2012-06-12 12:45:09 +04:00
|
|
|
nsresult res = nsRange::CompareNodeToRange(aNode, mRange,
|
|
|
|
&nodeBefore, &nodeAfter);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(res) && !nodeBefore && !nodeAfter,
|
|
|
|
"aNode isn't in mRange, or something else weird happened");
|
|
|
|
if (NS_FAILED(res) || nodeBefore || nodeAfter) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2012-06-12 12:45:09 +04:00
|
|
|
}
|
2003-01-15 02:05:52 +03:00
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
while (content) {
|
|
|
|
nsIContent* parent = content->GetParent();
|
|
|
|
// content always has a parent. If its parent is the root, however --
|
|
|
|
// i.e., either it's not content, or it is content but its own parent is
|
|
|
|
// null -- then we're finished, since we don't go up to the root.
|
|
|
|
//
|
|
|
|
// We have to special-case this because CompareNodeToRange treats the root
|
|
|
|
// node differently -- see bug 765205.
|
2012-10-09 16:31:24 +04:00
|
|
|
if (!parent || !parent->GetParentNode()) {
|
2012-06-14 10:47:03 +04:00
|
|
|
return content;
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 14:57:30 +04:00
|
|
|
}
|
2016-03-28 20:28:15 +03:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(
|
|
|
|
nsRange::CompareNodeToRange(parent, mRange, &nodeBefore, &nodeAfter));
|
2003-01-15 02:05:52 +03:00
|
|
|
|
2012-06-12 12:45:09 +04:00
|
|
|
if (nodeBefore || nodeAfter) {
|
2012-06-14 10:47:03 +04:00
|
|
|
return content;
|
1999-03-01 11:17:18 +03:00
|
|
|
}
|
2012-06-14 10:47:03 +04:00
|
|
|
content = parent;
|
1999-03-01 11:17:18 +03:00
|
|
|
}
|
|
|
|
|
2013-06-29 05:38:30 +04:00
|
|
|
MOZ_CRASH("This should only be possible if aNode was null");
|
2012-06-12 12:45:09 +04:00
|
|
|
}
|