2008-07-22 00:38:52 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
*
|
2012-05-21 11:12:37 +00: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-22 00:38:52 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Implementation of DOM Traversal's nsIDOMNodeIterator
|
|
|
|
*/
|
|
|
|
|
2013-02-28 17:56:41 +00:00
|
|
|
#include "mozilla/dom/NodeIterator.h"
|
2008-07-22 00:38:52 +00:00
|
|
|
|
|
|
|
#include "nsIDOMNode.h"
|
2012-07-27 14:03:27 +00:00
|
|
|
#include "nsError.h"
|
2008-07-22 00:38:52 +00:00
|
|
|
|
|
|
|
#include "nsIContent.h"
|
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsCOMPtr.h"
|
2013-02-28 17:56:42 +00:00
|
|
|
#include "mozilla/dom/NodeIteratorBinding.h"
|
2013-02-26 20:10:15 +00:00
|
|
|
|
2013-02-28 17:56:41 +00:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
2008-07-22 00:38:52 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* NodePointer implementation
|
|
|
|
*/
|
2013-02-28 17:56:41 +00:00
|
|
|
NodeIterator::NodePointer::NodePointer(nsINode *aNode, bool aBeforeNode) :
|
2008-07-22 00:38:52 +00:00
|
|
|
mNode(aNode),
|
|
|
|
mBeforeNode(aBeforeNode)
|
2010-02-25 18:34:36 +00:00
|
|
|
{
|
2008-07-22 00:38:52 +00:00
|
|
|
}
|
|
|
|
|
2013-02-28 17:56:41 +00:00
|
|
|
bool NodeIterator::NodePointer::MoveToNext(nsINode *aRoot)
|
2008-07-22 00:38:52 +00:00
|
|
|
{
|
2010-07-21 22:05:20 +00:00
|
|
|
if (!mNode)
|
2011-10-17 14:59:28 +00:00
|
|
|
return false;
|
2010-02-25 18:34:36 +00:00
|
|
|
|
2008-07-22 00:38:52 +00:00
|
|
|
if (mBeforeNode) {
|
2011-10-17 14:59:28 +00:00
|
|
|
mBeforeNode = false;
|
|
|
|
return true;
|
2008-07-22 00:38:52 +00:00
|
|
|
}
|
|
|
|
|
2010-07-21 22:05:20 +00:00
|
|
|
nsINode* child = mNode->GetFirstChild();
|
|
|
|
if (child) {
|
|
|
|
mNode = child;
|
2011-10-17 14:59:28 +00:00
|
|
|
return true;
|
2010-07-21 22:05:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return MoveForward(aRoot, mNode);
|
2008-07-22 00:38:52 +00:00
|
|
|
}
|
|
|
|
|
2013-02-28 17:56:41 +00:00
|
|
|
bool NodeIterator::NodePointer::MoveToPrevious(nsINode *aRoot)
|
2008-07-22 00:38:52 +00:00
|
|
|
{
|
2010-07-21 22:05:20 +00:00
|
|
|
if (!mNode)
|
2011-10-17 14:59:28 +00:00
|
|
|
return false;
|
2010-02-25 18:34:36 +00:00
|
|
|
|
2008-07-22 00:38:52 +00:00
|
|
|
if (!mBeforeNode) {
|
2011-10-17 14:59:28 +00:00
|
|
|
mBeforeNode = true;
|
|
|
|
return true;
|
2008-07-22 00:38:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mNode == aRoot)
|
2011-10-17 14:59:28 +00:00
|
|
|
return false;
|
2008-07-22 00:38:52 +00:00
|
|
|
|
2012-10-09 12:31:24 +00:00
|
|
|
MoveBackward(mNode->GetParentNode(), mNode->GetPreviousSibling());
|
2008-07-22 00:38:52 +00:00
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
return true;
|
2008-07-22 00:38:52 +00:00
|
|
|
}
|
|
|
|
|
2013-02-28 17:56:41 +00:00
|
|
|
void NodeIterator::NodePointer::AdjustAfterRemoval(nsINode *aRoot,
|
|
|
|
nsINode *aContainer,
|
|
|
|
nsIContent *aChild,
|
|
|
|
nsIContent *aPreviousSibling)
|
2008-07-22 00:38:52 +00:00
|
|
|
{
|
2010-07-21 22:05:20 +00:00
|
|
|
// If mNode is null or the root there is nothing to do.
|
2010-03-02 05:06:29 +00:00
|
|
|
if (!mNode || mNode == aRoot)
|
2008-07-22 00:38:52 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
// check if ancestor was removed
|
|
|
|
if (!nsContentUtils::ContentIsDescendantOf(mNode, aChild))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (mBeforeNode) {
|
|
|
|
|
2010-07-21 22:05:20 +00: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-22 00:38:52 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
// No suitable node was found so try going backwards
|
2011-10-17 14:59:28 +00:00
|
|
|
mBeforeNode = false;
|
2008-07-22 00:38:52 +00:00
|
|
|
}
|
|
|
|
|
2010-07-21 22:05:20 +00:00
|
|
|
MoveBackward(aContainer, aPreviousSibling);
|
2008-07-22 00:38:52 +00:00
|
|
|
}
|
|
|
|
|
2013-02-28 17:56:41 +00:00
|
|
|
bool NodeIterator::NodePointer::MoveForward(nsINode *aRoot, nsINode *aNode)
|
2008-07-22 00:38:52 +00:00
|
|
|
{
|
|
|
|
while (1) {
|
2010-07-21 22:05:20 +00:00
|
|
|
if (aNode == aRoot)
|
2008-07-22 00:38:52 +00:00
|
|
|
break;
|
|
|
|
|
2010-07-21 22:05:20 +00:00
|
|
|
nsINode *sibling = aNode->GetNextSibling();
|
|
|
|
if (sibling) {
|
|
|
|
mNode = sibling;
|
2011-10-17 14:59:28 +00:00
|
|
|
return true;
|
2008-07-22 00:38:52 +00:00
|
|
|
}
|
2012-10-09 12:31:24 +00:00
|
|
|
aNode = aNode->GetParentNode();
|
2008-07-22 00:38:52 +00:00
|
|
|
}
|
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
return false;
|
2008-07-22 00:38:52 +00:00
|
|
|
}
|
|
|
|
|
2013-02-28 17:56:41 +00:00
|
|
|
void NodeIterator::NodePointer::MoveBackward(nsINode *aParent, nsINode *aNode)
|
2008-07-22 00:38:52 +00:00
|
|
|
{
|
2010-07-21 22:05:20 +00:00
|
|
|
if (aNode) {
|
2008-07-22 00:38:52 +00:00
|
|
|
do {
|
2010-07-21 22:05:20 +00:00
|
|
|
mNode = aNode;
|
|
|
|
aNode = aNode->GetLastChild();
|
|
|
|
} while (aNode);
|
2008-07-22 00:38:52 +00:00
|
|
|
} else {
|
2010-07-21 22:05:20 +00:00
|
|
|
mNode = aParent;
|
2008-07-22 00:38:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Factories, constructors and destructors
|
|
|
|
*/
|
|
|
|
|
2013-02-28 17:56:41 +00:00
|
|
|
NodeIterator::NodeIterator(nsINode *aRoot,
|
|
|
|
uint32_t aWhatToShow,
|
|
|
|
const NodeFilterHolder &aFilter) :
|
2012-01-25 12:47:50 +00:00
|
|
|
nsTraversal(aRoot, aWhatToShow, aFilter),
|
2011-10-17 14:59:28 +00:00
|
|
|
mPointer(mRoot, true)
|
2008-07-22 00:38:52 +00:00
|
|
|
{
|
|
|
|
aRoot->AddMutationObserver(this);
|
|
|
|
}
|
|
|
|
|
2013-02-28 17:56:41 +00:00
|
|
|
NodeIterator::~NodeIterator()
|
2008-07-22 00:38:52 +00:00
|
|
|
{
|
|
|
|
/* destructor code */
|
2013-03-10 07:58:25 +00:00
|
|
|
if (mRoot)
|
2008-07-22 00:38:52 +00:00
|
|
|
mRoot->RemoveMutationObserver(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* nsISupports and cycle collection stuff
|
|
|
|
*/
|
|
|
|
|
2013-08-02 01:29:05 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(NodeIterator)
|
|
|
|
|
2013-02-28 17:56:41 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(NodeIterator)
|
2013-03-10 07:58:25 +00:00
|
|
|
if (tmp->mRoot)
|
2008-07-22 00:38:52 +00:00
|
|
|
tmp->mRoot->RemoveMutationObserver(tmp);
|
2012-11-15 07:32:40 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mRoot)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mFilter)
|
2008-07-22 00:38:52 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
2013-02-28 17:56:41 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(NodeIterator)
|
2012-11-15 07:32:40 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mRoot)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mFilter)
|
2008-07-22 00:38:52 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
2013-02-28 17:56:41 +00:00
|
|
|
// QueryInterface implementation for NodeIterator
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(NodeIterator)
|
2008-07-22 00:38:52 +00: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 17:56:41 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(NodeIterator)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(NodeIterator)
|
2008-07-22 00:38:52 +00:00
|
|
|
|
2013-02-28 17:56:41 +00:00
|
|
|
NS_IMETHODIMP NodeIterator::GetRoot(nsIDOMNode * *aRoot)
|
2008-07-22 00:38:52 +00:00
|
|
|
{
|
2013-02-28 17:56:42 +00:00
|
|
|
NS_ADDREF(*aRoot = Root()->AsDOMNode());
|
2008-07-22 00:38:52 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-02-28 17:56:41 +00:00
|
|
|
NS_IMETHODIMP NodeIterator::GetWhatToShow(uint32_t *aWhatToShow)
|
2008-07-22 00:38:52 +00:00
|
|
|
{
|
2013-02-28 17:56:42 +00:00
|
|
|
*aWhatToShow = WhatToShow();
|
2008-07-22 00:38:52 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-02-28 17:56:41 +00:00
|
|
|
NS_IMETHODIMP NodeIterator::GetFilter(nsIDOMNodeFilter **aFilter)
|
2008-07-22 00:38:52 +00:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aFilter);
|
|
|
|
|
2014-03-15 19:00:15 +00:00
|
|
|
*aFilter = mFilter.ToXPCOMCallback().take();
|
2008-07-22 00:38:52 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-02-28 17:56:41 +00:00
|
|
|
NS_IMETHODIMP NodeIterator::NextNode(nsIDOMNode **_retval)
|
2008-07-22 00:38:52 +00:00
|
|
|
{
|
2013-02-28 17:56:42 +00:00
|
|
|
return ImplNodeGetter(&NodeIterator::NextNode, _retval);
|
2008-07-22 00:38:52 +00:00
|
|
|
}
|
|
|
|
|
2013-02-28 17:56:41 +00:00
|
|
|
NS_IMETHODIMP NodeIterator::PreviousNode(nsIDOMNode **_retval)
|
2010-04-10 01:36:40 +00:00
|
|
|
{
|
2013-02-28 17:56:42 +00:00
|
|
|
return ImplNodeGetter(&NodeIterator::PreviousNode, _retval);
|
2010-04-10 01:36:40 +00:00
|
|
|
}
|
|
|
|
|
2013-02-28 17:56:42 +00:00
|
|
|
already_AddRefed<nsINode>
|
2013-02-28 17:56:41 +00:00
|
|
|
NodeIterator::NextOrPrevNode(NodePointer::MoveToMethodType aMove,
|
2013-02-28 17:56:42 +00:00
|
|
|
ErrorResult& aResult)
|
2008-07-22 00:38:52 +00:00
|
|
|
{
|
2013-03-10 07:58:25 +00:00
|
|
|
if (mInAcceptNode) {
|
2013-02-28 17:56:42 +00:00
|
|
|
aResult.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2008-07-22 00:38:52 +00:00
|
|
|
|
|
|
|
mWorkingPointer = mPointer;
|
|
|
|
|
2010-04-10 01:36:40 +00:00
|
|
|
struct AutoClear {
|
|
|
|
NodePointer* mPtr;
|
2014-09-02 00:49:25 +00:00
|
|
|
explicit AutoClear(NodePointer* ptr) : mPtr(ptr) {}
|
2010-04-10 01:36:40 +00:00
|
|
|
~AutoClear() { mPtr->Clear(); }
|
|
|
|
} ac(&mWorkingPointer);
|
|
|
|
|
|
|
|
while ((mWorkingPointer.*aMove)(mRoot)) {
|
2008-07-22 00:38:52 +00:00
|
|
|
nsCOMPtr<nsINode> testNode = mWorkingPointer.mNode;
|
2013-02-28 17:56:42 +00:00
|
|
|
int16_t filtered = TestNode(testNode, aResult);
|
|
|
|
if (aResult.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2008-07-22 00:38:52 +00:00
|
|
|
|
|
|
|
if (filtered == nsIDOMNodeFilter::FILTER_ACCEPT) {
|
|
|
|
mPointer = mWorkingPointer;
|
2013-02-28 17:56:42 +00:00
|
|
|
return testNode.forget();
|
2008-07-22 00:38:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-28 17:56:42 +00:00
|
|
|
return nullptr;
|
2008-07-22 00:38:52 +00:00
|
|
|
}
|
|
|
|
|
2013-02-28 17:56:41 +00:00
|
|
|
NS_IMETHODIMP NodeIterator::Detach(void)
|
2008-07-22 00:38:52 +00:00
|
|
|
{
|
2013-03-10 07:58:25 +00:00
|
|
|
if (mRoot) {
|
|
|
|
mRoot->OwnerDoc()->WarnOnceAbout(nsIDocument::eNodeIteratorDetach);
|
2008-07-22 00:38:52 +00:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-02-28 17:56:41 +00:00
|
|
|
NS_IMETHODIMP NodeIterator::GetReferenceNode(nsIDOMNode * *aRefNode)
|
2008-07-22 00:38:52 +00:00
|
|
|
{
|
2013-02-28 17:56:42 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> node(do_QueryInterface(GetReferenceNode()));
|
|
|
|
node.forget(aRefNode);
|
2008-07-22 00:38:52 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-02-28 17:56:41 +00:00
|
|
|
NS_IMETHODIMP NodeIterator::GetPointerBeforeReferenceNode(bool *aBeforeNode)
|
2008-07-22 00:38:52 +00:00
|
|
|
{
|
2013-02-28 17:56:42 +00:00
|
|
|
*aBeforeNode = PointerBeforeReferenceNode();
|
2008-07-22 00:38:52 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* nsIMutationObserver interface
|
|
|
|
*/
|
|
|
|
|
2013-02-28 17:56:41 +00:00
|
|
|
void NodeIterator::ContentRemoved(nsIDocument *aDocument,
|
|
|
|
nsIContent *aContainer,
|
|
|
|
nsIContent *aChild,
|
|
|
|
int32_t aIndexInContainer,
|
|
|
|
nsIContent *aPreviousSibling)
|
2008-07-22 00:38:52 +00:00
|
|
|
{
|
|
|
|
nsINode *container = NODE_FROM(aContainer, aDocument);
|
|
|
|
|
2010-07-21 22:05:20 +00:00
|
|
|
mPointer.AdjustAfterRemoval(mRoot, container, aChild, aPreviousSibling);
|
|
|
|
mWorkingPointer.AdjustAfterRemoval(mRoot, container, aChild, aPreviousSibling);
|
2008-07-22 00:38:52 +00:00
|
|
|
}
|
2013-02-28 17:56:41 +00:00
|
|
|
|
2015-01-08 21:56:42 +00:00
|
|
|
bool
|
Bug 1117172 part 2. Change the non-wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, Codegen.py, and
StructuredClone.cpp. The rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/WrapObject\((JSContext *\* *(?:aCx|cx)),(\s*)(JS::MutableHandle<JSObject\*> aReflector)/WrapObject(\1,\2JS::Handle<JSObject*> aGivenProto,\2\3/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx)), this, aReflector/\1, this, aGivenProto, aReflector/'
2015-03-19 14:13:32 +00:00
|
|
|
NodeIterator::WrapObject(JSContext *cx, JS::Handle<JSObject*> aGivenProto, JS::MutableHandle<JSObject*> aReflector)
|
2013-02-28 17:56:42 +00:00
|
|
|
{
|
Bug 1117172 part 2. Change the non-wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, Codegen.py, and
StructuredClone.cpp. The rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/WrapObject\((JSContext *\* *(?:aCx|cx)),(\s*)(JS::MutableHandle<JSObject\*> aReflector)/WrapObject(\1,\2JS::Handle<JSObject*> aGivenProto,\2\3/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx)), this, aReflector/\1, this, aGivenProto, aReflector/'
2015-03-19 14:13:32 +00:00
|
|
|
return NodeIteratorBinding::Wrap(cx, this, aGivenProto, aReflector);
|
2013-02-28 17:56:42 +00:00
|
|
|
}
|
|
|
|
|
2013-02-28 17:56:41 +00:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|