/src/mozilla-central/dom/base/NodeIterator.cpp
Line | Count | Source (jump to first uncovered line) |
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 NodeIterator |
9 | | */ |
10 | | |
11 | | #include "mozilla/dom/NodeIterator.h" |
12 | | |
13 | | #include "nsError.h" |
14 | | |
15 | | #include "nsIContent.h" |
16 | | #include "nsIDocument.h" |
17 | | #include "nsContentUtils.h" |
18 | | #include "nsCOMPtr.h" |
19 | | #include "mozilla/dom/NodeFilterBinding.h" |
20 | | #include "mozilla/dom/NodeIteratorBinding.h" |
21 | | |
22 | | namespace mozilla { |
23 | | namespace dom { |
24 | | |
25 | | /* |
26 | | * NodePointer implementation |
27 | | */ |
28 | | NodeIterator::NodePointer::NodePointer(nsINode *aNode, bool aBeforeNode) : |
29 | | mNode(aNode), |
30 | | mBeforeNode(aBeforeNode) |
31 | 0 | { |
32 | 0 | } |
33 | | |
34 | | bool NodeIterator::NodePointer::MoveToNext(nsINode *aRoot) |
35 | 0 | { |
36 | 0 | if (!mNode) |
37 | 0 | return false; |
38 | 0 | |
39 | 0 | if (mBeforeNode) { |
40 | 0 | mBeforeNode = false; |
41 | 0 | return true; |
42 | 0 | } |
43 | 0 | |
44 | 0 | nsINode* child = mNode->GetFirstChild(); |
45 | 0 | if (child) { |
46 | 0 | mNode = child; |
47 | 0 | return true; |
48 | 0 | } |
49 | 0 | |
50 | 0 | return MoveForward(aRoot, mNode); |
51 | 0 | } |
52 | | |
53 | | bool NodeIterator::NodePointer::MoveToPrevious(nsINode *aRoot) |
54 | 0 | { |
55 | 0 | if (!mNode) |
56 | 0 | return false; |
57 | 0 | |
58 | 0 | if (!mBeforeNode) { |
59 | 0 | mBeforeNode = true; |
60 | 0 | return true; |
61 | 0 | } |
62 | 0 | |
63 | 0 | if (mNode == aRoot) |
64 | 0 | return false; |
65 | 0 | |
66 | 0 | MoveBackward(mNode->GetParentNode(), mNode->GetPreviousSibling()); |
67 | 0 |
|
68 | 0 | return true; |
69 | 0 | } |
70 | | |
71 | | void NodeIterator::NodePointer::AdjustAfterRemoval(nsINode *aRoot, |
72 | | nsINode *aContainer, |
73 | | nsIContent *aChild, |
74 | | nsIContent *aPreviousSibling) |
75 | 0 | { |
76 | 0 | // If mNode is null or the root there is nothing to do. |
77 | 0 | if (!mNode || mNode == aRoot) |
78 | 0 | return; |
79 | 0 | |
80 | 0 | // check if ancestor was removed |
81 | 0 | if (!nsContentUtils::ContentIsDescendantOf(mNode, aChild)) |
82 | 0 | return; |
83 | 0 | |
84 | 0 | if (mBeforeNode) { |
85 | 0 |
|
86 | 0 | // Try the next sibling |
87 | 0 | nsINode *nextSibling = aPreviousSibling ? aPreviousSibling->GetNextSibling() |
88 | 0 | : aContainer->GetFirstChild(); |
89 | 0 |
|
90 | 0 | if (nextSibling) { |
91 | 0 | mNode = nextSibling; |
92 | 0 | return; |
93 | 0 | } |
94 | 0 | |
95 | 0 | // Next try siblings of ancestors |
96 | 0 | if (MoveForward(aRoot, aContainer)) |
97 | 0 | return; |
98 | 0 | |
99 | 0 | // No suitable node was found so try going backwards |
100 | 0 | mBeforeNode = false; |
101 | 0 | } |
102 | 0 |
|
103 | 0 | MoveBackward(aContainer, aPreviousSibling); |
104 | 0 | } |
105 | | |
106 | | bool NodeIterator::NodePointer::MoveForward(nsINode *aRoot, nsINode *aNode) |
107 | 0 | { |
108 | 0 | while (1) { |
109 | 0 | if (aNode == aRoot) |
110 | 0 | break; |
111 | 0 | |
112 | 0 | nsINode *sibling = aNode->GetNextSibling(); |
113 | 0 | if (sibling) { |
114 | 0 | mNode = sibling; |
115 | 0 | return true; |
116 | 0 | } |
117 | 0 | aNode = aNode->GetParentNode(); |
118 | 0 | } |
119 | 0 |
|
120 | 0 | return false; |
121 | 0 | } |
122 | | |
123 | | void NodeIterator::NodePointer::MoveBackward(nsINode *aParent, nsINode *aNode) |
124 | 0 | { |
125 | 0 | if (aNode) { |
126 | 0 | do { |
127 | 0 | mNode = aNode; |
128 | 0 | aNode = aNode->GetLastChild(); |
129 | 0 | } while (aNode); |
130 | 0 | } else { |
131 | 0 | mNode = aParent; |
132 | 0 | } |
133 | 0 | } |
134 | | |
135 | | /* |
136 | | * Factories, constructors and destructors |
137 | | */ |
138 | | |
139 | | NodeIterator::NodeIterator(nsINode *aRoot, |
140 | | uint32_t aWhatToShow, |
141 | | NodeFilter* aFilter) : |
142 | | nsTraversal(aRoot, aWhatToShow, aFilter), |
143 | | mPointer(mRoot, true) |
144 | 0 | { |
145 | 0 | aRoot->AddMutationObserver(this); |
146 | 0 | } |
147 | | |
148 | | NodeIterator::~NodeIterator() |
149 | 0 | { |
150 | 0 | /* destructor code */ |
151 | 0 | if (mRoot) |
152 | 0 | mRoot->RemoveMutationObserver(this); |
153 | 0 | } |
154 | | |
155 | | /* |
156 | | * nsISupports and cycle collection stuff |
157 | | */ |
158 | | |
159 | | NS_IMPL_CYCLE_COLLECTION_CLASS(NodeIterator) |
160 | | |
161 | 0 | NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(NodeIterator) |
162 | 0 | if (tmp->mRoot) |
163 | 0 | tmp->mRoot->RemoveMutationObserver(tmp); |
164 | 0 | NS_IMPL_CYCLE_COLLECTION_UNLINK(mRoot) |
165 | 0 | NS_IMPL_CYCLE_COLLECTION_UNLINK(mFilter) |
166 | 0 | NS_IMPL_CYCLE_COLLECTION_UNLINK_END |
167 | 0 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(NodeIterator) |
168 | 0 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mRoot) |
169 | 0 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mFilter) |
170 | 0 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END |
171 | | |
172 | | // QueryInterface implementation for NodeIterator |
173 | 0 | NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(NodeIterator) |
174 | 0 | NS_INTERFACE_MAP_ENTRY(nsIMutationObserver) |
175 | 0 | NS_INTERFACE_MAP_ENTRY(nsISupports) |
176 | 0 | NS_INTERFACE_MAP_END |
177 | | |
178 | | NS_IMPL_CYCLE_COLLECTING_ADDREF(NodeIterator) |
179 | | NS_IMPL_CYCLE_COLLECTING_RELEASE(NodeIterator) |
180 | | |
181 | | already_AddRefed<nsINode> |
182 | | NodeIterator::NextOrPrevNode(NodePointer::MoveToMethodType aMove, |
183 | | ErrorResult& aResult) |
184 | 0 | { |
185 | 0 | if (mInAcceptNode) { |
186 | 0 | aResult.Throw(NS_ERROR_DOM_INVALID_STATE_ERR); |
187 | 0 | return nullptr; |
188 | 0 | } |
189 | 0 | |
190 | 0 | mWorkingPointer = mPointer; |
191 | 0 |
|
192 | 0 | struct AutoClear { |
193 | 0 | NodePointer* mPtr; |
194 | 0 | explicit AutoClear(NodePointer* ptr) : mPtr(ptr) {} |
195 | 0 | ~AutoClear() { mPtr->Clear(); } |
196 | 0 | } ac(&mWorkingPointer); |
197 | 0 |
|
198 | 0 | while ((mWorkingPointer.*aMove)(mRoot)) { |
199 | 0 | nsCOMPtr<nsINode> testNode = mWorkingPointer.mNode; |
200 | 0 | int16_t filtered = TestNode(testNode, aResult); |
201 | 0 | if (aResult.Failed()) { |
202 | 0 | return nullptr; |
203 | 0 | } |
204 | 0 | |
205 | 0 | if (filtered == NodeFilter_Binding::FILTER_ACCEPT) { |
206 | 0 | mPointer = mWorkingPointer; |
207 | 0 | return testNode.forget(); |
208 | 0 | } |
209 | 0 | } |
210 | 0 |
|
211 | 0 | return nullptr; |
212 | 0 | } |
213 | | |
214 | | void |
215 | | NodeIterator::Detach() |
216 | 0 | { |
217 | 0 | if (mRoot) { |
218 | 0 | mRoot->OwnerDoc()->WarnOnceAbout(nsIDocument::eNodeIteratorDetach); |
219 | 0 | } |
220 | 0 | } |
221 | | |
222 | | /* |
223 | | * nsIMutationObserver interface |
224 | | */ |
225 | | |
226 | | void NodeIterator::ContentRemoved(nsIContent* aChild, |
227 | | nsIContent* aPreviousSibling) |
228 | 0 | { |
229 | 0 | nsINode* container = aChild->GetParentNode(); |
230 | 0 |
|
231 | 0 | mPointer.AdjustAfterRemoval(mRoot, container, aChild, aPreviousSibling); |
232 | 0 | mWorkingPointer.AdjustAfterRemoval(mRoot, container, aChild, aPreviousSibling); |
233 | 0 | } |
234 | | |
235 | | bool |
236 | | NodeIterator::WrapObject(JSContext *cx, JS::Handle<JSObject*> aGivenProto, JS::MutableHandle<JSObject*> aReflector) |
237 | 0 | { |
238 | 0 | return NodeIterator_Binding::Wrap(cx, this, aGivenProto, aReflector); |
239 | 0 | } |
240 | | |
241 | | } // namespace dom |
242 | | } // namespace mozilla |