content/xslt/src/xpath/txNodeTypeTest.cpp
author David Anderson <danderson@mozilla.com>
Mon, 21 May 2012 14:40:04 -0700
changeset 106274 80e4ab0d24bc64ceaa7693ab5def36faffde7a40
parent 105314 f93960a93ad97a56d308bd9ce25d97cbc175d524
parent 94484 f4157e8c410708d76703f19e4dfb61859bfe32d8
child 162124 5a9badd6db004310b9d1aca5db25ef4c275a4362
permissions -rw-r--r--
Merge from mozilla-central.

/* -*- 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 "txExpr.h"
#include "nsIAtom.h"
#include "txIXPathContext.h"
#include "txXPathTreeWalker.h"

bool txNodeTypeTest::matches(const txXPathNode& aNode,
                               txIMatchContext* aContext)
{
    switch (mNodeType) {
        case COMMENT_TYPE:
        {
            return txXPathNodeUtils::isComment(aNode);
        }
        case TEXT_TYPE:
        {
            return txXPathNodeUtils::isText(aNode) &&
                   !aContext->isStripSpaceAllowed(aNode);
        }
        case PI_TYPE:
        {
            return txXPathNodeUtils::isProcessingInstruction(aNode) &&
                   (!mNodeName ||
                    txXPathNodeUtils::localNameEquals(aNode, mNodeName));
        }
        case NODE_TYPE:
        {
            return !txXPathNodeUtils::isText(aNode) ||
                   !aContext->isStripSpaceAllowed(aNode);
        }
    }
    return true;
}

txNodeTest::NodeTestType
txNodeTypeTest::getType()
{
    return NODETYPE_TEST;
}

/*
 * Returns the default priority of this txNodeTest
 */
double txNodeTypeTest::getDefaultPriority()
{
    return mNodeName ? 0 : -0.5;
}

bool
txNodeTypeTest::isSensitiveTo(Expr::ContextSensitivity aContext)
{
    return !!(aContext & Expr::NODE_CONTEXT);
}

#ifdef TX_TO_STRING
void
txNodeTypeTest::toString(nsAString& aDest)
{
    switch (mNodeType) {
        case COMMENT_TYPE:
            aDest.Append(NS_LITERAL_STRING("comment()"));
            break;
        case TEXT_TYPE:
            aDest.Append(NS_LITERAL_STRING("text()"));
            break;
        case PI_TYPE:
            aDest.AppendLiteral("processing-instruction(");
            if (mNodeName) {
                nsAutoString str;
                mNodeName->ToString(str);
                aDest.Append(PRUnichar('\''));
                aDest.Append(str);
                aDest.Append(PRUnichar('\''));
            }
            aDest.Append(PRUnichar(')'));
            break;
        case NODE_TYPE:
            aDest.Append(NS_LITERAL_STRING("node()"));
            break;
    }
}
#endif