|
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
|
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 nsIDOMNodeIterator |
|
9 */ |
|
10 |
|
11 #ifndef mozilla_dom_NodeIterator_h |
|
12 #define mozilla_dom_NodeIterator_h |
|
13 |
|
14 #include "nsIDOMNodeIterator.h" |
|
15 #include "nsTraversal.h" |
|
16 #include "nsCycleCollectionParticipant.h" |
|
17 #include "nsStubMutationObserver.h" |
|
18 |
|
19 class nsINode; |
|
20 class nsIDOMNode; |
|
21 |
|
22 namespace mozilla { |
|
23 namespace dom { |
|
24 |
|
25 class NodeIterator MOZ_FINAL : public nsIDOMNodeIterator, |
|
26 public nsTraversal, |
|
27 public nsStubMutationObserver |
|
28 { |
|
29 public: |
|
30 NS_DECL_CYCLE_COLLECTING_ISUPPORTS |
|
31 NS_DECL_NSIDOMNODEITERATOR |
|
32 |
|
33 NodeIterator(nsINode *aRoot, |
|
34 uint32_t aWhatToShow, |
|
35 const NodeFilterHolder &aFilter); |
|
36 virtual ~NodeIterator(); |
|
37 |
|
38 NS_DECL_NSIMUTATIONOBSERVER_CONTENTREMOVED |
|
39 |
|
40 NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(NodeIterator, nsIDOMNodeIterator) |
|
41 |
|
42 // WebIDL API |
|
43 nsINode* Root() const |
|
44 { |
|
45 return mRoot; |
|
46 } |
|
47 nsINode* GetReferenceNode() const |
|
48 { |
|
49 return mPointer.mNode; |
|
50 } |
|
51 bool PointerBeforeReferenceNode() const |
|
52 { |
|
53 return mPointer.mBeforeNode; |
|
54 } |
|
55 uint32_t WhatToShow() const |
|
56 { |
|
57 return mWhatToShow; |
|
58 } |
|
59 already_AddRefed<NodeFilter> GetFilter() |
|
60 { |
|
61 return mFilter.ToWebIDLCallback(); |
|
62 } |
|
63 already_AddRefed<nsINode> NextNode(ErrorResult& aResult) |
|
64 { |
|
65 return NextOrPrevNode(&NodePointer::MoveToNext, aResult); |
|
66 } |
|
67 already_AddRefed<nsINode> PreviousNode(ErrorResult& aResult) |
|
68 { |
|
69 return NextOrPrevNode(&NodePointer::MoveToPrevious, aResult); |
|
70 } |
|
71 // The XPCOM Detach() is fine for our purposes |
|
72 |
|
73 JSObject* WrapObject(JSContext *cx); |
|
74 |
|
75 private: |
|
76 struct NodePointer { |
|
77 NodePointer() : mNode(nullptr) {} |
|
78 NodePointer(nsINode *aNode, bool aBeforeNode); |
|
79 |
|
80 typedef bool (NodePointer::*MoveToMethodType)(nsINode*); |
|
81 bool MoveToNext(nsINode *aRoot); |
|
82 bool MoveToPrevious(nsINode *aRoot); |
|
83 |
|
84 bool MoveForward(nsINode *aRoot, nsINode *aNode); |
|
85 void MoveBackward(nsINode *aParent, nsINode *aNode); |
|
86 |
|
87 void AdjustAfterRemoval(nsINode *aRoot, nsINode *aContainer, nsIContent *aChild, nsIContent *aPreviousSibling); |
|
88 |
|
89 void Clear() { mNode = nullptr; } |
|
90 |
|
91 nsINode *mNode; |
|
92 bool mBeforeNode; |
|
93 }; |
|
94 |
|
95 // Implementation for some of our XPCOM getters |
|
96 typedef already_AddRefed<nsINode> (NodeIterator::*NodeGetter)(ErrorResult&); |
|
97 inline nsresult ImplNodeGetter(NodeGetter aGetter, nsIDOMNode** aRetval) |
|
98 { |
|
99 mozilla::ErrorResult rv; |
|
100 nsCOMPtr<nsINode> node = (this->*aGetter)(rv); |
|
101 if (rv.Failed()) { |
|
102 return rv.ErrorCode(); |
|
103 } |
|
104 *aRetval = node ? node.forget().take()->AsDOMNode() : nullptr; |
|
105 return NS_OK; |
|
106 } |
|
107 |
|
108 // Have to return a strong ref, because the act of testing the node can |
|
109 // remove it from the DOM so we're holding the only ref to it. |
|
110 already_AddRefed<nsINode> |
|
111 NextOrPrevNode(NodePointer::MoveToMethodType aMove, ErrorResult& aResult); |
|
112 |
|
113 NodePointer mPointer; |
|
114 NodePointer mWorkingPointer; |
|
115 }; |
|
116 |
|
117 } // namespace dom |
|
118 } // namespace mozilla |
|
119 |
|
120 #endif // mozilla_dom_NodeIterator_h |