1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/base/src/TreeWalker.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,110 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 1.5 +/* vim: set ts=4 et sw=4 tw=80: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +/* 1.11 + * Implementation of DOM Traversal's nsIDOMTreeWalker 1.12 + */ 1.13 + 1.14 +#ifndef mozilla_dom_TreeWalker_h 1.15 +#define mozilla_dom_TreeWalker_h 1.16 + 1.17 +#include "nsIDOMTreeWalker.h" 1.18 +#include "nsTraversal.h" 1.19 +#include "nsCOMPtr.h" 1.20 +#include "nsTArray.h" 1.21 +#include "nsCycleCollectionParticipant.h" 1.22 + 1.23 +class nsINode; 1.24 +class nsIDOMNode; 1.25 + 1.26 +namespace mozilla { 1.27 +namespace dom { 1.28 + 1.29 +class TreeWalker MOZ_FINAL : public nsIDOMTreeWalker, public nsTraversal 1.30 +{ 1.31 +public: 1.32 + NS_DECL_CYCLE_COLLECTING_ISUPPORTS 1.33 + NS_DECL_NSIDOMTREEWALKER 1.34 + 1.35 + TreeWalker(nsINode *aRoot, 1.36 + uint32_t aWhatToShow, 1.37 + const NodeFilterHolder &aFilter); 1.38 + virtual ~TreeWalker(); 1.39 + 1.40 + NS_DECL_CYCLE_COLLECTION_CLASS(TreeWalker) 1.41 + 1.42 + // WebIDL API 1.43 + nsINode* Root() const 1.44 + { 1.45 + return mRoot; 1.46 + } 1.47 + uint32_t WhatToShow() const 1.48 + { 1.49 + return mWhatToShow; 1.50 + } 1.51 + already_AddRefed<NodeFilter> GetFilter() 1.52 + { 1.53 + return mFilter.ToWebIDLCallback(); 1.54 + } 1.55 + nsINode* CurrentNode() const 1.56 + { 1.57 + return mCurrentNode; 1.58 + } 1.59 + void SetCurrentNode(nsINode& aNode, ErrorResult& aResult); 1.60 + // All our traversal methods return strong refs because filtering can 1.61 + // remove nodes from the tree. 1.62 + already_AddRefed<nsINode> ParentNode(ErrorResult& aResult); 1.63 + already_AddRefed<nsINode> FirstChild(ErrorResult& aResult); 1.64 + already_AddRefed<nsINode> LastChild(ErrorResult& aResult); 1.65 + already_AddRefed<nsINode> PreviousSibling(ErrorResult& aResult); 1.66 + already_AddRefed<nsINode> NextSibling(ErrorResult& aResult); 1.67 + already_AddRefed<nsINode> PreviousNode(ErrorResult& aResult); 1.68 + already_AddRefed<nsINode> NextNode(ErrorResult& aResult); 1.69 + 1.70 + JSObject* WrapObject(JSContext *cx); 1.71 + 1.72 +private: 1.73 + nsCOMPtr<nsINode> mCurrentNode; 1.74 + 1.75 + /* 1.76 + * Implements FirstChild and LastChild which only vary in which direction 1.77 + * they search. 1.78 + * @param aReversed Controls whether we search forwards or backwards 1.79 + * @param aResult Whether we threw or not. 1.80 + * @returns The desired node. Null if no child is found 1.81 + */ 1.82 + already_AddRefed<nsINode> FirstChildInternal(bool aReversed, 1.83 + ErrorResult& aResult); 1.84 + 1.85 + /* 1.86 + * Implements NextSibling and PreviousSibling which only vary in which 1.87 + * direction they search. 1.88 + * @param aReversed Controls whether we search forwards or backwards 1.89 + * @param aResult Whether we threw or not. 1.90 + * @returns The desired node. Null if no child is found 1.91 + */ 1.92 + already_AddRefed<nsINode> NextSiblingInternal(bool aReversed, 1.93 + ErrorResult& aResult); 1.94 + 1.95 + // Implementation for our various XPCOM getters 1.96 + typedef already_AddRefed<nsINode> (TreeWalker::*NodeGetter)(ErrorResult&); 1.97 + inline nsresult ImplNodeGetter(NodeGetter aGetter, nsIDOMNode** aRetval) 1.98 + { 1.99 + mozilla::ErrorResult rv; 1.100 + nsCOMPtr<nsINode> node = (this->*aGetter)(rv); 1.101 + if (rv.Failed()) { 1.102 + return rv.ErrorCode(); 1.103 + } 1.104 + *aRetval = node ? node.forget().take()->AsDOMNode() : nullptr; 1.105 + return NS_OK; 1.106 + } 1.107 +}; 1.108 + 1.109 +} // namespace dom 1.110 +} // namespace mozilla 1.111 + 1.112 +#endif // mozilla_dom_TreeWalker_h 1.113 +