1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 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/. */
7 #include "ScriptableContentIterator.h"
9 #include "mozilla/ContentIterator.h"
15 NS_IMPL_CYCLE_COLLECTING_ADDREF(ScriptableContentIterator
)
16 NS_IMPL_CYCLE_COLLECTING_RELEASE(ScriptableContentIterator
)
18 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ScriptableContentIterator
)
19 NS_INTERFACE_MAP_ENTRY(nsIScriptableContentIterator
)
20 NS_INTERFACE_MAP_ENTRY(nsISupports
)
23 NS_IMPL_CYCLE_COLLECTION_CLASS(ScriptableContentIterator
)
25 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(ScriptableContentIterator
)
26 if (tmp
->mContentIterator
) {
27 switch (tmp
->mIteratorType
) {
28 case POST_ORDER_ITERATOR
:
30 ImplCycleCollectionUnlink(
31 static_cast<PostContentIterator
&>(*tmp
->mContentIterator
));
33 case PRE_ORDER_ITERATOR
:
34 ImplCycleCollectionUnlink(
35 static_cast<PreContentIterator
&>(*tmp
->mContentIterator
));
37 case SUBTREE_ITERATOR
:
38 ImplCycleCollectionUnlink(
39 static_cast<ContentSubtreeIterator
&>(*tmp
->mContentIterator
));
43 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
44 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(ScriptableContentIterator
)
45 if (tmp
->mContentIterator
) {
46 switch (tmp
->mIteratorType
) {
47 case POST_ORDER_ITERATOR
:
49 ImplCycleCollectionTraverse(
50 cb
, static_cast<PostContentIterator
&>(*tmp
->mContentIterator
),
53 case PRE_ORDER_ITERATOR
:
54 ImplCycleCollectionTraverse(
55 cb
, static_cast<PreContentIterator
&>(*tmp
->mContentIterator
),
58 case SUBTREE_ITERATOR
:
59 ImplCycleCollectionTraverse(
60 cb
, static_cast<ContentSubtreeIterator
&>(*tmp
->mContentIterator
),
65 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
67 ScriptableContentIterator::ScriptableContentIterator()
68 : mIteratorType(NOT_INITIALIZED
) {}
70 void ScriptableContentIterator::EnsureContentIterator() {
71 if (mContentIterator
) {
74 switch (mIteratorType
) {
75 case POST_ORDER_ITERATOR
:
77 mContentIterator
= MakeUnique
<PostContentIterator
>();
79 case PRE_ORDER_ITERATOR
:
80 mContentIterator
= MakeUnique
<PreContentIterator
>();
82 case SUBTREE_ITERATOR
:
83 mContentIterator
= MakeUnique
<ContentSubtreeIterator
>();
89 ScriptableContentIterator::InitWithRootNode(IteratorType aType
,
91 if (aType
== NOT_INITIALIZED
||
92 (mIteratorType
!= NOT_INITIALIZED
&& aType
!= mIteratorType
)) {
93 return NS_ERROR_INVALID_ARG
;
95 mIteratorType
= aType
;
96 EnsureContentIterator();
97 return mContentIterator
->Init(aRoot
);
101 ScriptableContentIterator::InitWithRange(IteratorType aType
, nsRange
* aRange
) {
102 if (aType
== NOT_INITIALIZED
||
103 (mIteratorType
!= NOT_INITIALIZED
&& aType
!= mIteratorType
)) {
104 return NS_ERROR_INVALID_ARG
;
106 mIteratorType
= aType
;
107 EnsureContentIterator();
108 return mContentIterator
->Init(aRange
);
112 ScriptableContentIterator::InitWithPositions(IteratorType aType
,
113 nsINode
* aStartContainer
,
114 uint32_t aStartOffset
,
115 nsINode
* aEndContainer
,
116 uint32_t aEndOffset
) {
117 if (aType
== NOT_INITIALIZED
||
118 (mIteratorType
!= NOT_INITIALIZED
&& aType
!= mIteratorType
)) {
119 return NS_ERROR_INVALID_ARG
;
121 mIteratorType
= aType
;
122 EnsureContentIterator();
123 return mContentIterator
->Init(aStartContainer
, aStartOffset
, aEndContainer
,
128 ScriptableContentIterator::First() {
129 if (!mContentIterator
) {
130 return NS_ERROR_NOT_INITIALIZED
;
132 mContentIterator
->First();
137 ScriptableContentIterator::Last() {
138 if (!mContentIterator
) {
139 return NS_ERROR_NOT_INITIALIZED
;
141 mContentIterator
->Last();
146 ScriptableContentIterator::Next() {
147 if (!mContentIterator
) {
148 return NS_ERROR_NOT_INITIALIZED
;
150 mContentIterator
->Next();
155 ScriptableContentIterator::Prev() {
156 if (!mContentIterator
) {
157 return NS_ERROR_NOT_INITIALIZED
;
159 mContentIterator
->Prev();
164 ScriptableContentIterator::GetCurrentNode(nsINode
** aNode
) {
165 if (!mContentIterator
) {
166 return NS_ERROR_NOT_INITIALIZED
;
168 NS_IF_ADDREF(*aNode
= mContentIterator
->GetCurrentNode());
173 ScriptableContentIterator::GetIsDone(bool* aIsDone
) {
174 if (!mContentIterator
) {
175 return NS_ERROR_NOT_INITIALIZED
;
177 *aIsDone
= mContentIterator
->IsDone();
182 ScriptableContentIterator::PositionAt(nsINode
* aNode
) {
183 if (!mContentIterator
) {
184 return NS_ERROR_NOT_INITIALIZED
;
186 return mContentIterator
->PositionAt(aNode
);
189 } // namespace mozilla