Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include "txExpr.h" |
michael@0 | 7 | #include "nsIAtom.h" |
michael@0 | 8 | #include "txIXPathContext.h" |
michael@0 | 9 | #include "txNodeSet.h" |
michael@0 | 10 | |
michael@0 | 11 | /** |
michael@0 | 12 | * This class represents a FunctionCall as defined by the XSL Working Draft |
michael@0 | 13 | **/ |
michael@0 | 14 | |
michael@0 | 15 | //------------------/ |
michael@0 | 16 | //- Public Methods -/ |
michael@0 | 17 | //------------------/ |
michael@0 | 18 | |
michael@0 | 19 | /* |
michael@0 | 20 | * Evaluates the given Expression and converts its result to a number. |
michael@0 | 21 | */ |
michael@0 | 22 | // static |
michael@0 | 23 | nsresult |
michael@0 | 24 | FunctionCall::evaluateToNumber(Expr* aExpr, txIEvalContext* aContext, |
michael@0 | 25 | double* aResult) |
michael@0 | 26 | { |
michael@0 | 27 | NS_ASSERTION(aExpr, "missing expression"); |
michael@0 | 28 | nsRefPtr<txAExprResult> exprResult; |
michael@0 | 29 | nsresult rv = aExpr->evaluate(aContext, getter_AddRefs(exprResult)); |
michael@0 | 30 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 31 | |
michael@0 | 32 | *aResult = exprResult->numberValue(); |
michael@0 | 33 | |
michael@0 | 34 | return NS_OK; |
michael@0 | 35 | } |
michael@0 | 36 | |
michael@0 | 37 | /* |
michael@0 | 38 | * Evaluates the given Expression and converts its result to a NodeSet. |
michael@0 | 39 | * If the result is not a NodeSet nullptr is returned. |
michael@0 | 40 | */ |
michael@0 | 41 | nsresult |
michael@0 | 42 | FunctionCall::evaluateToNodeSet(Expr* aExpr, txIEvalContext* aContext, |
michael@0 | 43 | txNodeSet** aResult) |
michael@0 | 44 | { |
michael@0 | 45 | NS_ASSERTION(aExpr, "Missing expression to evaluate"); |
michael@0 | 46 | *aResult = nullptr; |
michael@0 | 47 | |
michael@0 | 48 | nsRefPtr<txAExprResult> exprRes; |
michael@0 | 49 | nsresult rv = aExpr->evaluate(aContext, getter_AddRefs(exprRes)); |
michael@0 | 50 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 51 | |
michael@0 | 52 | if (exprRes->getResultType() != txAExprResult::NODESET) { |
michael@0 | 53 | aContext->receiveError(NS_LITERAL_STRING("NodeSet expected as argument"), NS_ERROR_XSLT_NODESET_EXPECTED); |
michael@0 | 54 | return NS_ERROR_XSLT_NODESET_EXPECTED; |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | *aResult = |
michael@0 | 58 | static_cast<txNodeSet*>(static_cast<txAExprResult*>(exprRes)); |
michael@0 | 59 | NS_ADDREF(*aResult); |
michael@0 | 60 | |
michael@0 | 61 | return NS_OK; |
michael@0 | 62 | } |
michael@0 | 63 | |
michael@0 | 64 | bool FunctionCall::requireParams(int32_t aParamCountMin, |
michael@0 | 65 | int32_t aParamCountMax, |
michael@0 | 66 | txIEvalContext* aContext) |
michael@0 | 67 | { |
michael@0 | 68 | int32_t argc = mParams.Length(); |
michael@0 | 69 | if (argc < aParamCountMin || |
michael@0 | 70 | (aParamCountMax > -1 && argc > aParamCountMax)) { |
michael@0 | 71 | nsAutoString err(NS_LITERAL_STRING("invalid number of parameters for function")); |
michael@0 | 72 | #ifdef TX_TO_STRING |
michael@0 | 73 | err.AppendLiteral(": "); |
michael@0 | 74 | toString(err); |
michael@0 | 75 | #endif |
michael@0 | 76 | aContext->receiveError(err, NS_ERROR_XPATH_INVALID_ARG); |
michael@0 | 77 | |
michael@0 | 78 | return false; |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | return true; |
michael@0 | 82 | } |
michael@0 | 83 | |
michael@0 | 84 | Expr* |
michael@0 | 85 | FunctionCall::getSubExprAt(uint32_t aPos) |
michael@0 | 86 | { |
michael@0 | 87 | return mParams.SafeElementAt(aPos); |
michael@0 | 88 | } |
michael@0 | 89 | |
michael@0 | 90 | void |
michael@0 | 91 | FunctionCall::setSubExprAt(uint32_t aPos, Expr* aExpr) |
michael@0 | 92 | { |
michael@0 | 93 | NS_ASSERTION(aPos < mParams.Length(), |
michael@0 | 94 | "setting bad subexpression index"); |
michael@0 | 95 | mParams[aPos] = aExpr; |
michael@0 | 96 | } |
michael@0 | 97 | |
michael@0 | 98 | bool |
michael@0 | 99 | FunctionCall::argsSensitiveTo(ContextSensitivity aContext) |
michael@0 | 100 | { |
michael@0 | 101 | uint32_t i, len = mParams.Length(); |
michael@0 | 102 | for (i = 0; i < len; ++i) { |
michael@0 | 103 | if (mParams[i]->isSensitiveTo(aContext)) { |
michael@0 | 104 | return true; |
michael@0 | 105 | } |
michael@0 | 106 | } |
michael@0 | 107 | |
michael@0 | 108 | return false; |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | #ifdef TX_TO_STRING |
michael@0 | 112 | void |
michael@0 | 113 | FunctionCall::toString(nsAString& aDest) |
michael@0 | 114 | { |
michael@0 | 115 | nsCOMPtr<nsIAtom> functionNameAtom; |
michael@0 | 116 | if (NS_FAILED(getNameAtom(getter_AddRefs(functionNameAtom)))) { |
michael@0 | 117 | NS_ERROR("Can't get function name."); |
michael@0 | 118 | return; |
michael@0 | 119 | } |
michael@0 | 120 | |
michael@0 | 121 | |
michael@0 | 122 | |
michael@0 | 123 | aDest.Append(nsDependentAtomString(functionNameAtom) + |
michael@0 | 124 | NS_LITERAL_STRING("(")); |
michael@0 | 125 | for (uint32_t i = 0; i < mParams.Length(); ++i) { |
michael@0 | 126 | if (i != 0) { |
michael@0 | 127 | aDest.Append(char16_t(',')); |
michael@0 | 128 | } |
michael@0 | 129 | mParams[i]->toString(aDest); |
michael@0 | 130 | } |
michael@0 | 131 | aDest.Append(char16_t(')')); |
michael@0 | 132 | } |
michael@0 | 133 | #endif |