Backed out changeset b462e7b742d8 (bug 1908261) for causing multiple reftest failures...
[gecko.git] / dom / base / nsTraversal.cpp
blob0709df0c02cf2bf1003372648ac16b036a7d5b5f
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"
9 #include "nsError.h"
10 #include "nsINode.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,
20 NodeFilter* aFilter)
21 : mRoot(aRoot),
22 mWhatToShow(aWhatToShow),
23 mFilter(aFilter),
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) {
39 if (mInAcceptNode) {
40 aResult.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
41 return 0;
44 uint16_t nodeType = aNode->NodeType();
46 if (nodeType <= 12 && !((1 << (nodeType - 1)) & mWhatToShow)) {
47 return NodeFilter_Binding::FILTER_SKIP;
50 if (aUnskippedNode) {
51 *aUnskippedNode = aNode;
54 if (!mFilter) {
55 // No filter, just accept
56 return NodeFilter_Binding::FILTER_ACCEPT;
59 AutoRestore<bool> inAcceptNode(mInAcceptNode);
60 mInAcceptNode = true;
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);