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