|
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 nsTraversal::nsTraversal(nsINode *aRoot, |
|
20 uint32_t aWhatToShow, |
|
21 const NodeFilterHolder &aFilter) : |
|
22 mRoot(aRoot), |
|
23 mWhatToShow(aWhatToShow), |
|
24 mFilter(aFilter), |
|
25 mInAcceptNode(false) |
|
26 { |
|
27 NS_ASSERTION(aRoot, "invalid root in call to nsTraversal constructor"); |
|
28 } |
|
29 |
|
30 nsTraversal::~nsTraversal() |
|
31 { |
|
32 /* destructor code */ |
|
33 } |
|
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 nsTraversal::TestNode(nsINode* aNode, mozilla::ErrorResult& aResult) |
|
44 { |
|
45 if (mInAcceptNode) { |
|
46 aResult.Throw(NS_ERROR_DOM_INVALID_STATE_ERR); |
|
47 return 0; |
|
48 } |
|
49 |
|
50 uint16_t nodeType = aNode->NodeType(); |
|
51 |
|
52 if (nodeType <= 12 && !((1 << (nodeType-1)) & mWhatToShow)) { |
|
53 return nsIDOMNodeFilter::FILTER_SKIP; |
|
54 } |
|
55 |
|
56 if (!mFilter.GetISupports()) { |
|
57 // No filter, just accept |
|
58 return nsIDOMNodeFilter::FILTER_ACCEPT; |
|
59 } |
|
60 |
|
61 if (mFilter.HasWebIDLCallback()) { |
|
62 AutoRestore<bool> inAcceptNode(mInAcceptNode); |
|
63 mInAcceptNode = true; |
|
64 return mFilter.GetWebIDLCallback()-> |
|
65 AcceptNode(*aNode, aResult, CallbackObject::eRethrowExceptions); |
|
66 } |
|
67 |
|
68 nsCOMPtr<nsIDOMNode> domNode = do_QueryInterface(aNode); |
|
69 AutoRestore<bool> inAcceptNode(mInAcceptNode); |
|
70 mInAcceptNode = true; |
|
71 int16_t filtered; |
|
72 nsresult rv = mFilter.GetXPCOMCallback()->AcceptNode(domNode, &filtered); |
|
73 if (NS_FAILED(rv)) { |
|
74 aResult.Throw(rv); |
|
75 } |
|
76 return filtered; |
|
77 } |