Backed out changeset 2450366cf7ca (bug 1891629) for causing win msix mochitest failures
[gecko.git] / dom / base / nsTraversal.cpp
blob33582a5e8d1750abafe4424e21b2e328390cb29e
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 */
32 * Tests if and how a node should be filtered. Uses mWhatToShow and
33 * mFilter to test the node.
34 * @param aNode Node to test
35 * @param aResult Whether we succeeded
36 * @returns Filtervalue. See NodeFilter.webidl
38 int16_t nsTraversal::TestNode(nsINode* aNode, mozilla::ErrorResult& aResult,
39 nsCOMPtr<nsINode>* aUnskippedNode) {
40 if (mInAcceptNode) {
41 aResult.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
42 return 0;
45 uint16_t nodeType = aNode->NodeType();
47 if (nodeType <= 12 && !((1 << (nodeType - 1)) & mWhatToShow)) {
48 return NodeFilter_Binding::FILTER_SKIP;
51 if (aUnskippedNode) {
52 *aUnskippedNode = aNode;
55 if (!mFilter) {
56 // No filter, just accept
57 return NodeFilter_Binding::FILTER_ACCEPT;
60 AutoRestore<bool> inAcceptNode(mInAcceptNode);
61 mInAcceptNode = true;
62 // No need to pass in an execution reason, since the generated default,
63 // "NodeFilter.acceptNode", is pretty much exactly what we'd say anyway.
64 return mFilter->AcceptNode(*aNode, aResult, nullptr,
65 CallbackObject::eRethrowExceptions);