tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

txPredicateList.cpp (2347B)


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