content/base/src/nsTraversal.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/base/src/nsTraversal.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,77 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
     1.5 + *
     1.6 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#include "nsTraversal.h"
    1.11 +
    1.12 +#include "nsIDOMNode.h"
    1.13 +#include "nsError.h"
    1.14 +#include "nsINode.h"
    1.15 +#include "mozilla/AutoRestore.h"
    1.16 +
    1.17 +#include "nsGkAtoms.h"
    1.18 +
    1.19 +using namespace mozilla;
    1.20 +using namespace mozilla::dom;
    1.21 +
    1.22 +nsTraversal::nsTraversal(nsINode *aRoot,
    1.23 +                         uint32_t aWhatToShow,
    1.24 +                         const NodeFilterHolder &aFilter) :
    1.25 +    mRoot(aRoot),
    1.26 +    mWhatToShow(aWhatToShow),
    1.27 +    mFilter(aFilter),
    1.28 +    mInAcceptNode(false)
    1.29 +{
    1.30 +    NS_ASSERTION(aRoot, "invalid root in call to nsTraversal constructor");
    1.31 +}
    1.32 +
    1.33 +nsTraversal::~nsTraversal()
    1.34 +{
    1.35 +    /* destructor code */
    1.36 +}
    1.37 +
    1.38 +/*
    1.39 + * Tests if and how a node should be filtered. Uses mWhatToShow and
    1.40 + * mFilter to test the node.
    1.41 + * @param aNode     Node to test
    1.42 + * @param aResult   Whether we succeeded
    1.43 + * @returns         Filtervalue. See nsIDOMNodeFilter.idl
    1.44 + */
    1.45 +int16_t
    1.46 +nsTraversal::TestNode(nsINode* aNode, mozilla::ErrorResult& aResult)
    1.47 +{
    1.48 +    if (mInAcceptNode) {
    1.49 +        aResult.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
    1.50 +        return 0;
    1.51 +    }
    1.52 +
    1.53 +    uint16_t nodeType = aNode->NodeType();
    1.54 +
    1.55 +    if (nodeType <= 12 && !((1 << (nodeType-1)) & mWhatToShow)) {
    1.56 +        return nsIDOMNodeFilter::FILTER_SKIP;
    1.57 +    }
    1.58 +
    1.59 +    if (!mFilter.GetISupports()) {
    1.60 +        // No filter, just accept
    1.61 +        return nsIDOMNodeFilter::FILTER_ACCEPT;
    1.62 +    }
    1.63 +
    1.64 +    if (mFilter.HasWebIDLCallback()) {
    1.65 +        AutoRestore<bool> inAcceptNode(mInAcceptNode);
    1.66 +        mInAcceptNode = true;
    1.67 +        return mFilter.GetWebIDLCallback()->
    1.68 +            AcceptNode(*aNode, aResult, CallbackObject::eRethrowExceptions);
    1.69 +    }
    1.70 +
    1.71 +    nsCOMPtr<nsIDOMNode> domNode = do_QueryInterface(aNode);
    1.72 +    AutoRestore<bool> inAcceptNode(mInAcceptNode);
    1.73 +    mInAcceptNode = true;
    1.74 +    int16_t filtered;
    1.75 +    nsresult rv = mFilter.GetXPCOMCallback()->AcceptNode(domNode, &filtered);
    1.76 +    if (NS_FAILED(rv)) {
    1.77 +        aResult.Throw(rv);
    1.78 +    }
    1.79 +    return filtered;
    1.80 +}

mercurial