dom/xslt/xpath/txNameTest.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/xslt/xpath/txNameTest.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,95 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "txExpr.h"
    1.10 +#include "nsIAtom.h"
    1.11 +#include "nsGkAtoms.h"
    1.12 +#include "txXPathTreeWalker.h"
    1.13 +#include "txIXPathContext.h"
    1.14 +
    1.15 +txNameTest::txNameTest(nsIAtom* aPrefix, nsIAtom* aLocalName, int32_t aNSID,
    1.16 +                       uint16_t aNodeType)
    1.17 +    :mPrefix(aPrefix), mLocalName(aLocalName), mNamespace(aNSID),
    1.18 +     mNodeType(aNodeType)
    1.19 +{
    1.20 +    if (aPrefix == nsGkAtoms::_empty)
    1.21 +        mPrefix = 0;
    1.22 +    NS_ASSERTION(aLocalName, "txNameTest without a local name?");
    1.23 +    NS_ASSERTION(aNodeType == txXPathNodeType::DOCUMENT_NODE ||
    1.24 +                 aNodeType == txXPathNodeType::ELEMENT_NODE ||
    1.25 +                 aNodeType == txXPathNodeType::ATTRIBUTE_NODE,
    1.26 +                 "Go fix txNameTest::matches");
    1.27 +}
    1.28 +
    1.29 +bool txNameTest::matches(const txXPathNode& aNode, txIMatchContext* aContext)
    1.30 +{
    1.31 +    if ((mNodeType == txXPathNodeType::ELEMENT_NODE &&
    1.32 +         !txXPathNodeUtils::isElement(aNode)) ||
    1.33 +        (mNodeType == txXPathNodeType::ATTRIBUTE_NODE &&
    1.34 +         !txXPathNodeUtils::isAttribute(aNode)) ||
    1.35 +        (mNodeType == txXPathNodeType::DOCUMENT_NODE &&
    1.36 +         !txXPathNodeUtils::isRoot(aNode))) {
    1.37 +        return false;
    1.38 +    }
    1.39 +
    1.40 +    // Totally wild?
    1.41 +    if (mLocalName == nsGkAtoms::_asterix && !mPrefix)
    1.42 +        return true;
    1.43 +
    1.44 +    // Compare namespaces
    1.45 +    if (mNamespace != txXPathNodeUtils::getNamespaceID(aNode) 
    1.46 +        && !(mNamespace == kNameSpaceID_None &&
    1.47 +             txXPathNodeUtils::isHTMLElementInHTMLDocument(aNode))
    1.48 +       )
    1.49 +        return false;
    1.50 +
    1.51 +    // Name wild?
    1.52 +    if (mLocalName == nsGkAtoms::_asterix)
    1.53 +        return true;
    1.54 +
    1.55 +    // Compare local-names
    1.56 +    return txXPathNodeUtils::localNameEquals(aNode, mLocalName);
    1.57 +}
    1.58 +
    1.59 +/*
    1.60 + * Returns the default priority of this txNodeTest
    1.61 + */
    1.62 +double txNameTest::getDefaultPriority()
    1.63 +{
    1.64 +    if (mLocalName == nsGkAtoms::_asterix) {
    1.65 +        if (!mPrefix)
    1.66 +            return -0.5;
    1.67 +        return -0.25;
    1.68 +    }
    1.69 +    return 0;
    1.70 +}
    1.71 +
    1.72 +txNodeTest::NodeTestType
    1.73 +txNameTest::getType()
    1.74 +{
    1.75 +    return NAME_TEST;
    1.76 +}
    1.77 +
    1.78 +bool
    1.79 +txNameTest::isSensitiveTo(Expr::ContextSensitivity aContext)
    1.80 +{
    1.81 +    return !!(aContext & Expr::NODE_CONTEXT);
    1.82 +}
    1.83 +
    1.84 +#ifdef TX_TO_STRING
    1.85 +void
    1.86 +txNameTest::toString(nsAString& aDest)
    1.87 +{
    1.88 +    if (mPrefix) {
    1.89 +        nsAutoString prefix;
    1.90 +        mPrefix->ToString(prefix);
    1.91 +        aDest.Append(prefix);
    1.92 +        aDest.Append(char16_t(':'));
    1.93 +    }
    1.94 +    nsAutoString localName;
    1.95 +    mLocalName->ToString(localName);
    1.96 +    aDest.Append(localName);
    1.97 +}
    1.98 +#endif

mercurial