2008-07-22 00:38:52 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
*
|
2012-05-21 11:12:37 +00:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2008-07-22 00:38:52 +00:00
|
|
|
|
|
|
|
#include "nsTraversal.h"
|
|
|
|
|
|
|
|
#include "nsIDOMNode.h"
|
2012-07-27 14:03:27 +00:00
|
|
|
#include "nsError.h"
|
2012-07-01 23:45:59 +00:00
|
|
|
#include "nsINode.h"
|
2013-02-26 20:10:15 +00:00
|
|
|
#include "mozilla/AutoRestore.h"
|
2008-07-22 00:38:52 +00:00
|
|
|
|
|
|
|
#include "nsGkAtoms.h"
|
|
|
|
|
2013-02-26 20:10:15 +00:00
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::dom;
|
|
|
|
|
2008-07-22 00:38:52 +00:00
|
|
|
nsTraversal::nsTraversal(nsINode *aRoot,
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t aWhatToShow,
|
2013-02-26 20:10:15 +00:00
|
|
|
const NodeFilterHolder &aFilter) :
|
2008-07-22 00:38:52 +00:00
|
|
|
mRoot(aRoot),
|
|
|
|
mWhatToShow(aWhatToShow),
|
|
|
|
mFilter(aFilter),
|
2011-10-17 14:59:28 +00:00
|
|
|
mInAcceptNode(false)
|
2008-07-22 00:38:52 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aRoot, "invalid root in call to nsTraversal constructor");
|
|
|
|
}
|
|
|
|
|
|
|
|
nsTraversal::~nsTraversal()
|
|
|
|
{
|
|
|
|
/* destructor code */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Tests if and how a node should be filtered. Uses mWhatToShow and
|
|
|
|
* mFilter to test the node.
|
|
|
|
* @param aNode Node to test
|
2013-02-28 17:56:42 +00:00
|
|
|
* @param aResult Whether we succeeded
|
|
|
|
* @returns Filtervalue. See nsIDOMNodeFilter.idl
|
2008-07-22 00:38:52 +00:00
|
|
|
*/
|
2013-02-28 17:56:42 +00:00
|
|
|
int16_t
|
|
|
|
nsTraversal::TestNode(nsINode* aNode, mozilla::ErrorResult& aResult)
|
2008-07-22 00:38:52 +00:00
|
|
|
{
|
2013-02-28 17:56:42 +00:00
|
|
|
if (mInAcceptNode) {
|
|
|
|
aResult.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return 0;
|
|
|
|
}
|
2008-07-22 00:38:52 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint16_t nodeType = aNode->NodeType();
|
2008-07-22 00:38:52 +00:00
|
|
|
|
|
|
|
if (nodeType <= 12 && !((1 << (nodeType-1)) & mWhatToShow)) {
|
2013-02-28 17:56:42 +00:00
|
|
|
return nsIDOMNodeFilter::FILTER_SKIP;
|
2008-07-22 00:38:52 +00:00
|
|
|
}
|
|
|
|
|
2013-02-26 20:10:15 +00:00
|
|
|
if (!mFilter.GetISupports()) {
|
|
|
|
// No filter, just accept
|
2013-02-28 17:56:42 +00:00
|
|
|
return nsIDOMNodeFilter::FILTER_ACCEPT;
|
2013-02-26 20:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mFilter.HasWebIDLCallback()) {
|
|
|
|
AutoRestore<bool> inAcceptNode(mInAcceptNode);
|
2011-10-17 14:59:28 +00:00
|
|
|
mInAcceptNode = true;
|
2013-02-28 17:56:42 +00:00
|
|
|
return mFilter.GetWebIDLCallback()->
|
|
|
|
AcceptNode(*aNode, aResult, CallbackObject::eRethrowExceptions);
|
2008-07-22 00:38:52 +00:00
|
|
|
}
|
|
|
|
|
2013-02-26 20:10:15 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> domNode = do_QueryInterface(aNode);
|
|
|
|
AutoRestore<bool> inAcceptNode(mInAcceptNode);
|
|
|
|
mInAcceptNode = true;
|
2013-02-28 17:56:42 +00:00
|
|
|
int16_t filtered;
|
|
|
|
nsresult rv = mFilter.GetXPCOMCallback()->AcceptNode(domNode, &filtered);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aResult.Throw(rv);
|
|
|
|
}
|
|
|
|
return filtered;
|
2008-07-22 00:38:52 +00:00
|
|
|
}
|