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