michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "txExpr.h" michael@0: #include "nsIAtom.h" michael@0: #include "nsGkAtoms.h" michael@0: #include "txXPathTreeWalker.h" michael@0: #include "txIXPathContext.h" michael@0: michael@0: txNameTest::txNameTest(nsIAtom* aPrefix, nsIAtom* aLocalName, int32_t aNSID, michael@0: uint16_t aNodeType) michael@0: :mPrefix(aPrefix), mLocalName(aLocalName), mNamespace(aNSID), michael@0: mNodeType(aNodeType) michael@0: { michael@0: if (aPrefix == nsGkAtoms::_empty) michael@0: mPrefix = 0; michael@0: NS_ASSERTION(aLocalName, "txNameTest without a local name?"); michael@0: NS_ASSERTION(aNodeType == txXPathNodeType::DOCUMENT_NODE || michael@0: aNodeType == txXPathNodeType::ELEMENT_NODE || michael@0: aNodeType == txXPathNodeType::ATTRIBUTE_NODE, michael@0: "Go fix txNameTest::matches"); michael@0: } michael@0: michael@0: bool txNameTest::matches(const txXPathNode& aNode, txIMatchContext* aContext) michael@0: { michael@0: if ((mNodeType == txXPathNodeType::ELEMENT_NODE && michael@0: !txXPathNodeUtils::isElement(aNode)) || michael@0: (mNodeType == txXPathNodeType::ATTRIBUTE_NODE && michael@0: !txXPathNodeUtils::isAttribute(aNode)) || michael@0: (mNodeType == txXPathNodeType::DOCUMENT_NODE && michael@0: !txXPathNodeUtils::isRoot(aNode))) { michael@0: return false; michael@0: } michael@0: michael@0: // Totally wild? michael@0: if (mLocalName == nsGkAtoms::_asterix && !mPrefix) michael@0: return true; michael@0: michael@0: // Compare namespaces michael@0: if (mNamespace != txXPathNodeUtils::getNamespaceID(aNode) michael@0: && !(mNamespace == kNameSpaceID_None && michael@0: txXPathNodeUtils::isHTMLElementInHTMLDocument(aNode)) michael@0: ) michael@0: return false; michael@0: michael@0: // Name wild? michael@0: if (mLocalName == nsGkAtoms::_asterix) michael@0: return true; michael@0: michael@0: // Compare local-names michael@0: return txXPathNodeUtils::localNameEquals(aNode, mLocalName); michael@0: } michael@0: michael@0: /* michael@0: * Returns the default priority of this txNodeTest michael@0: */ michael@0: double txNameTest::getDefaultPriority() michael@0: { michael@0: if (mLocalName == nsGkAtoms::_asterix) { michael@0: if (!mPrefix) michael@0: return -0.5; michael@0: return -0.25; michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: txNodeTest::NodeTestType michael@0: txNameTest::getType() michael@0: { michael@0: return NAME_TEST; michael@0: } michael@0: michael@0: bool michael@0: txNameTest::isSensitiveTo(Expr::ContextSensitivity aContext) michael@0: { michael@0: return !!(aContext & Expr::NODE_CONTEXT); michael@0: } michael@0: michael@0: #ifdef TX_TO_STRING michael@0: void michael@0: txNameTest::toString(nsAString& aDest) michael@0: { michael@0: if (mPrefix) { michael@0: nsAutoString prefix; michael@0: mPrefix->ToString(prefix); michael@0: aDest.Append(prefix); michael@0: aDest.Append(char16_t(':')); michael@0: } michael@0: nsAutoString localName; michael@0: mLocalName->ToString(localName); michael@0: aDest.Append(localName); michael@0: } michael@0: #endif