txNodeTypeTest.cpp (2529B)
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 "txExpr.h" 8 #include "txIXPathContext.h" 9 #include "txXPathTreeWalker.h" 10 11 nsresult txNodeTypeTest::matches(const txXPathNode& aNode, 12 txIMatchContext* aContext, bool& aMatched) { 13 switch (mNodeType) { 14 case COMMENT_TYPE: { 15 aMatched = txXPathNodeUtils::isComment(aNode); 16 return NS_OK; 17 } 18 case TEXT_TYPE: { 19 aMatched = txXPathNodeUtils::isText(aNode); 20 if (aMatched) { 21 bool allowed; 22 nsresult rv = aContext->isStripSpaceAllowed(aNode, allowed); 23 NS_ENSURE_SUCCESS(rv, rv); 24 25 aMatched = !allowed; 26 } 27 return NS_OK; 28 } 29 case PI_TYPE: { 30 aMatched = 31 txXPathNodeUtils::isProcessingInstruction(aNode) && 32 (!mNodeName || txXPathNodeUtils::localNameEquals(aNode, mNodeName)); 33 return NS_OK; 34 } 35 case NODE_TYPE: { 36 if (txXPathNodeUtils::isText(aNode)) { 37 bool allowed; 38 nsresult rv = aContext->isStripSpaceAllowed(aNode, allowed); 39 NS_ENSURE_SUCCESS(rv, rv); 40 41 aMatched = !allowed; 42 } else { 43 aMatched = true; 44 } 45 return NS_OK; 46 } 47 } 48 49 MOZ_ASSERT_UNREACHABLE("Didn't deal with all values of the NodeType enum!"); 50 51 aMatched = false; 52 return NS_OK; 53 } 54 55 txNodeTest::NodeTestType txNodeTypeTest::getType() { return NODETYPE_TEST; } 56 57 /* 58 * Returns the default priority of this txNodeTest 59 */ 60 double txNodeTypeTest::getDefaultPriority() { return mNodeName ? 0 : -0.5; } 61 62 bool txNodeTypeTest::isSensitiveTo(Expr::ContextSensitivity aContext) { 63 return !!(aContext & Expr::NODE_CONTEXT); 64 } 65 66 #ifdef TX_TO_STRING 67 void txNodeTypeTest::toString(nsAString& aDest) { 68 switch (mNodeType) { 69 case COMMENT_TYPE: 70 aDest.AppendLiteral("comment()"); 71 break; 72 case TEXT_TYPE: 73 aDest.AppendLiteral("text()"); 74 break; 75 case PI_TYPE: 76 aDest.AppendLiteral("processing-instruction("); 77 if (mNodeName) { 78 nsAutoString str; 79 mNodeName->ToString(str); 80 aDest.Append(char16_t('\'')); 81 aDest.Append(str); 82 aDest.Append(char16_t('\'')); 83 } 84 aDest.Append(char16_t(')')); 85 break; 86 case NODE_TYPE: 87 aDest.AppendLiteral("node()"); 88 break; 89 } 90 } 91 #endif