nsTraversal.cpp (1985B)
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- 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 #include "nsTraversal.h" 8 9 #include "mozilla/AutoRestore.h" 10 #include "mozilla/dom/NodeFilterBinding.h" 11 #include "nsError.h" 12 #include "nsGkAtoms.h" 13 #include "nsINode.h" 14 15 using namespace mozilla; 16 using namespace mozilla::dom; 17 18 nsTraversal::nsTraversal(nsINode* aRoot, uint32_t aWhatToShow, 19 NodeFilter* aFilter) 20 : mRoot(aRoot), 21 mWhatToShow(aWhatToShow), 22 mFilter(aFilter), 23 mInAcceptNode(false) { 24 NS_ASSERTION(aRoot, "invalid root in call to nsTraversal constructor"); 25 } 26 27 nsTraversal::~nsTraversal() { /* destructor code */ } 28 29 /* 30 * Tests if and how a node should be filtered. Uses mWhatToShow and 31 * mFilter to test the node. 32 * @param aNode Node to test 33 * @param aResult Whether we succeeded 34 * @returns Filtervalue. See NodeFilter.webidl 35 */ 36 int16_t nsTraversal::TestNode(nsINode* aNode, mozilla::ErrorResult& aResult, 37 nsCOMPtr<nsINode>* aUnskippedNode) { 38 if (mInAcceptNode) { 39 aResult.Throw(NS_ERROR_DOM_INVALID_STATE_ERR); 40 return 0; 41 } 42 43 uint16_t nodeType = aNode->NodeType(); 44 45 if (nodeType <= 12 && !((1 << (nodeType - 1)) & mWhatToShow)) { 46 return NodeFilter_Binding::FILTER_SKIP; 47 } 48 49 if (aUnskippedNode) { 50 *aUnskippedNode = aNode; 51 } 52 53 if (!mFilter) { 54 // No filter, just accept 55 return NodeFilter_Binding::FILTER_ACCEPT; 56 } 57 58 AutoRestore<bool> inAcceptNode(mInAcceptNode); 59 mInAcceptNode = true; 60 // No need to pass in an execution reason, since the generated default, 61 // "NodeFilter.acceptNode", is pretty much exactly what we'd say anyway. 62 return mFilter->AcceptNode(*aNode, aResult, nullptr, 63 CallbackObject::eRethrowExceptions); 64 }