michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "txExpr.h" michael@0: #include "txNodeSet.h" michael@0: #include "txNodeSetContext.h" michael@0: michael@0: /* michael@0: * Represents an ordered list of Predicates, michael@0: * for use with Step and Filter Expressions michael@0: */ michael@0: michael@0: nsresult michael@0: PredicateList::evaluatePredicates(txNodeSet* nodes, michael@0: txIMatchContext* aContext) michael@0: { michael@0: NS_ASSERTION(nodes, "called evaluatePredicates with nullptr NodeSet"); michael@0: nsresult rv = NS_OK; michael@0: michael@0: uint32_t i, len = mPredicates.Length(); michael@0: for (i = 0; i < len && !nodes->isEmpty(); ++i) { michael@0: txNodeSetContext predContext(nodes, aContext); michael@0: /* michael@0: * add nodes to newNodes that match the expression michael@0: * or, if the result is a number, add the node with the right michael@0: * position michael@0: */ michael@0: int32_t index = 0; michael@0: while (predContext.hasNext()) { michael@0: predContext.next(); michael@0: nsRefPtr exprResult; michael@0: rv = mPredicates[i]->evaluate(&predContext, michael@0: getter_AddRefs(exprResult)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: // handle default, [position() == numberValue()] michael@0: if (exprResult->getResultType() == txAExprResult::NUMBER) { michael@0: if ((double)predContext.position() == exprResult->numberValue()) { michael@0: nodes->mark(index); michael@0: } michael@0: } michael@0: else if (exprResult->booleanValue()) { michael@0: nodes->mark(index); michael@0: } michael@0: ++index; michael@0: } michael@0: // sweep the non-marked nodes michael@0: nodes->sweep(); michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: bool michael@0: PredicateList::isSensitiveTo(Expr::ContextSensitivity aContext) michael@0: { michael@0: // We're creating a new node/nodeset so we can ignore those bits. michael@0: Expr::ContextSensitivity context = michael@0: aContext & ~(Expr::NODE_CONTEXT | Expr::NODESET_CONTEXT); michael@0: if (context == Expr::NO_CONTEXT) { michael@0: return false; michael@0: } michael@0: michael@0: uint32_t i, len = mPredicates.Length(); michael@0: for (i = 0; i < len; ++i) { michael@0: if (mPredicates[i]->isSensitiveTo(context)) { michael@0: return true; michael@0: } michael@0: } michael@0: michael@0: return false; michael@0: } michael@0: michael@0: #ifdef TX_TO_STRING michael@0: void PredicateList::toString(nsAString& dest) michael@0: { michael@0: for (uint32_t i = 0; i < mPredicates.Length(); ++i) { michael@0: dest.Append(char16_t('[')); michael@0: mPredicates[i]->toString(dest); michael@0: dest.Append(char16_t(']')); michael@0: } michael@0: } michael@0: #endif