1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 * Implementation of DOM Traversal's NodeIterator
11 #ifndef mozilla_dom_NodeIterator_h
12 #define mozilla_dom_NodeIterator_h
14 #include "nsTraversal.h"
15 #include "nsCycleCollectionParticipant.h"
16 #include "nsStubMutationObserver.h"
20 namespace mozilla::dom
{
22 class NodeIterator final
: public nsStubMutationObserver
, public nsTraversal
{
24 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
26 NodeIterator(nsINode
* aRoot
, uint32_t aWhatToShow
, NodeFilter
* aFilter
);
28 NS_DECL_NSIMUTATIONOBSERVER_CONTENTREMOVED
30 NS_DECL_CYCLE_COLLECTION_CLASS(NodeIterator
)
33 nsINode
* Root() const { return mRoot
; }
34 nsINode
* GetReferenceNode() const { return mPointer
.mNode
; }
35 bool PointerBeforeReferenceNode() const { return mPointer
.mBeforeNode
; }
36 uint32_t WhatToShow() const { return mWhatToShow
; }
37 NodeFilter
* GetFilter() { return mFilter
; }
38 already_AddRefed
<nsINode
> NextNode(ErrorResult
& aResult
) {
39 return NextOrPrevNode(&NodePointer::MoveToNext
, aResult
);
41 already_AddRefed
<nsINode
> PreviousNode(ErrorResult
& aResult
) {
42 return NextOrPrevNode(&NodePointer::MoveToPrevious
, aResult
);
46 bool WrapObject(JSContext
* cx
, JS::Handle
<JSObject
*> aGivenProto
,
47 JS::MutableHandle
<JSObject
*> aReflector
);
50 virtual ~NodeIterator();
53 NodePointer() : mNode(nullptr), mBeforeNode(false) {}
54 NodePointer(nsINode
* aNode
, bool aBeforeNode
);
56 typedef bool (NodePointer::*MoveToMethodType
)(nsINode
*);
57 bool MoveToNext(nsINode
* aRoot
);
58 bool MoveToPrevious(nsINode
* aRoot
);
60 bool MoveForward(nsINode
* aRoot
, nsINode
* aNode
);
61 void MoveBackward(nsINode
* aParent
, nsINode
* aNode
);
63 void AdjustAfterRemoval(nsINode
* aRoot
, nsINode
* aContainer
,
64 nsIContent
* aChild
, nsIContent
* aPreviousSibling
);
66 void Clear() { mNode
= nullptr; }
72 // Have to return a strong ref, because the act of testing the node can
73 // remove it from the DOM so we're holding the only ref to it.
74 already_AddRefed
<nsINode
> NextOrPrevNode(NodePointer::MoveToMethodType aMove
,
75 ErrorResult
& aResult
);
78 NodePointer mWorkingPointer
;
81 } // namespace mozilla::dom
83 #endif // mozilla_dom_NodeIterator_h