content/base/src/NodeIterator.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial