TreeWalker.h (2801B)
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=4 et sw=2 tw=80: */ 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 TreeWalker 9 */ 10 11 #ifndef mozilla_dom_TreeWalker_h 12 #define mozilla_dom_TreeWalker_h 13 14 #include "nsCOMPtr.h" 15 #include "nsCycleCollectionParticipant.h" 16 #include "nsISupports.h" 17 #include "nsTArray.h" 18 #include "nsTraversal.h" 19 20 class nsINode; 21 22 namespace mozilla::dom { 23 24 class TreeWalker final : public nsISupports, public nsTraversal { 25 virtual ~TreeWalker(); 26 27 public: 28 NS_DECL_CYCLE_COLLECTING_ISUPPORTS 29 30 TreeWalker(nsINode* aRoot, uint32_t aWhatToShow, NodeFilter* aFilter); 31 32 NS_DECL_CYCLE_COLLECTION_CLASS(TreeWalker) 33 34 // WebIDL API 35 nsINode* Root() const { return mRoot; } 36 uint32_t WhatToShow() const { return mWhatToShow; } 37 NodeFilter* GetFilter() { return mFilter; } 38 nsINode* CurrentNode() const { return mCurrentNode; } 39 void SetCurrentNode(nsINode& aNode, ErrorResult& aResult); 40 // All our traversal methods return strong refs because filtering can 41 // remove nodes from the tree. 42 already_AddRefed<nsINode> ParentNode(ErrorResult& aResult); 43 already_AddRefed<nsINode> FirstChild(ErrorResult& aResult); 44 already_AddRefed<nsINode> LastChild(ErrorResult& aResult); 45 already_AddRefed<nsINode> PreviousSibling(ErrorResult& aResult); 46 already_AddRefed<nsINode> NextSibling(ErrorResult& aResult); 47 already_AddRefed<nsINode> PreviousNode(ErrorResult& aResult); 48 already_AddRefed<nsINode> NextNode(ErrorResult& aResult); 49 50 bool WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto, 51 JS::MutableHandle<JSObject*> aReflector); 52 53 private: 54 nsCOMPtr<nsINode> mCurrentNode; 55 56 /* 57 * Implements FirstChild and LastChild which only vary in which direction 58 * they search. 59 * @param aReversed Controls whether we search forwards or backwards 60 * @param aResult Whether we threw or not. 61 * @returns The desired node. Null if no child is found 62 */ 63 already_AddRefed<nsINode> FirstChildInternal(bool aReversed, 64 ErrorResult& aResult); 65 66 /* 67 * Implements NextSibling and PreviousSibling which only vary in which 68 * direction they search. 69 * @param aReversed Controls whether we search forwards or backwards 70 * @param aResult Whether we threw or not. 71 * @returns The desired node. Null if no child is found 72 */ 73 already_AddRefed<nsINode> NextSiblingInternal(bool aReversed, 74 ErrorResult& aResult); 75 }; 76 77 } // namespace mozilla::dom 78 79 #endif // mozilla_dom_TreeWalker_h