1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 "txExpr.h"
7 
8 nsresult
evaluate(txIEvalContext * aContext,txAExprResult ** aResult)9 txLiteralExpr::evaluate(txIEvalContext* aContext, txAExprResult** aResult)
10 {
11     NS_ENSURE_TRUE(mValue, NS_ERROR_OUT_OF_MEMORY);
12 
13     *aResult = mValue;
14     NS_ADDREF(*aResult);
15 
16     return NS_OK;
17 }
18 
19 static Expr::ResultType resultTypes[] =
20 {
21     Expr::NODESET_RESULT, // NODESET
22     Expr::BOOLEAN_RESULT, // BOOLEAN
23     Expr::NUMBER_RESULT,  // NUMBER
24     Expr::STRING_RESULT,  // STRING
25     Expr::RTF_RESULT      // RESULT_TREE_FRAGMENT
26 };
27 
28 Expr::ResultType
getReturnType()29 txLiteralExpr::getReturnType()
30 {
31     return resultTypes[mValue->getResultType()];
32 }
33 
34 Expr*
getSubExprAt(uint32_t aPos)35 txLiteralExpr::getSubExprAt(uint32_t aPos)
36 {
37     return nullptr;
38 }
39 void
setSubExprAt(uint32_t aPos,Expr * aExpr)40 txLiteralExpr::setSubExprAt(uint32_t aPos, Expr* aExpr)
41 {
42     NS_NOTREACHED("setting bad subexpression index");
43 }
44 
45 bool
isSensitiveTo(ContextSensitivity aContext)46 txLiteralExpr::isSensitiveTo(ContextSensitivity aContext)
47 {
48     return false;
49 }
50 
51 #ifdef TX_TO_STRING
52 void
toString(nsAString & aStr)53 txLiteralExpr::toString(nsAString& aStr)
54 {
55     switch (mValue->getResultType()) {
56         case txAExprResult::NODESET:
57         {
58             aStr.AppendLiteral(" { Nodeset literal } ");
59             return;
60         }
61         case txAExprResult::BOOLEAN:
62         {
63             if (mValue->booleanValue()) {
64               aStr.AppendLiteral("true()");
65             }
66             else {
67               aStr.AppendLiteral("false()");
68             }
69             return;
70         }
71         case txAExprResult::NUMBER:
72         {
73             txDouble::toString(mValue->numberValue(), aStr);
74             return;
75         }
76         case txAExprResult::STRING:
77         {
78             StringResult* strRes =
79                 static_cast<StringResult*>(static_cast<txAExprResult*>
80                                        (mValue));
81             char16_t ch = '\'';
82             if (strRes->mValue.Contains(ch)) {
83                 ch = '\"';
84             }
85             aStr.Append(ch);
86             aStr.Append(strRes->mValue);
87             aStr.Append(ch);
88             return;
89         }
90         case txAExprResult::RESULT_TREE_FRAGMENT:
91         {
92             aStr.AppendLiteral(" { RTF literal } ");
93             return;
94         }
95     }
96 }
97 #endif
98