txLocationStep.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. Implementation of an XPath LocationStep
  7. */
  8. #include "txExpr.h"
  9. #include "txIXPathContext.h"
  10. #include "txNodeSet.h"
  11. #include "txXPathTreeWalker.h"
  12. //-----------------------------/
  13. //- Virtual methods from Expr -/
  14. //-----------------------------/
  15. /**
  16. * Evaluates this Expr based on the given context node and processor state
  17. * @param context the context node for evaluation of this Expr
  18. * @param ps the ProcessorState containing the stack information needed
  19. * for evaluation
  20. * @return the result of the evaluation
  21. * @see Expr
  22. **/
  23. nsresult
  24. LocationStep::evaluate(txIEvalContext* aContext, txAExprResult** aResult)
  25. {
  26. NS_ASSERTION(aContext, "internal error");
  27. *aResult = nullptr;
  28. RefPtr<txNodeSet> nodes;
  29. nsresult rv = aContext->recycler()->getNodeSet(getter_AddRefs(nodes));
  30. NS_ENSURE_SUCCESS(rv, rv);
  31. txXPathTreeWalker walker(aContext->getContextNode());
  32. switch (mAxisIdentifier) {
  33. case ANCESTOR_AXIS:
  34. {
  35. if (!walker.moveToParent()) {
  36. break;
  37. }
  38. MOZ_FALLTHROUGH;
  39. }
  40. case ANCESTOR_OR_SELF_AXIS:
  41. {
  42. nodes->setReverse();
  43. do {
  44. if (mNodeTest->matches(walker.getCurrentPosition(), aContext)) {
  45. nodes->append(walker.getCurrentPosition());
  46. }
  47. } while (walker.moveToParent());
  48. break;
  49. }
  50. case ATTRIBUTE_AXIS:
  51. {
  52. if (!walker.moveToFirstAttribute()) {
  53. break;
  54. }
  55. do {
  56. if (mNodeTest->matches(walker.getCurrentPosition(), aContext)) {
  57. nodes->append(walker.getCurrentPosition());
  58. }
  59. } while (walker.moveToNextAttribute());
  60. break;
  61. }
  62. case DESCENDANT_OR_SELF_AXIS:
  63. {
  64. if (mNodeTest->matches(walker.getCurrentPosition(), aContext)) {
  65. nodes->append(walker.getCurrentPosition());
  66. }
  67. MOZ_FALLTHROUGH;
  68. }
  69. case DESCENDANT_AXIS:
  70. {
  71. fromDescendants(walker.getCurrentPosition(), aContext, nodes);
  72. break;
  73. }
  74. case FOLLOWING_AXIS:
  75. {
  76. if (txXPathNodeUtils::isAttribute(walker.getCurrentPosition())) {
  77. walker.moveToParent();
  78. fromDescendants(walker.getCurrentPosition(), aContext, nodes);
  79. }
  80. bool cont = true;
  81. while (!walker.moveToNextSibling()) {
  82. if (!walker.moveToParent()) {
  83. cont = false;
  84. break;
  85. }
  86. }
  87. while (cont) {
  88. if (mNodeTest->matches(walker.getCurrentPosition(), aContext)) {
  89. nodes->append(walker.getCurrentPosition());
  90. }
  91. fromDescendants(walker.getCurrentPosition(), aContext, nodes);
  92. while (!walker.moveToNextSibling()) {
  93. if (!walker.moveToParent()) {
  94. cont = false;
  95. break;
  96. }
  97. }
  98. }
  99. break;
  100. }
  101. case FOLLOWING_SIBLING_AXIS:
  102. {
  103. while (walker.moveToNextSibling()) {
  104. if (mNodeTest->matches(walker.getCurrentPosition(), aContext)) {
  105. nodes->append(walker.getCurrentPosition());
  106. }
  107. }
  108. break;
  109. }
  110. case NAMESPACE_AXIS: //-- not yet implemented
  111. #if 0
  112. // XXX DEBUG OUTPUT
  113. cout << "namespace axis not yet implemented"<<endl;
  114. #endif
  115. break;
  116. case PARENT_AXIS :
  117. {
  118. if (walker.moveToParent() &&
  119. mNodeTest->matches(walker.getCurrentPosition(), aContext)) {
  120. nodes->append(walker.getCurrentPosition());
  121. }
  122. break;
  123. }
  124. case PRECEDING_AXIS:
  125. {
  126. nodes->setReverse();
  127. bool cont = true;
  128. while (!walker.moveToPreviousSibling()) {
  129. if (!walker.moveToParent()) {
  130. cont = false;
  131. break;
  132. }
  133. }
  134. while (cont) {
  135. fromDescendantsRev(walker.getCurrentPosition(), aContext, nodes);
  136. if (mNodeTest->matches(walker.getCurrentPosition(), aContext)) {
  137. nodes->append(walker.getCurrentPosition());
  138. }
  139. while (!walker.moveToPreviousSibling()) {
  140. if (!walker.moveToParent()) {
  141. cont = false;
  142. break;
  143. }
  144. }
  145. }
  146. break;
  147. }
  148. case PRECEDING_SIBLING_AXIS:
  149. {
  150. nodes->setReverse();
  151. while (walker.moveToPreviousSibling()) {
  152. if (mNodeTest->matches(walker.getCurrentPosition(), aContext)) {
  153. nodes->append(walker.getCurrentPosition());
  154. }
  155. }
  156. break;
  157. }
  158. case SELF_AXIS:
  159. {
  160. if (mNodeTest->matches(walker.getCurrentPosition(), aContext)) {
  161. nodes->append(walker.getCurrentPosition());
  162. }
  163. break;
  164. }
  165. default: // Children Axis
  166. {
  167. if (!walker.moveToFirstChild()) {
  168. break;
  169. }
  170. do {
  171. if (mNodeTest->matches(walker.getCurrentPosition(), aContext)) {
  172. nodes->append(walker.getCurrentPosition());
  173. }
  174. } while (walker.moveToNextSibling());
  175. break;
  176. }
  177. }
  178. // Apply predicates
  179. if (!isEmpty()) {
  180. rv = evaluatePredicates(nodes, aContext);
  181. NS_ENSURE_SUCCESS(rv, rv);
  182. }
  183. nodes->unsetReverse();
  184. NS_ADDREF(*aResult = nodes);
  185. return NS_OK;
  186. }
  187. void LocationStep::fromDescendants(const txXPathNode& aNode,
  188. txIMatchContext* aCs,
  189. txNodeSet* aNodes)
  190. {
  191. txXPathTreeWalker walker(aNode);
  192. if (!walker.moveToFirstChild()) {
  193. return;
  194. }
  195. do {
  196. const txXPathNode& child = walker.getCurrentPosition();
  197. if (mNodeTest->matches(child, aCs)) {
  198. aNodes->append(child);
  199. }
  200. fromDescendants(child, aCs, aNodes);
  201. } while (walker.moveToNextSibling());
  202. }
  203. void LocationStep::fromDescendantsRev(const txXPathNode& aNode,
  204. txIMatchContext* aCs,
  205. txNodeSet* aNodes)
  206. {
  207. txXPathTreeWalker walker(aNode);
  208. if (!walker.moveToLastChild()) {
  209. return;
  210. }
  211. do {
  212. const txXPathNode& child = walker.getCurrentPosition();
  213. fromDescendantsRev(child, aCs, aNodes);
  214. if (mNodeTest->matches(child, aCs)) {
  215. aNodes->append(child);
  216. }
  217. } while (walker.moveToPreviousSibling());
  218. }
  219. Expr::ExprType
  220. LocationStep::getType()
  221. {
  222. return LOCATIONSTEP_EXPR;
  223. }
  224. TX_IMPL_EXPR_STUBS_BASE(LocationStep, NODESET_RESULT)
  225. Expr*
  226. LocationStep::getSubExprAt(uint32_t aPos)
  227. {
  228. return PredicateList::getSubExprAt(aPos);
  229. }
  230. void
  231. LocationStep::setSubExprAt(uint32_t aPos, Expr* aExpr)
  232. {
  233. PredicateList::setSubExprAt(aPos, aExpr);
  234. }
  235. bool
  236. LocationStep::isSensitiveTo(ContextSensitivity aContext)
  237. {
  238. return (aContext & NODE_CONTEXT) ||
  239. mNodeTest->isSensitiveTo(aContext) ||
  240. PredicateList::isSensitiveTo(aContext);
  241. }
  242. #ifdef TX_TO_STRING
  243. void
  244. LocationStep::toString(nsAString& str)
  245. {
  246. switch (mAxisIdentifier) {
  247. case ANCESTOR_AXIS :
  248. str.AppendLiteral("ancestor::");
  249. break;
  250. case ANCESTOR_OR_SELF_AXIS :
  251. str.AppendLiteral("ancestor-or-self::");
  252. break;
  253. case ATTRIBUTE_AXIS:
  254. str.Append(char16_t('@'));
  255. break;
  256. case DESCENDANT_AXIS:
  257. str.AppendLiteral("descendant::");
  258. break;
  259. case DESCENDANT_OR_SELF_AXIS:
  260. str.AppendLiteral("descendant-or-self::");
  261. break;
  262. case FOLLOWING_AXIS :
  263. str.AppendLiteral("following::");
  264. break;
  265. case FOLLOWING_SIBLING_AXIS:
  266. str.AppendLiteral("following-sibling::");
  267. break;
  268. case NAMESPACE_AXIS:
  269. str.AppendLiteral("namespace::");
  270. break;
  271. case PARENT_AXIS :
  272. str.AppendLiteral("parent::");
  273. break;
  274. case PRECEDING_AXIS :
  275. str.AppendLiteral("preceding::");
  276. break;
  277. case PRECEDING_SIBLING_AXIS :
  278. str.AppendLiteral("preceding-sibling::");
  279. break;
  280. case SELF_AXIS :
  281. str.AppendLiteral("self::");
  282. break;
  283. default:
  284. break;
  285. }
  286. NS_ASSERTION(mNodeTest, "mNodeTest is null, that's verboten");
  287. mNodeTest->toString(str);
  288. PredicateList::toString(str);
  289. }
  290. #endif