|
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 #include "txNodeSet.h" |
|
8 #include "txNodeSetContext.h" |
|
9 |
|
10 /* |
|
11 * Represents an ordered list of Predicates, |
|
12 * for use with Step and Filter Expressions |
|
13 */ |
|
14 |
|
15 nsresult |
|
16 PredicateList::evaluatePredicates(txNodeSet* nodes, |
|
17 txIMatchContext* aContext) |
|
18 { |
|
19 NS_ASSERTION(nodes, "called evaluatePredicates with nullptr NodeSet"); |
|
20 nsresult rv = NS_OK; |
|
21 |
|
22 uint32_t i, len = mPredicates.Length(); |
|
23 for (i = 0; i < len && !nodes->isEmpty(); ++i) { |
|
24 txNodeSetContext predContext(nodes, aContext); |
|
25 /* |
|
26 * add nodes to newNodes that match the expression |
|
27 * or, if the result is a number, add the node with the right |
|
28 * position |
|
29 */ |
|
30 int32_t index = 0; |
|
31 while (predContext.hasNext()) { |
|
32 predContext.next(); |
|
33 nsRefPtr<txAExprResult> exprResult; |
|
34 rv = mPredicates[i]->evaluate(&predContext, |
|
35 getter_AddRefs(exprResult)); |
|
36 NS_ENSURE_SUCCESS(rv, rv); |
|
37 |
|
38 // handle default, [position() == numberValue()] |
|
39 if (exprResult->getResultType() == txAExprResult::NUMBER) { |
|
40 if ((double)predContext.position() == exprResult->numberValue()) { |
|
41 nodes->mark(index); |
|
42 } |
|
43 } |
|
44 else if (exprResult->booleanValue()) { |
|
45 nodes->mark(index); |
|
46 } |
|
47 ++index; |
|
48 } |
|
49 // sweep the non-marked nodes |
|
50 nodes->sweep(); |
|
51 } |
|
52 |
|
53 return NS_OK; |
|
54 } |
|
55 |
|
56 bool |
|
57 PredicateList::isSensitiveTo(Expr::ContextSensitivity aContext) |
|
58 { |
|
59 // We're creating a new node/nodeset so we can ignore those bits. |
|
60 Expr::ContextSensitivity context = |
|
61 aContext & ~(Expr::NODE_CONTEXT | Expr::NODESET_CONTEXT); |
|
62 if (context == Expr::NO_CONTEXT) { |
|
63 return false; |
|
64 } |
|
65 |
|
66 uint32_t i, len = mPredicates.Length(); |
|
67 for (i = 0; i < len; ++i) { |
|
68 if (mPredicates[i]->isSensitiveTo(context)) { |
|
69 return true; |
|
70 } |
|
71 } |
|
72 |
|
73 return false; |
|
74 } |
|
75 |
|
76 #ifdef TX_TO_STRING |
|
77 void PredicateList::toString(nsAString& dest) |
|
78 { |
|
79 for (uint32_t i = 0; i < mPredicates.Length(); ++i) { |
|
80 dest.Append(char16_t('[')); |
|
81 mPredicates[i]->toString(dest); |
|
82 dest.Append(char16_t(']')); |
|
83 } |
|
84 } |
|
85 #endif |