michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* vim: set ts=4 et sw=4 tw=80: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * Implementation of DOM Traversal's nsIDOMTreeWalker michael@0: */ michael@0: michael@0: #ifndef mozilla_dom_TreeWalker_h michael@0: #define mozilla_dom_TreeWalker_h michael@0: michael@0: #include "nsIDOMTreeWalker.h" michael@0: #include "nsTraversal.h" michael@0: #include "nsCOMPtr.h" michael@0: #include "nsTArray.h" michael@0: #include "nsCycleCollectionParticipant.h" michael@0: michael@0: class nsINode; michael@0: class nsIDOMNode; michael@0: michael@0: namespace mozilla { michael@0: namespace dom { michael@0: michael@0: class TreeWalker MOZ_FINAL : public nsIDOMTreeWalker, public nsTraversal michael@0: { michael@0: public: michael@0: NS_DECL_CYCLE_COLLECTING_ISUPPORTS michael@0: NS_DECL_NSIDOMTREEWALKER michael@0: michael@0: TreeWalker(nsINode *aRoot, michael@0: uint32_t aWhatToShow, michael@0: const NodeFilterHolder &aFilter); michael@0: virtual ~TreeWalker(); michael@0: michael@0: NS_DECL_CYCLE_COLLECTION_CLASS(TreeWalker) michael@0: michael@0: // WebIDL API michael@0: nsINode* Root() const michael@0: { michael@0: return mRoot; michael@0: } michael@0: uint32_t WhatToShow() const michael@0: { michael@0: return mWhatToShow; michael@0: } michael@0: already_AddRefed GetFilter() michael@0: { michael@0: return mFilter.ToWebIDLCallback(); michael@0: } michael@0: nsINode* CurrentNode() const michael@0: { michael@0: return mCurrentNode; michael@0: } michael@0: void SetCurrentNode(nsINode& aNode, ErrorResult& aResult); michael@0: // All our traversal methods return strong refs because filtering can michael@0: // remove nodes from the tree. michael@0: already_AddRefed ParentNode(ErrorResult& aResult); michael@0: already_AddRefed FirstChild(ErrorResult& aResult); michael@0: already_AddRefed LastChild(ErrorResult& aResult); michael@0: already_AddRefed PreviousSibling(ErrorResult& aResult); michael@0: already_AddRefed NextSibling(ErrorResult& aResult); michael@0: already_AddRefed PreviousNode(ErrorResult& aResult); michael@0: already_AddRefed NextNode(ErrorResult& aResult); michael@0: michael@0: JSObject* WrapObject(JSContext *cx); michael@0: michael@0: private: michael@0: nsCOMPtr mCurrentNode; michael@0: michael@0: /* michael@0: * Implements FirstChild and LastChild which only vary in which direction michael@0: * they search. michael@0: * @param aReversed Controls whether we search forwards or backwards michael@0: * @param aResult Whether we threw or not. michael@0: * @returns The desired node. Null if no child is found michael@0: */ michael@0: already_AddRefed FirstChildInternal(bool aReversed, michael@0: ErrorResult& aResult); michael@0: michael@0: /* michael@0: * Implements NextSibling and PreviousSibling which only vary in which michael@0: * direction they search. michael@0: * @param aReversed Controls whether we search forwards or backwards michael@0: * @param aResult Whether we threw or not. michael@0: * @returns The desired node. Null if no child is found michael@0: */ michael@0: already_AddRefed NextSiblingInternal(bool aReversed, michael@0: ErrorResult& aResult); michael@0: michael@0: // Implementation for our various XPCOM getters michael@0: typedef already_AddRefed (TreeWalker::*NodeGetter)(ErrorResult&); michael@0: inline nsresult ImplNodeGetter(NodeGetter aGetter, nsIDOMNode** aRetval) michael@0: { michael@0: mozilla::ErrorResult rv; michael@0: nsCOMPtr node = (this->*aGetter)(rv); michael@0: if (rv.Failed()) { michael@0: return rv.ErrorCode(); michael@0: } michael@0: *aRetval = node ? node.forget().take()->AsDOMNode() : nullptr; michael@0: return NS_OK; michael@0: } michael@0: }; michael@0: michael@0: } // namespace dom michael@0: } // namespace mozilla michael@0: michael@0: #endif // mozilla_dom_TreeWalker_h michael@0: