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 09:56:41 -08:00
|
|
|
#include "mozilla/dom/NodeIterator.h"
|
2008-07-21 17:38:52 -07:00
|
|
|
|
|
|
|
#include "nsIDOMNode.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"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsCOMPtr.h"
|
2013-02-28 09:56:42 -08:00
|
|
|
#include "mozilla/dom/NodeIteratorBinding.h"
|
2013-02-26 12:10:15 -08:00
|
|
|
|
2013-02-28 09:56:41 -08:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
2008-07-21 17:38:52 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* NodePointer implementation
|
|
|
|
*/
|
2013-02-28 09:56:41 -08:00
|
|
|
NodeIterator::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 09:56:41 -08:00
|
|
|
bool NodeIterator::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 09:56:41 -08:00
|
|
|
bool NodeIterator::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 09:56:41 -08:00
|
|
|
void NodeIterator::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 09:56:41 -08:00
|
|
|
bool NodeIterator::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 09:56:41 -08:00
|
|
|
void NodeIterator::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 09:56:41 -08:00
|
|
|
NodeIterator::NodeIterator(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
|
|
|
mPointer(mRoot, true)
|
2008-07-21 17:38:52 -07:00
|
|
|
{
|
|
|
|
aRoot->AddMutationObserver(this);
|
|
|
|
}
|
|
|
|
|
2013-02-28 09:56:41 -08:00
|
|
|
NodeIterator::~NodeIterator()
|
2008-07-21 17:38:52 -07:00
|
|
|
{
|
|
|
|
/* destructor code */
|
2013-03-09 23:58:25 -08:00
|
|
|
if (mRoot)
|
2008-07-21 17:38:52 -07:00
|
|
|
mRoot->RemoveMutationObserver(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* nsISupports and cycle collection stuff
|
|
|
|
*/
|
|
|
|
|
2013-08-01 18:29:05 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(NodeIterator)
|
|
|
|
|
2013-02-28 09:56:41 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(NodeIterator)
|
2013-03-09 23:58:25 -08:00
|
|
|
if (tmp->mRoot)
|
2008-07-21 17:38:52 -07:00
|
|
|
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 09:56:41 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(NodeIterator)
|
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 09:56:41 -08:00
|
|
|
// QueryInterface implementation for NodeIterator
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(NodeIterator)
|
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)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2013-02-28 09:56:41 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(NodeIterator)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(NodeIterator)
|
2008-07-21 17:38:52 -07:00
|
|
|
|
|
|
|
/* readonly attribute nsIDOMNode root; */
|
2013-02-28 09:56:41 -08:00
|
|
|
NS_IMETHODIMP NodeIterator::GetRoot(nsIDOMNode * *aRoot)
|
2008-07-21 17:38:52 -07:00
|
|
|
{
|
2013-02-28 09:56:42 -08:00
|
|
|
NS_ADDREF(*aRoot = Root()->AsDOMNode());
|
2008-07-21 17:38:52 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* readonly attribute unsigned long whatToShow; */
|
2013-02-28 09:56:41 -08:00
|
|
|
NS_IMETHODIMP NodeIterator::GetWhatToShow(uint32_t *aWhatToShow)
|
2008-07-21 17:38:52 -07:00
|
|
|
{
|
2013-02-28 09:56:42 -08:00
|
|
|
*aWhatToShow = WhatToShow();
|
2008-07-21 17:38:52 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* readonly attribute nsIDOMNodeFilter filter; */
|
2013-02-28 09:56:41 -08:00
|
|
|
NS_IMETHODIMP NodeIterator::GetFilter(nsIDOMNodeFilter **aFilter)
|
2008-07-21 17:38:52 -07:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aFilter);
|
|
|
|
|
2014-03-15 12:00:15 -07:00
|
|
|
*aFilter = mFilter.ToXPCOMCallback().take();
|
2008-07-21 17:38:52 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* nsIDOMNode nextNode () raises (DOMException); */
|
2013-02-28 09:56:41 -08:00
|
|
|
NS_IMETHODIMP NodeIterator::NextNode(nsIDOMNode **_retval)
|
2008-07-21 17:38:52 -07:00
|
|
|
{
|
2013-02-28 09:56:42 -08:00
|
|
|
return ImplNodeGetter(&NodeIterator::NextNode, _retval);
|
2008-07-21 17:38:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* nsIDOMNode previousNode () raises (DOMException); */
|
2013-02-28 09:56:41 -08:00
|
|
|
NS_IMETHODIMP NodeIterator::PreviousNode(nsIDOMNode **_retval)
|
2010-04-09 18:36:40 -07:00
|
|
|
{
|
2013-02-28 09:56:42 -08:00
|
|
|
return ImplNodeGetter(&NodeIterator::PreviousNode, _retval);
|
2010-04-09 18:36:40 -07:00
|
|
|
}
|
|
|
|
|
2013-02-28 09:56:42 -08:00
|
|
|
already_AddRefed<nsINode>
|
2013-02-28 09:56:41 -08:00
|
|
|
NodeIterator::NextOrPrevNode(NodePointer::MoveToMethodType aMove,
|
2013-02-28 09:56:42 -08:00
|
|
|
ErrorResult& aResult)
|
2008-07-21 17:38:52 -07:00
|
|
|
{
|
2013-03-09 23:58:25 -08:00
|
|
|
if (mInAcceptNode) {
|
2013-02-28 09:56:42 -08:00
|
|
|
aResult.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2008-07-21 17:38:52 -07:00
|
|
|
|
|
|
|
mWorkingPointer = mPointer;
|
|
|
|
|
2010-04-09 18:36:40 -07:00
|
|
|
struct AutoClear {
|
|
|
|
NodePointer* mPtr;
|
2014-09-01 17:49:25 -07:00
|
|
|
explicit AutoClear(NodePointer* ptr) : mPtr(ptr) {}
|
2010-04-09 18:36:40 -07:00
|
|
|
~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 09:56:42 -08:00
|
|
|
int16_t filtered = TestNode(testNode, aResult);
|
|
|
|
if (aResult.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2008-07-21 17:38:52 -07:00
|
|
|
|
|
|
|
if (filtered == nsIDOMNodeFilter::FILTER_ACCEPT) {
|
|
|
|
mPointer = mWorkingPointer;
|
2013-02-28 09:56:42 -08:00
|
|
|
return testNode.forget();
|
2008-07-21 17:38:52 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-28 09:56:42 -08:00
|
|
|
return nullptr;
|
2008-07-21 17:38:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* void detach (); */
|
2013-02-28 09:56:41 -08:00
|
|
|
NS_IMETHODIMP NodeIterator::Detach(void)
|
2008-07-21 17:38:52 -07:00
|
|
|
{
|
2013-03-09 23:58:25 -08:00
|
|
|
if (mRoot) {
|
|
|
|
mRoot->OwnerDoc()->WarnOnceAbout(nsIDocument::eNodeIteratorDetach);
|
2008-07-21 17:38:52 -07:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* readonly attribute nsIDOMNode referenceNode; */
|
2013-02-28 09:56:41 -08:00
|
|
|
NS_IMETHODIMP NodeIterator::GetReferenceNode(nsIDOMNode * *aRefNode)
|
2008-07-21 17:38:52 -07:00
|
|
|
{
|
2013-02-28 09:56:42 -08:00
|
|
|
nsCOMPtr<nsIDOMNode> node(do_QueryInterface(GetReferenceNode()));
|
|
|
|
node.forget(aRefNode);
|
2008-07-21 17:38:52 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* readonly attribute boolean pointerBeforeReferenceNode; */
|
2013-02-28 09:56:41 -08:00
|
|
|
NS_IMETHODIMP NodeIterator::GetPointerBeforeReferenceNode(bool *aBeforeNode)
|
2008-07-21 17:38:52 -07:00
|
|
|
{
|
2013-02-28 09:56:42 -08:00
|
|
|
*aBeforeNode = PointerBeforeReferenceNode();
|
2008-07-21 17:38:52 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* nsIMutationObserver interface
|
|
|
|
*/
|
|
|
|
|
2013-02-28 09:56:41 -08:00
|
|
|
void NodeIterator::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
|
|
|
}
|
2013-02-28 09:56:41 -08:00
|
|
|
|
2013-02-28 09:56:42 -08:00
|
|
|
JSObject*
|
2014-04-08 15:27:18 -07:00
|
|
|
NodeIterator::WrapObject(JSContext *cx)
|
2013-02-28 09:56:42 -08:00
|
|
|
{
|
Bug 991742 part 6. Remove the "aScope" argument of binding Wrap() methods. r=bholley
This patch was mostly generated with this command:
find . -name "*.h" -o -name "*.cpp" | xargs sed -e 's/Binding::Wrap(aCx, aScope, this/Binding::Wrap(aCx, this/' -e 's/Binding_workers::Wrap(aCx, aScope, this/Binding_workers::Wrap(aCx, this/' -e 's/Binding::Wrap(cx, scope, this/Binding::Wrap(cx, this/' -i ""
plus a few manual fixes to dom/bindings/Codegen.py, js/xpconnect/src/event_impl_gen.py, and a few C++ files that were not caught in the search-and-replace above.
2014-04-08 15:27:17 -07:00
|
|
|
return NodeIteratorBinding::Wrap(cx, this);
|
2013-02-28 09:56:42 -08:00
|
|
|
}
|
|
|
|
|
2013-02-28 09:56:41 -08:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|