gecko/content/base/src/nsTraversal.cpp
Boris Zbarsky 4549dd5886 Bug 776536 part 4. Turn on WebIDL bindings for NodeIterator and TreeWalker. r=peterv
The behavior here is a bit weird because Document is still not a
WebIDL object, so calling createNodeIterator or createTreeWalker via
an Xray will call the XPCOM versions of those methods.  That means
that I can't just disable XPCOM-based wrapping for TreeWalker and
NodeIterator altogether, unfortunately, which means a web page could
try stashing a TreeWalker in something like userdata and then getting
it back and end up wrapping it as an XPCOM object the second time.

I could "fix" that by adding a wrapper cache and whatnot, I guess, if
desired...  But the problem will go away once we convert Document in
any case.
2013-02-28 12:56:42 -05:00

78 lines
2.1 KiB
C++

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* 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/. */
#include "nsTraversal.h"
#include "nsIDOMNode.h"
#include "nsError.h"
#include "nsINode.h"
#include "mozilla/AutoRestore.h"
#include "nsGkAtoms.h"
using namespace mozilla;
using namespace mozilla::dom;
nsTraversal::nsTraversal(nsINode *aRoot,
uint32_t aWhatToShow,
const NodeFilterHolder &aFilter) :
mRoot(aRoot),
mWhatToShow(aWhatToShow),
mFilter(aFilter),
mInAcceptNode(false)
{
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
* @param aResult Whether we succeeded
* @returns Filtervalue. See nsIDOMNodeFilter.idl
*/
int16_t
nsTraversal::TestNode(nsINode* aNode, mozilla::ErrorResult& aResult)
{
if (mInAcceptNode) {
aResult.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
return 0;
}
uint16_t nodeType = aNode->NodeType();
if (nodeType <= 12 && !((1 << (nodeType-1)) & mWhatToShow)) {
return nsIDOMNodeFilter::FILTER_SKIP;
}
if (!mFilter.GetISupports()) {
// No filter, just accept
return nsIDOMNodeFilter::FILTER_ACCEPT;
}
if (mFilter.HasWebIDLCallback()) {
AutoRestore<bool> inAcceptNode(mInAcceptNode);
mInAcceptNode = true;
return mFilter.GetWebIDLCallback()->
AcceptNode(*aNode, aResult, CallbackObject::eRethrowExceptions);
}
nsCOMPtr<nsIDOMNode> domNode = do_QueryInterface(aNode);
AutoRestore<bool> inAcceptNode(mInAcceptNode);
mInAcceptNode = true;
int16_t filtered;
nsresult rv = mFilter.GetXPCOMCallback()->AcceptNode(domNode, &filtered);
if (NS_FAILED(rv)) {
aResult.Throw(rv);
}
return filtered;
}