txVariableRefExpr.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. #include "txExpr.h"
  6. #include "nsIAtom.h"
  7. #include "txNodeSet.h"
  8. #include "nsGkAtoms.h"
  9. #include "txIXPathContext.h"
  10. //-------------------/
  11. //- VariableRefExpr -/
  12. //-------------------/
  13. /**
  14. * Creates a VariableRefExpr with the given variable name
  15. **/
  16. VariableRefExpr::VariableRefExpr(nsIAtom* aPrefix, nsIAtom* aLocalName,
  17. int32_t aNSID)
  18. : mPrefix(aPrefix), mLocalName(aLocalName), mNamespace(aNSID)
  19. {
  20. NS_ASSERTION(mLocalName, "VariableRefExpr without local name?");
  21. if (mPrefix == nsGkAtoms::_empty)
  22. mPrefix = nullptr;
  23. }
  24. /**
  25. * Evaluates this Expr based on the given context node and processor state
  26. * @param context the context node for evaluation of this Expr
  27. * @param ps the ContextState containing the stack information needed
  28. * for evaluation
  29. * @return the result of the evaluation
  30. **/
  31. nsresult
  32. VariableRefExpr::evaluate(txIEvalContext* aContext, txAExprResult** aResult)
  33. {
  34. nsresult rv = aContext->getVariable(mNamespace, mLocalName, *aResult);
  35. if (NS_FAILED(rv)) {
  36. // XXX report error, undefined variable
  37. return rv;
  38. }
  39. return NS_OK;
  40. }
  41. TX_IMPL_EXPR_STUBS_0(VariableRefExpr, ANY_RESULT)
  42. bool
  43. VariableRefExpr::isSensitiveTo(ContextSensitivity aContext)
  44. {
  45. return !!(aContext & VARIABLES_CONTEXT);
  46. }
  47. #ifdef TX_TO_STRING
  48. void
  49. VariableRefExpr::toString(nsAString& aDest)
  50. {
  51. aDest.Append(char16_t('$'));
  52. if (mPrefix) {
  53. nsAutoString prefix;
  54. mPrefix->ToString(prefix);
  55. aDest.Append(prefix);
  56. aDest.Append(char16_t(':'));
  57. }
  58. nsAutoString lname;
  59. mLocalName->ToString(lname);
  60. aDest.Append(lname);
  61. }
  62. #endif