/src/mozilla-central/dom/xslt/xpath/txPredicateList.cpp
Line | Count | Source (jump to first uncovered line) |
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 | 0 | { |
19 | 0 | NS_ASSERTION(nodes, "called evaluatePredicates with nullptr NodeSet"); |
20 | 0 | nsresult rv = NS_OK; |
21 | 0 |
|
22 | 0 | uint32_t i, len = mPredicates.Length(); |
23 | 0 | for (i = 0; i < len && !nodes->isEmpty(); ++i) { |
24 | 0 | txNodeSetContext predContext(nodes, aContext); |
25 | 0 | /* |
26 | 0 | * add nodes to newNodes that match the expression |
27 | 0 | * or, if the result is a number, add the node with the right |
28 | 0 | * position |
29 | 0 | */ |
30 | 0 | int32_t index = 0; |
31 | 0 | while (predContext.hasNext()) { |
32 | 0 | predContext.next(); |
33 | 0 | RefPtr<txAExprResult> exprResult; |
34 | 0 | rv = mPredicates[i]->evaluate(&predContext, |
35 | 0 | getter_AddRefs(exprResult)); |
36 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
37 | 0 |
|
38 | 0 | // handle default, [position() == numberValue()] |
39 | 0 | if (exprResult->getResultType() == txAExprResult::NUMBER) { |
40 | 0 | if ((double)predContext.position() == exprResult->numberValue()) { |
41 | 0 | nodes->mark(index); |
42 | 0 | } |
43 | 0 | } |
44 | 0 | else if (exprResult->booleanValue()) { |
45 | 0 | nodes->mark(index); |
46 | 0 | } |
47 | 0 | ++index; |
48 | 0 | } |
49 | 0 | // sweep the non-marked nodes |
50 | 0 | nodes->sweep(); |
51 | 0 | } |
52 | 0 |
|
53 | 0 | return NS_OK; |
54 | 0 | } |
55 | | |
56 | | bool |
57 | | PredicateList::isSensitiveTo(Expr::ContextSensitivity aContext) |
58 | 0 | { |
59 | 0 | // We're creating a new node/nodeset so we can ignore those bits. |
60 | 0 | Expr::ContextSensitivity context = |
61 | 0 | aContext & ~(Expr::NODE_CONTEXT | Expr::NODESET_CONTEXT); |
62 | 0 | if (context == Expr::NO_CONTEXT) { |
63 | 0 | return false; |
64 | 0 | } |
65 | 0 | |
66 | 0 | uint32_t i, len = mPredicates.Length(); |
67 | 0 | for (i = 0; i < len; ++i) { |
68 | 0 | if (mPredicates[i]->isSensitiveTo(context)) { |
69 | 0 | return true; |
70 | 0 | } |
71 | 0 | } |
72 | 0 |
|
73 | 0 | return false; |
74 | 0 | } |
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 |