|
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
|
2 /* vim: set ts=4 et sw=4 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 nsIDOMTreeWalker |
|
9 */ |
|
10 |
|
11 #ifndef mozilla_dom_TreeWalker_h |
|
12 #define mozilla_dom_TreeWalker_h |
|
13 |
|
14 #include "nsIDOMTreeWalker.h" |
|
15 #include "nsTraversal.h" |
|
16 #include "nsCOMPtr.h" |
|
17 #include "nsTArray.h" |
|
18 #include "nsCycleCollectionParticipant.h" |
|
19 |
|
20 class nsINode; |
|
21 class nsIDOMNode; |
|
22 |
|
23 namespace mozilla { |
|
24 namespace dom { |
|
25 |
|
26 class TreeWalker MOZ_FINAL : public nsIDOMTreeWalker, public nsTraversal |
|
27 { |
|
28 public: |
|
29 NS_DECL_CYCLE_COLLECTING_ISUPPORTS |
|
30 NS_DECL_NSIDOMTREEWALKER |
|
31 |
|
32 TreeWalker(nsINode *aRoot, |
|
33 uint32_t aWhatToShow, |
|
34 const NodeFilterHolder &aFilter); |
|
35 virtual ~TreeWalker(); |
|
36 |
|
37 NS_DECL_CYCLE_COLLECTION_CLASS(TreeWalker) |
|
38 |
|
39 // WebIDL API |
|
40 nsINode* Root() const |
|
41 { |
|
42 return mRoot; |
|
43 } |
|
44 uint32_t WhatToShow() const |
|
45 { |
|
46 return mWhatToShow; |
|
47 } |
|
48 already_AddRefed<NodeFilter> GetFilter() |
|
49 { |
|
50 return mFilter.ToWebIDLCallback(); |
|
51 } |
|
52 nsINode* CurrentNode() const |
|
53 { |
|
54 return mCurrentNode; |
|
55 } |
|
56 void SetCurrentNode(nsINode& aNode, ErrorResult& aResult); |
|
57 // All our traversal methods return strong refs because filtering can |
|
58 // remove nodes from the tree. |
|
59 already_AddRefed<nsINode> ParentNode(ErrorResult& aResult); |
|
60 already_AddRefed<nsINode> FirstChild(ErrorResult& aResult); |
|
61 already_AddRefed<nsINode> LastChild(ErrorResult& aResult); |
|
62 already_AddRefed<nsINode> PreviousSibling(ErrorResult& aResult); |
|
63 already_AddRefed<nsINode> NextSibling(ErrorResult& aResult); |
|
64 already_AddRefed<nsINode> PreviousNode(ErrorResult& aResult); |
|
65 already_AddRefed<nsINode> NextNode(ErrorResult& aResult); |
|
66 |
|
67 JSObject* WrapObject(JSContext *cx); |
|
68 |
|
69 private: |
|
70 nsCOMPtr<nsINode> mCurrentNode; |
|
71 |
|
72 /* |
|
73 * Implements FirstChild and LastChild which only vary in which direction |
|
74 * they search. |
|
75 * @param aReversed Controls whether we search forwards or backwards |
|
76 * @param aResult Whether we threw or not. |
|
77 * @returns The desired node. Null if no child is found |
|
78 */ |
|
79 already_AddRefed<nsINode> FirstChildInternal(bool aReversed, |
|
80 ErrorResult& aResult); |
|
81 |
|
82 /* |
|
83 * Implements NextSibling and PreviousSibling which only vary in which |
|
84 * direction they search. |
|
85 * @param aReversed Controls whether we search forwards or backwards |
|
86 * @param aResult Whether we threw or not. |
|
87 * @returns The desired node. Null if no child is found |
|
88 */ |
|
89 already_AddRefed<nsINode> NextSiblingInternal(bool aReversed, |
|
90 ErrorResult& aResult); |
|
91 |
|
92 // Implementation for our various XPCOM getters |
|
93 typedef already_AddRefed<nsINode> (TreeWalker::*NodeGetter)(ErrorResult&); |
|
94 inline nsresult ImplNodeGetter(NodeGetter aGetter, nsIDOMNode** aRetval) |
|
95 { |
|
96 mozilla::ErrorResult rv; |
|
97 nsCOMPtr<nsINode> node = (this->*aGetter)(rv); |
|
98 if (rv.Failed()) { |
|
99 return rv.ErrorCode(); |
|
100 } |
|
101 *aRetval = node ? node.forget().take()->AsDOMNode() : nullptr; |
|
102 return NS_OK; |
|
103 } |
|
104 }; |
|
105 |
|
106 } // namespace dom |
|
107 } // namespace mozilla |
|
108 |
|
109 #endif // mozilla_dom_TreeWalker_h |
|
110 |