2008-07-21 17:38:52 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
*
|
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/. */
|
2008-07-21 17:38:52 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Implementation of DOM Traversal's nsIDOMNodeIterator
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nsNodeIterator.h"
|
|
|
|
|
|
|
|
#include "nsIDOMNode.h"
|
|
|
|
#include "nsIDOMNodeFilter.h"
|
2012-07-27 07:03:27 -07:00
|
|
|
#include "nsError.h"
|
2008-07-21 17:38:52 -07:00
|
|
|
|
|
|
|
#include "nsIContent.h"
|
|
|
|
#include "nsIDocument.h"
|
2012-07-20 09:42:08 -07:00
|
|
|
#include "nsDOMClassInfoID.h"
|
2008-07-21 17:38:52 -07:00
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* NodePointer implementation
|
|
|
|
*/
|
|
|
|
nsNodeIterator::NodePointer::NodePointer(nsINode *aNode,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aBeforeNode) :
|
2008-07-21 17:38:52 -07:00
|
|
|
mNode(aNode),
|
|
|
|
mBeforeNode(aBeforeNode)
|
2010-02-25 10:34:36 -08:00
|
|
|
{
|
2008-07-21 17:38:52 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool nsNodeIterator::NodePointer::MoveToNext(nsINode *aRoot)
|
2008-07-21 17:38:52 -07:00
|
|
|
{
|
2010-07-21 15:05:20 -07:00
|
|
|
if (!mNode)
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2010-02-25 10:34:36 -08:00
|
|
|
|
2008-07-21 17:38:52 -07:00
|
|
|
if (mBeforeNode) {
|
2011-10-17 07:59:28 -07:00
|
|
|
mBeforeNode = false;
|
|
|
|
return true;
|
2008-07-21 17:38:52 -07:00
|
|
|
}
|
|
|
|
|
2010-07-21 15:05:20 -07:00
|
|
|
nsINode* child = mNode->GetFirstChild();
|
|
|
|
if (child) {
|
|
|
|
mNode = child;
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2010-07-21 15:05:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return MoveForward(aRoot, mNode);
|
2008-07-21 17:38:52 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool nsNodeIterator::NodePointer::MoveToPrevious(nsINode *aRoot)
|
2008-07-21 17:38:52 -07:00
|
|
|
{
|
2010-07-21 15:05:20 -07:00
|
|
|
if (!mNode)
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2010-02-25 10:34:36 -08:00
|
|
|
|
2008-07-21 17:38:52 -07:00
|
|
|
if (!mBeforeNode) {
|
2011-10-17 07:59:28 -07:00
|
|
|
mBeforeNode = true;
|
|
|
|
return true;
|
2008-07-21 17:38:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mNode == aRoot)
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2008-07-21 17:38:52 -07:00
|
|
|
|
2010-07-21 15:05:20 -07:00
|
|
|
MoveBackward(mNode->GetNodeParent(), mNode->GetPreviousSibling());
|
2008-07-21 17:38:52 -07:00
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2008-07-21 17:38:52 -07:00
|
|
|
}
|
|
|
|
|
2010-03-01 21:06:29 -08:00
|
|
|
void nsNodeIterator::NodePointer::AdjustAfterRemoval(nsINode *aRoot,
|
2010-02-25 10:34:36 -08:00
|
|
|
nsINode *aContainer,
|
|
|
|
nsIContent *aChild,
|
2010-07-21 15:05:20 -07:00
|
|
|
nsIContent *aPreviousSibling)
|
2008-07-21 17:38:52 -07:00
|
|
|
{
|
2010-07-21 15:05:20 -07:00
|
|
|
// If mNode is null or the root there is nothing to do.
|
2010-03-01 21:06:29 -08:00
|
|
|
if (!mNode || mNode == aRoot)
|
2008-07-21 17:38:52 -07:00
|
|
|
return;
|
|
|
|
|
|
|
|
// check if ancestor was removed
|
|
|
|
if (!nsContentUtils::ContentIsDescendantOf(mNode, aChild))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (mBeforeNode) {
|
|
|
|
|
2010-07-21 15:05:20 -07:00
|
|
|
// Try the next sibling
|
|
|
|
nsINode *nextSibling = aPreviousSibling ? aPreviousSibling->GetNextSibling()
|
|
|
|
: aContainer->GetFirstChild();
|
|
|
|
|
|
|
|
if (nextSibling) {
|
|
|
|
mNode = nextSibling;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next try siblings of ancestors
|
|
|
|
if (MoveForward(aRoot, aContainer))
|
2008-07-21 17:38:52 -07:00
|
|
|
return;
|
|
|
|
|
|
|
|
// No suitable node was found so try going backwards
|
2011-10-17 07:59:28 -07:00
|
|
|
mBeforeNode = false;
|
2008-07-21 17:38:52 -07:00
|
|
|
}
|
|
|
|
|
2010-07-21 15:05:20 -07:00
|
|
|
MoveBackward(aContainer, aPreviousSibling);
|
2008-07-21 17:38:52 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool nsNodeIterator::NodePointer::MoveForward(nsINode *aRoot, nsINode *aNode)
|
2008-07-21 17:38:52 -07:00
|
|
|
{
|
|
|
|
while (1) {
|
2010-07-21 15:05:20 -07:00
|
|
|
if (aNode == aRoot)
|
2008-07-21 17:38:52 -07:00
|
|
|
break;
|
|
|
|
|
2010-07-21 15:05:20 -07:00
|
|
|
nsINode *sibling = aNode->GetNextSibling();
|
|
|
|
if (sibling) {
|
|
|
|
mNode = sibling;
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2008-07-21 17:38:52 -07:00
|
|
|
}
|
2010-07-21 15:05:20 -07:00
|
|
|
aNode = aNode->GetNodeParent();
|
2008-07-21 17:38:52 -07:00
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2008-07-21 17:38:52 -07:00
|
|
|
}
|
|
|
|
|
2010-07-21 15:05:20 -07:00
|
|
|
void nsNodeIterator::NodePointer::MoveBackward(nsINode *aParent, nsINode *aNode)
|
2008-07-21 17:38:52 -07:00
|
|
|
{
|
2010-07-21 15:05:20 -07:00
|
|
|
if (aNode) {
|
2008-07-21 17:38:52 -07:00
|
|
|
do {
|
2010-07-21 15:05:20 -07:00
|
|
|
mNode = aNode;
|
|
|
|
aNode = aNode->GetLastChild();
|
|
|
|
} while (aNode);
|
2008-07-21 17:38:52 -07:00
|
|
|
} else {
|
2010-07-21 15:05:20 -07:00
|
|
|
mNode = aParent;
|
2008-07-21 17:38:52 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Factories, constructors and destructors
|
|
|
|
*/
|
|
|
|
|
|
|
|
nsNodeIterator::nsNodeIterator(nsINode *aRoot,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aWhatToShow,
|
2012-01-25 04:47:50 -08:00
|
|
|
nsIDOMNodeFilter *aFilter) :
|
|
|
|
nsTraversal(aRoot, aWhatToShow, aFilter),
|
2011-10-17 07:59:28 -07:00
|
|
|
mDetached(false),
|
|
|
|
mPointer(mRoot, true)
|
2008-07-21 17:38:52 -07:00
|
|
|
{
|
|
|
|
aRoot->AddMutationObserver(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsNodeIterator::~nsNodeIterator()
|
|
|
|
{
|
|
|
|
/* destructor code */
|
|
|
|
if (!mDetached && mRoot)
|
|
|
|
mRoot->RemoveMutationObserver(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* nsISupports and cycle collection stuff
|
|
|
|
*/
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(nsNodeIterator)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsNodeIterator)
|
|
|
|
if (!tmp->mDetached && tmp->mRoot)
|
|
|
|
tmp->mRoot->RemoveMutationObserver(tmp);
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mRoot)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mFilter)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsNodeIterator)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mRoot)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mFilter)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
2010-01-12 05:08:43 -08:00
|
|
|
DOMCI_DATA(NodeIterator, nsNodeIterator)
|
|
|
|
|
2008-07-21 17:38:52 -07:00
|
|
|
// QueryInterface implementation for nsNodeIterator
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsNodeIterator)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDOMNodeIterator)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIMutationObserver)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMNodeIterator)
|
2010-03-17 08:09:05 -07:00
|
|
|
NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(NodeIterator)
|
2008-07-21 17:38:52 -07:00
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsNodeIterator)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(nsNodeIterator)
|
|
|
|
|
|
|
|
/* readonly attribute nsIDOMNode root; */
|
|
|
|
NS_IMETHODIMP nsNodeIterator::GetRoot(nsIDOMNode * *aRoot)
|
|
|
|
{
|
|
|
|
if (mRoot)
|
|
|
|
return CallQueryInterface(mRoot, aRoot);
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
*aRoot = nullptr;
|
2008-07-21 17:38:52 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* readonly attribute unsigned long whatToShow; */
|
2012-08-22 08:56:38 -07:00
|
|
|
NS_IMETHODIMP nsNodeIterator::GetWhatToShow(uint32_t *aWhatToShow)
|
2008-07-21 17:38:52 -07:00
|
|
|
{
|
|
|
|
*aWhatToShow = mWhatToShow;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* readonly attribute nsIDOMNodeFilter filter; */
|
|
|
|
NS_IMETHODIMP nsNodeIterator::GetFilter(nsIDOMNodeFilter **aFilter)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aFilter);
|
|
|
|
|
2010-07-21 15:05:20 -07:00
|
|
|
NS_IF_ADDREF(*aFilter = mFilter);
|
2008-07-21 17:38:52 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* readonly attribute boolean expandEntityReferences; */
|
2011-09-28 23:19:26 -07:00
|
|
|
NS_IMETHODIMP nsNodeIterator::GetExpandEntityReferences(bool *aExpandEntityReferences)
|
2008-07-21 17:38:52 -07:00
|
|
|
{
|
2012-01-25 04:47:50 -08:00
|
|
|
*aExpandEntityReferences = false;
|
2008-07-21 17:38:52 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* nsIDOMNode nextNode () raises (DOMException); */
|
|
|
|
NS_IMETHODIMP nsNodeIterator::NextNode(nsIDOMNode **_retval)
|
|
|
|
{
|
2010-04-09 18:36:40 -07:00
|
|
|
return NextOrPrevNode(&NodePointer::MoveToNext, _retval);
|
2008-07-21 17:38:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* nsIDOMNode previousNode () raises (DOMException); */
|
|
|
|
NS_IMETHODIMP nsNodeIterator::PreviousNode(nsIDOMNode **_retval)
|
2010-04-09 18:36:40 -07:00
|
|
|
{
|
|
|
|
return NextOrPrevNode(&NodePointer::MoveToPrevious, _retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsNodeIterator::NextOrPrevNode(NodePointer::MoveToMethodType aMove,
|
|
|
|
nsIDOMNode **_retval)
|
2008-07-21 17:38:52 -07:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2012-08-22 08:56:38 -07:00
|
|
|
int16_t filtered;
|
2008-07-21 17:38:52 -07:00
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
*_retval = nullptr;
|
2008-07-21 17:38:52 -07:00
|
|
|
|
2010-04-16 11:45:13 -07:00
|
|
|
if (mDetached || mInAcceptNode)
|
2008-07-21 17:38:52 -07:00
|
|
|
return NS_ERROR_DOM_INVALID_STATE_ERR;
|
|
|
|
|
|
|
|
mWorkingPointer = mPointer;
|
|
|
|
|
2010-04-09 18:36:40 -07:00
|
|
|
struct AutoClear {
|
|
|
|
NodePointer* mPtr;
|
|
|
|
AutoClear(NodePointer* ptr) : mPtr(ptr) {}
|
|
|
|
~AutoClear() { mPtr->Clear(); }
|
|
|
|
} ac(&mWorkingPointer);
|
|
|
|
|
|
|
|
while ((mWorkingPointer.*aMove)(mRoot)) {
|
2008-07-21 17:38:52 -07:00
|
|
|
nsCOMPtr<nsINode> testNode = mWorkingPointer.mNode;
|
|
|
|
rv = TestNode(testNode, &filtered);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2010-04-09 18:36:40 -07:00
|
|
|
if (mDetached)
|
|
|
|
return NS_ERROR_DOM_INVALID_STATE_ERR;
|
|
|
|
|
2008-07-21 17:38:52 -07:00
|
|
|
if (filtered == nsIDOMNodeFilter::FILTER_ACCEPT) {
|
|
|
|
mPointer = mWorkingPointer;
|
|
|
|
return CallQueryInterface(testNode, _retval);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* void detach (); */
|
|
|
|
NS_IMETHODIMP nsNodeIterator::Detach(void)
|
|
|
|
{
|
|
|
|
if (!mDetached) {
|
|
|
|
mRoot->RemoveMutationObserver(this);
|
|
|
|
|
|
|
|
mPointer.Clear();
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
mDetached = true;
|
2008-07-21 17:38:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* readonly attribute nsIDOMNode referenceNode; */
|
|
|
|
NS_IMETHODIMP nsNodeIterator::GetReferenceNode(nsIDOMNode * *aRefNode)
|
|
|
|
{
|
|
|
|
if (mPointer.mNode)
|
|
|
|
return CallQueryInterface(mPointer.mNode, aRefNode);
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
*aRefNode = nullptr;
|
2008-07-21 17:38:52 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* readonly attribute boolean pointerBeforeReferenceNode; */
|
2011-09-28 23:19:26 -07:00
|
|
|
NS_IMETHODIMP nsNodeIterator::GetPointerBeforeReferenceNode(bool *aBeforeNode)
|
2008-07-21 17:38:52 -07:00
|
|
|
{
|
|
|
|
*aBeforeNode = mPointer.mBeforeNode;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* nsIMutationObserver interface
|
|
|
|
*/
|
|
|
|
|
|
|
|
void nsNodeIterator::ContentRemoved(nsIDocument *aDocument,
|
|
|
|
nsIContent *aContainer,
|
|
|
|
nsIContent *aChild,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aIndexInContainer,
|
2010-07-21 15:05:17 -07:00
|
|
|
nsIContent *aPreviousSibling)
|
2008-07-21 17:38:52 -07:00
|
|
|
{
|
|
|
|
nsINode *container = NODE_FROM(aContainer, aDocument);
|
|
|
|
|
2010-07-21 15:05:20 -07:00
|
|
|
mPointer.AdjustAfterRemoval(mRoot, container, aChild, aPreviousSibling);
|
|
|
|
mWorkingPointer.AdjustAfterRemoval(mRoot, container, aChild, aPreviousSibling);
|
2008-07-21 17:38:52 -07:00
|
|
|
}
|