2018-11-30 22:52:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2008-07-22 04:38:52 +04:00
|
|
|
*
|
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/. */
|
2008-07-22 04:38:52 +04:00
|
|
|
|
|
|
|
/*
|
2018-02-01 22:26:12 +03:00
|
|
|
* Implementation of DOM Traversal's NodeIterator
|
2008-07-22 04:38:52 +04:00
|
|
|
*/
|
|
|
|
|
2013-02-28 21:56:41 +04:00
|
|
|
#include "mozilla/dom/NodeIterator.h"
|
2008-07-22 04:38:52 +04:00
|
|
|
|
2012-07-27 18:03:27 +04:00
|
|
|
#include "nsError.h"
|
2008-07-22 04:38:52 +04:00
|
|
|
|
|
|
|
#include "nsIContent.h"
|
2019-01-02 16:05:23 +03:00
|
|
|
#include "mozilla/dom/Document.h"
|
2008-07-22 04:38:52 +04:00
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsCOMPtr.h"
|
2018-02-01 22:26:12 +03:00
|
|
|
#include "mozilla/dom/NodeFilterBinding.h"
|
2013-02-28 21:56:42 +04:00
|
|
|
#include "mozilla/dom/NodeIteratorBinding.h"
|
2013-02-27 00:10:15 +04:00
|
|
|
|
2020-11-04 20:04:01 +03:00
|
|
|
namespace mozilla::dom {
|
2008-07-22 04:38:52 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* NodePointer implementation
|
|
|
|
*/
|
2013-02-28 21:56:41 +04:00
|
|
|
NodeIterator::NodePointer::NodePointer(nsINode* aNode, bool aBeforeNode)
|
2008-07-22 04:38:52 +04:00
|
|
|
: mNode(aNode), mBeforeNode(aBeforeNode) {}
|
|
|
|
|
2013-02-28 21:56:41 +04:00
|
|
|
bool NodeIterator::NodePointer::MoveToNext(nsINode* aRoot) {
|
2011-10-17 18:59:28 +04:00
|
|
|
if (!mNode) return false;
|
2010-02-25 21:34:36 +03:00
|
|
|
|
2008-07-22 04:38:52 +04:00
|
|
|
if (mBeforeNode) {
|
2011-10-17 18:59:28 +04:00
|
|
|
mBeforeNode = false;
|
|
|
|
return true;
|
2008-07-22 04:38:52 +04:00
|
|
|
}
|
|
|
|
|
2010-07-22 02:05:20 +04:00
|
|
|
nsINode* child = mNode->GetFirstChild();
|
|
|
|
if (child) {
|
|
|
|
mNode = child;
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2010-07-22 02:05:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return MoveForward(aRoot, mNode);
|
2008-07-22 04:38:52 +04:00
|
|
|
}
|
|
|
|
|
2013-02-28 21:56:41 +04:00
|
|
|
bool NodeIterator::NodePointer::MoveToPrevious(nsINode* aRoot) {
|
2011-10-17 18:59:28 +04:00
|
|
|
if (!mNode) return false;
|
2010-02-25 21:34:36 +03:00
|
|
|
|
2008-07-22 04:38:52 +04:00
|
|
|
if (!mBeforeNode) {
|
2011-10-17 18:59:28 +04:00
|
|
|
mBeforeNode = true;
|
|
|
|
return true;
|
2008-07-22 04:38:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mNode == aRoot) return false;
|
|
|
|
|
2012-10-09 16:31:24 +04:00
|
|
|
MoveBackward(mNode->GetParentNode(), mNode->GetPreviousSibling());
|
2008-07-22 04:38:52 +04:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2008-07-22 04:38:52 +04:00
|
|
|
}
|
|
|
|
|
2013-02-28 21:56:41 +04:00
|
|
|
void NodeIterator::NodePointer::AdjustAfterRemoval(
|
|
|
|
nsINode* aRoot, nsINode* aContainer, nsIContent* aChild,
|
|
|
|
nsIContent* aPreviousSibling) {
|
2010-07-22 02:05:20 +04:00
|
|
|
// If mNode is null or the root there is nothing to do.
|
2010-03-02 08:06:29 +03:00
|
|
|
if (!mNode || mNode == aRoot) return;
|
2008-07-22 04:38:52 +04:00
|
|
|
|
|
|
|
// check if ancestor was removed
|
2019-07-12 16:10:28 +03:00
|
|
|
if (!mNode->IsInclusiveDescendantOf(aChild)) return;
|
2008-07-22 04:38:52 +04:00
|
|
|
|
|
|
|
if (mBeforeNode) {
|
2010-07-22 02:05:20 +04:00
|
|
|
// Try the next sibling
|
|
|
|
nsINode* nextSibling = aPreviousSibling ? aPreviousSibling->GetNextSibling()
|
|
|
|
: aContainer->GetFirstChild();
|
2008-07-22 04:38:52 +04:00
|
|
|
|
2010-07-22 02:05:20 +04:00
|
|
|
if (nextSibling) {
|
|
|
|
mNode = nextSibling;
|
|
|
|
return;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2010-07-22 02:05:20 +04:00
|
|
|
|
|
|
|
// Next try siblings of ancestors
|
|
|
|
if (MoveForward(aRoot, aContainer)) return;
|
|
|
|
|
|
|
|
// No suitable node was found so try going backwards
|
|
|
|
mBeforeNode = false;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2008-07-22 04:38:52 +04:00
|
|
|
|
2010-07-22 02:05:20 +04:00
|
|
|
MoveBackward(aContainer, aPreviousSibling);
|
2008-07-22 04:38:52 +04:00
|
|
|
}
|
|
|
|
|
2013-02-28 21:56:41 +04:00
|
|
|
bool NodeIterator::NodePointer::MoveForward(nsINode* aRoot, nsINode* aNode) {
|
2008-07-22 04:38:52 +04:00
|
|
|
while (1) {
|
2010-07-22 02:05:20 +04:00
|
|
|
if (aNode == aRoot) break;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2010-07-22 02:05:20 +04:00
|
|
|
nsINode* sibling = aNode->GetNextSibling();
|
|
|
|
if (sibling) {
|
|
|
|
mNode = sibling;
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2008-07-22 04:38:52 +04:00
|
|
|
}
|
2012-10-09 16:31:24 +04:00
|
|
|
aNode = aNode->GetParentNode();
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2008-07-22 04:38:52 +04:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2008-07-22 04:38:52 +04:00
|
|
|
}
|
|
|
|
|
2013-02-28 21:56:41 +04:00
|
|
|
void NodeIterator::NodePointer::MoveBackward(nsINode* aParent, nsINode* aNode) {
|
2010-07-22 02:05:20 +04:00
|
|
|
if (aNode) {
|
2008-07-22 04:38:52 +04:00
|
|
|
do {
|
2010-07-22 02:05:20 +04:00
|
|
|
mNode = aNode;
|
|
|
|
aNode = aNode->GetLastChild();
|
|
|
|
} while (aNode);
|
2008-07-22 04:38:52 +04:00
|
|
|
} else {
|
2010-07-22 02:05:20 +04:00
|
|
|
mNode = aParent;
|
2008-07-22 04:38:52 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Factories, constructors and destructors
|
|
|
|
*/
|
|
|
|
|
2013-02-28 21:56:41 +04:00
|
|
|
NodeIterator::NodeIterator(nsINode* aRoot, uint32_t aWhatToShow,
|
2018-02-01 22:26:12 +03:00
|
|
|
NodeFilter* aFilter)
|
|
|
|
: nsTraversal(aRoot, aWhatToShow, aFilter), mPointer(mRoot, true) {
|
2008-07-22 04:38:52 +04:00
|
|
|
aRoot->AddMutationObserver(this);
|
|
|
|
}
|
|
|
|
|
2013-02-28 21:56:41 +04:00
|
|
|
NodeIterator::~NodeIterator() {
|
2008-07-22 04:38:52 +04:00
|
|
|
/* destructor code */
|
|
|
|
if (mRoot) mRoot->RemoveMutationObserver(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* nsISupports and cycle collection stuff
|
|
|
|
*/
|
|
|
|
|
2013-08-02 05:29:05 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(NodeIterator)
|
|
|
|
|
2013-02-28 21:56:41 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(NodeIterator)
|
2008-07-22 04:38:52 +04:00
|
|
|
if (tmp->mRoot) tmp->mRoot->RemoveMutationObserver(tmp);
|
2012-11-15 11:32:40 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mRoot)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mFilter)
|
2008-07-22 04:38:52 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
2013-02-28 21:56:41 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(NodeIterator)
|
2012-11-15 11:32:40 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mRoot)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mFilter)
|
2008-07-22 04:38:52 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
2013-02-28 21:56:41 +04:00
|
|
|
// QueryInterface implementation for NodeIterator
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(NodeIterator)
|
2008-07-22 04:38:52 +04:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIMutationObserver)
|
2018-02-01 22:26:12 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
2008-07-22 04:38:52 +04:00
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2013-02-28 21:56:41 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(NodeIterator)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(NodeIterator)
|
2008-07-22 04:38:52 +04:00
|
|
|
|
2013-02-28 21:56:42 +04:00
|
|
|
already_AddRefed<nsINode> NodeIterator::NextOrPrevNode(
|
2013-02-28 21:56:41 +04:00
|
|
|
NodePointer::MoveToMethodType aMove, ErrorResult& aResult) {
|
2013-03-10 11:58:25 +04:00
|
|
|
if (mInAcceptNode) {
|
2013-02-28 21:56:42 +04:00
|
|
|
aResult.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return nullptr;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2008-07-22 04:38:52 +04:00
|
|
|
mWorkingPointer = mPointer;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2010-04-10 05:36:40 +04:00
|
|
|
struct AutoClear {
|
|
|
|
NodePointer* mPtr;
|
2014-09-02 04:49:25 +04:00
|
|
|
explicit AutoClear(NodePointer* ptr) : mPtr(ptr) {}
|
2010-04-10 05:36:40 +04:00
|
|
|
~AutoClear() { mPtr->Clear(); }
|
|
|
|
} ac(&mWorkingPointer);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2010-04-10 05:36:40 +04:00
|
|
|
while ((mWorkingPointer.*aMove)(mRoot)) {
|
2019-11-14 20:29:27 +03:00
|
|
|
nsCOMPtr<nsINode> testNode;
|
|
|
|
int16_t filtered = TestNode(mWorkingPointer.mNode, aResult, &testNode);
|
2013-02-28 21:56:42 +04:00
|
|
|
if (aResult.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2008-07-22 04:38:52 +04:00
|
|
|
|
2013-02-28 21:56:42 +04:00
|
|
|
if (filtered == NodeFilter_Binding::FILTER_ACCEPT) {
|
2008-07-22 04:38:52 +04:00
|
|
|
mPointer = mWorkingPointer;
|
2013-02-28 21:56:42 +04:00
|
|
|
return testNode.forget();
|
2008-07-22 04:38:52 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2008-07-22 04:38:52 +04:00
|
|
|
|
2013-02-28 21:56:42 +04:00
|
|
|
return nullptr;
|
2008-07-22 04:38:52 +04:00
|
|
|
}
|
|
|
|
|
2018-02-01 22:26:11 +03:00
|
|
|
void NodeIterator::Detach() {
|
2013-03-10 11:58:25 +04:00
|
|
|
if (mRoot) {
|
2020-11-23 19:08:03 +03:00
|
|
|
mRoot->OwnerDoc()->WarnOnceAbout(DeprecatedOperations::eNodeIteratorDetach);
|
2008-07-22 04:38:52 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* nsIMutationObserver interface
|
|
|
|
*/
|
|
|
|
|
2018-03-01 14:36:58 +03:00
|
|
|
void NodeIterator::ContentRemoved(nsIContent* aChild,
|
|
|
|
nsIContent* aPreviousSibling) {
|
|
|
|
nsINode* container = aChild->GetParentNode();
|
2008-07-22 04:38:52 +04:00
|
|
|
|
2010-07-22 02:05:20 +04:00
|
|
|
mPointer.AdjustAfterRemoval(mRoot, container, aChild, aPreviousSibling);
|
|
|
|
mWorkingPointer.AdjustAfterRemoval(mRoot, container, aChild,
|
|
|
|
aPreviousSibling);
|
2008-07-22 04:38:52 +04:00
|
|
|
}
|
2013-02-28 21:56:41 +04: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 17:13:32 +03:00
|
|
|
bool NodeIterator::WrapObject(JSContext* cx, JS::Handle<JSObject*> aGivenProto,
|
|
|
|
JS::MutableHandle<JSObject*> aReflector) {
|
2018-06-26 00:20:54 +03:00
|
|
|
return NodeIterator_Binding::Wrap(cx, this, aGivenProto, aReflector);
|
2013-02-28 21:56:42 +04:00
|
|
|
}
|
|
|
|
|
2020-11-04 20:04:01 +03:00
|
|
|
} // namespace mozilla::dom
|