Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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/. */
7 /*
8 * Implementation of DOM Traversal's nsIDOMNodeIterator
9 */
11 #ifndef mozilla_dom_NodeIterator_h
12 #define mozilla_dom_NodeIterator_h
14 #include "nsIDOMNodeIterator.h"
15 #include "nsTraversal.h"
16 #include "nsCycleCollectionParticipant.h"
17 #include "nsStubMutationObserver.h"
19 class nsINode;
20 class nsIDOMNode;
22 namespace mozilla {
23 namespace dom {
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
33 NodeIterator(nsINode *aRoot,
34 uint32_t aWhatToShow,
35 const NodeFilterHolder &aFilter);
36 virtual ~NodeIterator();
38 NS_DECL_NSIMUTATIONOBSERVER_CONTENTREMOVED
40 NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(NodeIterator, nsIDOMNodeIterator)
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
73 JSObject* WrapObject(JSContext *cx);
75 private:
76 struct NodePointer {
77 NodePointer() : mNode(nullptr) {}
78 NodePointer(nsINode *aNode, bool aBeforeNode);
80 typedef bool (NodePointer::*MoveToMethodType)(nsINode*);
81 bool MoveToNext(nsINode *aRoot);
82 bool MoveToPrevious(nsINode *aRoot);
84 bool MoveForward(nsINode *aRoot, nsINode *aNode);
85 void MoveBackward(nsINode *aParent, nsINode *aNode);
87 void AdjustAfterRemoval(nsINode *aRoot, nsINode *aContainer, nsIContent *aChild, nsIContent *aPreviousSibling);
89 void Clear() { mNode = nullptr; }
91 nsINode *mNode;
92 bool mBeforeNode;
93 };
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 }
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);
113 NodePointer mPointer;
114 NodePointer mWorkingPointer;
115 };
117 } // namespace dom
118 } // namespace mozilla
120 #endif // mozilla_dom_NodeIterator_h