dom/xslt/xpath/txPredicateList.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial