NodeIterator.h (2596B)
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- 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/. */ 6 7 /* 8 * Implementation of DOM Traversal's NodeIterator 9 */ 10 11 #ifndef mozilla_dom_NodeIterator_h 12 #define mozilla_dom_NodeIterator_h 13 14 #include "nsCycleCollectionParticipant.h" 15 #include "nsStubMutationObserver.h" 16 #include "nsTraversal.h" 17 18 class nsINode; 19 20 namespace mozilla::dom { 21 22 class NodeIterator final : public nsStubMutationObserver, public nsTraversal { 23 public: 24 NS_DECL_CYCLE_COLLECTING_ISUPPORTS 25 26 NodeIterator(nsINode* aRoot, uint32_t aWhatToShow, NodeFilter* aFilter); 27 28 NS_DECL_NSIMUTATIONOBSERVER_CONTENTREMOVED 29 30 NS_DECL_CYCLE_COLLECTION_CLASS(NodeIterator) 31 32 // WebIDL API 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); 40 } 41 already_AddRefed<nsINode> PreviousNode(ErrorResult& aResult) { 42 return NextOrPrevNode(&NodePointer::MoveToPrevious, aResult); 43 } 44 void Detach(); 45 46 bool WrapObject(JSContext* cx, JS::Handle<JSObject*> aGivenProto, 47 JS::MutableHandle<JSObject*> aReflector); 48 49 private: 50 virtual ~NodeIterator(); 51 52 struct NodePointer { 53 NodePointer() : mNode(nullptr), mBeforeNode(false) {} 54 NodePointer(nsINode* aNode, bool aBeforeNode); 55 56 typedef bool (NodePointer::*MoveToMethodType)(nsINode*); 57 bool MoveToNext(nsINode* aRoot); 58 bool MoveToPrevious(nsINode* aRoot); 59 60 bool MoveForward(nsINode* aRoot, nsINode* aNode); 61 void MoveBackward(nsINode* aParent, nsINode* aNode); 62 63 void AdjustForRemoval(nsINode* aRoot, nsINode* aContainer, 64 nsIContent* aChild); 65 66 void Clear() { mNode = nullptr; } 67 68 nsINode* mNode; 69 bool mBeforeNode; 70 }; 71 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); 76 77 NodePointer mPointer; 78 NodePointer mWorkingPointer; 79 }; 80 81 } // namespace mozilla::dom 82 83 #endif // mozilla_dom_NodeIterator_h