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
|
|
|
|
*/
|
|
|
|
|
2013-02-28 11:41:30 -08:00
|
|
|
#include "nsNodeIterator.h"
|
2008-07-21 17:38:52 -07:00
|
|
|
|
|
|
|
#include "nsIDOMNode.h"
|
2013-02-28 11:41:30 -08:00
|
|
|
#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"
|
2013-02-28 11:41:30 -08:00
|
|
|
#include "mozilla/dom/NodeFilterBinding.h"
|
2013-02-26 12:10:15 -08:00
|
|
|
|
2013-02-28 11:41:30 -08:00
|
|
|
using namespace mozilla::dom;
|
2008-07-21 17:38:52 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* NodePointer implementation
|
|
|
|
*/
|
2013-02-28 11:41:30 -08:00
|
|
|
nsNodeIterator::NodePointer::NodePointer(nsINode *aNode,
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-02-28 11:41:30 -08: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
|
|
|
}
|
|
|
|
|
2013-02-28 11:41:30 -08: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
|
|
|
|
2012-10-09 05:31:24 -07:00
|
|
|
MoveBackward(mNode->GetParentNode(), 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
|
|
|
}
|
|
|
|
|
2013-02-28 11:41:30 -08:00
|
|
|
void nsNodeIterator::NodePointer::AdjustAfterRemoval(nsINode *aRoot,
|
|
|
|
nsINode *aContainer,
|
|
|
|
nsIContent *aChild,
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-02-28 11:41:30 -08: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
|
|
|
}
|
2012-10-09 05:31:24 -07:00
|
|
|
aNode = aNode->GetParentNode();
|
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
|
|
|
}
|
|
|
|
|
2013-02-28 11:41:30 -08: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
|
|
|
|
*/
|
|
|
|
|
2013-02-28 11:41:30 -08:00
|
|
|
nsNodeIterator::nsNodeIterator(nsINode *aRoot,
|
|
|
|
uint32_t aWhatToShow,
|
|
|
|
const NodeFilterHolder &aFilter) :
|
2012-01-25 04:47:50 -08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2013-02-28 11:41:30 -08:00
|
|
|
nsNodeIterator::~nsNodeIterator()
|
2008-07-21 17:38:52 -07:00
|
|
|
{
|
|
|
|
/* destructor code */
|
|
|
|
if (!mDetached && mRoot)
|
|
|
|
mRoot->RemoveMutationObserver(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* nsISupports and cycle collection stuff
|
|
|
|
*/
|
|
|
|
|
2013-02-28 11:41:30 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsNodeIterator)
|
2008-07-21 17:38:52 -07:00
|
|
|
if (!tmp->mDetached && tmp->mRoot)
|
|
|
|
tmp->mRoot->RemoveMutationObserver(tmp);
|
2012-11-14 23:32:40 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mRoot)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mFilter)
|
2008-07-21 17:38:52 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
2013-02-28 11:41:30 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsNodeIterator)
|
2012-11-14 23:32:40 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mRoot)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mFilter)
|
2008-07-21 17:38:52 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
2013-02-28 11:41:30 -08:00
|
|
|
DOMCI_DATA(NodeIterator, nsNodeIterator)
|
|
|
|
|
|
|
|
// QueryInterface implementation for nsNodeIterator
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsNodeIterator)
|
2008-07-21 17:38:52 -07:00
|
|
|
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
|
|
|
|
|
2013-02-28 11:41:30 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsNodeIterator)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(nsNodeIterator)
|
2008-07-21 17:38:52 -07:00
|
|
|
|
|
|
|
/* readonly attribute nsIDOMNode root; */
|
2013-02-28 11:41:30 -08:00
|
|
|
NS_IMETHODIMP nsNodeIterator::GetRoot(nsIDOMNode * *aRoot)
|
2008-07-21 17:38:52 -07:00
|
|
|
{
|
2013-02-28 11:41:30 -08:00
|
|
|
if (mRoot)
|
|
|
|
return CallQueryInterface(mRoot, aRoot);
|
|
|
|
|
|
|
|
*aRoot = nullptr;
|
|
|
|
|
2008-07-21 17:38:52 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* readonly attribute unsigned long whatToShow; */
|
2013-02-28 11:41:30 -08:00
|
|
|
NS_IMETHODIMP nsNodeIterator::GetWhatToShow(uint32_t *aWhatToShow)
|
2008-07-21 17:38:52 -07:00
|
|
|
{
|
2013-02-28 11:41:30 -08:00
|
|
|
*aWhatToShow = mWhatToShow;
|
2008-07-21 17:38:52 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* readonly attribute nsIDOMNodeFilter filter; */
|
2013-02-28 11:41:30 -08:00
|
|
|
NS_IMETHODIMP nsNodeIterator::GetFilter(nsIDOMNodeFilter **aFilter)
|
2008-07-21 17:38:52 -07:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aFilter);
|
|
|
|
|
2013-02-26 12:10:15 -08:00
|
|
|
*aFilter = mFilter.ToXPCOMCallback().get();
|
2008-07-21 17:38:52 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* nsIDOMNode nextNode () raises (DOMException); */
|
2013-02-28 11:41:30 -08:00
|
|
|
NS_IMETHODIMP nsNodeIterator::NextNode(nsIDOMNode **_retval)
|
2008-07-21 17:38:52 -07:00
|
|
|
{
|
2013-02-28 11:41:30 -08:00
|
|
|
return NextOrPrevNode(&NodePointer::MoveToNext, _retval);
|
2008-07-21 17:38:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* nsIDOMNode previousNode () raises (DOMException); */
|
2013-02-28 11:41:30 -08:00
|
|
|
NS_IMETHODIMP nsNodeIterator::PreviousNode(nsIDOMNode **_retval)
|
2010-04-09 18:36:40 -07:00
|
|
|
{
|
2013-02-28 11:41:30 -08:00
|
|
|
return NextOrPrevNode(&NodePointer::MoveToPrevious, _retval);
|
2010-04-09 18:36:40 -07:00
|
|
|
}
|
|
|
|
|
2013-02-28 11:41:30 -08:00
|
|
|
nsresult
|
|
|
|
nsNodeIterator::NextOrPrevNode(NodePointer::MoveToMethodType aMove,
|
|
|
|
nsIDOMNode **_retval)
|
2008-07-21 17:38:52 -07:00
|
|
|
{
|
2013-02-28 11:41:30 -08:00
|
|
|
nsresult rv;
|
|
|
|
int16_t filtered;
|
|
|
|
|
|
|
|
*_retval = nullptr;
|
|
|
|
|
|
|
|
if (mDetached || mInAcceptNode)
|
|
|
|
return NS_ERROR_DOM_INVALID_STATE_ERR;
|
2008-07-21 17:38:52 -07:00
|
|
|
|
|
|
|
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;
|
2013-02-28 11:41:30 -08:00
|
|
|
rv = TestNode(testNode, &filtered);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2008-07-21 17:38:52 -07:00
|
|
|
|
2013-02-28 11:41:30 -08:00
|
|
|
if (mDetached)
|
|
|
|
return NS_ERROR_DOM_INVALID_STATE_ERR;
|
2010-04-09 18:36:40 -07:00
|
|
|
|
2008-07-21 17:38:52 -07:00
|
|
|
if (filtered == nsIDOMNodeFilter::FILTER_ACCEPT) {
|
|
|
|
mPointer = mWorkingPointer;
|
2013-02-28 11:41:30 -08:00
|
|
|
return CallQueryInterface(testNode, _retval);
|
2008-07-21 17:38:52 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-28 11:41:30 -08:00
|
|
|
return NS_OK;
|
2008-07-21 17:38:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* void detach (); */
|
2013-02-28 11:41:30 -08:00
|
|
|
NS_IMETHODIMP nsNodeIterator::Detach(void)
|
2008-07-21 17:38:52 -07:00
|
|
|
{
|
|
|
|
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; */
|
2013-02-28 11:41:30 -08:00
|
|
|
NS_IMETHODIMP nsNodeIterator::GetReferenceNode(nsIDOMNode * *aRefNode)
|
2008-07-21 17:38:52 -07:00
|
|
|
{
|
2013-02-28 11:41:30 -08:00
|
|
|
if (mPointer.mNode)
|
|
|
|
return CallQueryInterface(mPointer.mNode, aRefNode);
|
|
|
|
|
|
|
|
*aRefNode = nullptr;
|
2008-07-21 17:38:52 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* readonly attribute boolean pointerBeforeReferenceNode; */
|
2013-02-28 11:41:30 -08:00
|
|
|
NS_IMETHODIMP nsNodeIterator::GetPointerBeforeReferenceNode(bool *aBeforeNode)
|
2008-07-21 17:38:52 -07:00
|
|
|
{
|
2013-02-28 11:41:30 -08:00
|
|
|
*aBeforeNode = mPointer.mBeforeNode;
|
2008-07-21 17:38:52 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* nsIMutationObserver interface
|
|
|
|
*/
|
|
|
|
|
2013-02-28 11:41:30 -08:00
|
|
|
void nsNodeIterator::ContentRemoved(nsIDocument *aDocument,
|
|
|
|
nsIContent *aContainer,
|
|
|
|
nsIContent *aChild,
|
|
|
|
int32_t aIndexInContainer,
|
|
|
|
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
|
|
|
}
|