1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/xslt/xpath/txPredicateList.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,85 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "txExpr.h" 1.10 +#include "txNodeSet.h" 1.11 +#include "txNodeSetContext.h" 1.12 + 1.13 +/* 1.14 + * Represents an ordered list of Predicates, 1.15 + * for use with Step and Filter Expressions 1.16 + */ 1.17 + 1.18 +nsresult 1.19 +PredicateList::evaluatePredicates(txNodeSet* nodes, 1.20 + txIMatchContext* aContext) 1.21 +{ 1.22 + NS_ASSERTION(nodes, "called evaluatePredicates with nullptr NodeSet"); 1.23 + nsresult rv = NS_OK; 1.24 + 1.25 + uint32_t i, len = mPredicates.Length(); 1.26 + for (i = 0; i < len && !nodes->isEmpty(); ++i) { 1.27 + txNodeSetContext predContext(nodes, aContext); 1.28 + /* 1.29 + * add nodes to newNodes that match the expression 1.30 + * or, if the result is a number, add the node with the right 1.31 + * position 1.32 + */ 1.33 + int32_t index = 0; 1.34 + while (predContext.hasNext()) { 1.35 + predContext.next(); 1.36 + nsRefPtr<txAExprResult> exprResult; 1.37 + rv = mPredicates[i]->evaluate(&predContext, 1.38 + getter_AddRefs(exprResult)); 1.39 + NS_ENSURE_SUCCESS(rv, rv); 1.40 + 1.41 + // handle default, [position() == numberValue()] 1.42 + if (exprResult->getResultType() == txAExprResult::NUMBER) { 1.43 + if ((double)predContext.position() == exprResult->numberValue()) { 1.44 + nodes->mark(index); 1.45 + } 1.46 + } 1.47 + else if (exprResult->booleanValue()) { 1.48 + nodes->mark(index); 1.49 + } 1.50 + ++index; 1.51 + } 1.52 + // sweep the non-marked nodes 1.53 + nodes->sweep(); 1.54 + } 1.55 + 1.56 + return NS_OK; 1.57 +} 1.58 + 1.59 +bool 1.60 +PredicateList::isSensitiveTo(Expr::ContextSensitivity aContext) 1.61 +{ 1.62 + // We're creating a new node/nodeset so we can ignore those bits. 1.63 + Expr::ContextSensitivity context = 1.64 + aContext & ~(Expr::NODE_CONTEXT | Expr::NODESET_CONTEXT); 1.65 + if (context == Expr::NO_CONTEXT) { 1.66 + return false; 1.67 + } 1.68 + 1.69 + uint32_t i, len = mPredicates.Length(); 1.70 + for (i = 0; i < len; ++i) { 1.71 + if (mPredicates[i]->isSensitiveTo(context)) { 1.72 + return true; 1.73 + } 1.74 + } 1.75 + 1.76 + return false; 1.77 +} 1.78 + 1.79 +#ifdef TX_TO_STRING 1.80 +void PredicateList::toString(nsAString& dest) 1.81 +{ 1.82 + for (uint32_t i = 0; i < mPredicates.Length(); ++i) { 1.83 + dest.Append(char16_t('[')); 1.84 + mPredicates[i]->toString(dest); 1.85 + dest.Append(char16_t(']')); 1.86 + } 1.87 +} 1.88 +#endif