txNameTest.cpp (2742B)
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* This Source Code Form is subject to the terms of the Mozilla Public 3 * License, v. 2.0. If a copy of the MPL was not distributed with this 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 5 6 #include "nsAtom.h" 7 #include "nsGkAtoms.h" 8 #include "txExpr.h" 9 #include "txIXPathContext.h" 10 #include "txXPathTreeWalker.h" 11 12 txNameTest::txNameTest(nsAtom* aPrefix, nsAtom* aLocalName, int32_t aNSID, 13 uint16_t aNodeType) 14 : mPrefix(aPrefix), 15 mLocalName(aLocalName), 16 mNamespace(aNSID), 17 mNodeType(aNodeType) { 18 if (aPrefix == nsGkAtoms::_empty) mPrefix = nullptr; 19 NS_ASSERTION(aLocalName, "txNameTest without a local name?"); 20 NS_ASSERTION(aNodeType == txXPathNodeType::DOCUMENT_NODE || 21 aNodeType == txXPathNodeType::ELEMENT_NODE || 22 aNodeType == txXPathNodeType::ATTRIBUTE_NODE, 23 "Go fix txNameTest::matches"); 24 } 25 26 nsresult txNameTest::matches(const txXPathNode& aNode, 27 txIMatchContext* aContext, bool& aMatched) { 28 if ((mNodeType == txXPathNodeType::ELEMENT_NODE && 29 !txXPathNodeUtils::isElement(aNode)) || 30 (mNodeType == txXPathNodeType::ATTRIBUTE_NODE && 31 !txXPathNodeUtils::isAttribute(aNode)) || 32 (mNodeType == txXPathNodeType::DOCUMENT_NODE && 33 !txXPathNodeUtils::isRoot(aNode))) { 34 aMatched = false; 35 return NS_OK; 36 } 37 38 // Totally wild? 39 if (mLocalName == nsGkAtoms::_asterisk && !mPrefix) { 40 aMatched = true; 41 return NS_OK; 42 } 43 44 // Compare namespaces 45 if (mNamespace != txXPathNodeUtils::getNamespaceID(aNode) && 46 !(mNamespace == kNameSpaceID_None && 47 txXPathNodeUtils::isHTMLElementInHTMLDocument(aNode))) { 48 aMatched = false; 49 return NS_OK; 50 } 51 52 // Name wild? 53 if (mLocalName == nsGkAtoms::_asterisk) { 54 aMatched = true; 55 return NS_OK; 56 } 57 58 // Compare local-names 59 aMatched = txXPathNodeUtils::localNameEquals(aNode, mLocalName); 60 return NS_OK; 61 } 62 63 /* 64 * Returns the default priority of this txNodeTest 65 */ 66 double txNameTest::getDefaultPriority() { 67 if (mLocalName == nsGkAtoms::_asterisk) { 68 if (!mPrefix) return -0.5; 69 return -0.25; 70 } 71 return 0; 72 } 73 74 txNodeTest::NodeTestType txNameTest::getType() { return NAME_TEST; } 75 76 bool txNameTest::isSensitiveTo(Expr::ContextSensitivity aContext) { 77 return !!(aContext & Expr::NODE_CONTEXT); 78 } 79 80 #ifdef TX_TO_STRING 81 void txNameTest::toString(nsAString& aDest) { 82 if (mPrefix) { 83 nsAutoString prefix; 84 mPrefix->ToString(prefix); 85 aDest.Append(prefix); 86 aDest.Append(char16_t(':')); 87 } 88 nsAutoString localName; 89 mLocalName->ToString(localName); 90 aDest.Append(localName); 91 } 92 #endif