dom/xslt/xpath/txLocationStep.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/xslt/xpath/txLocationStep.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,325 @@
     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 +/*
    1.10 +  Implementation of an XPath LocationStep
    1.11 +*/
    1.12 +
    1.13 +#include "txExpr.h"
    1.14 +#include "txIXPathContext.h"
    1.15 +#include "txNodeSet.h"
    1.16 +#include "txXPathTreeWalker.h"
    1.17 +
    1.18 +  //-----------------------------/
    1.19 + //- Virtual methods from Expr -/
    1.20 +//-----------------------------/
    1.21 +
    1.22 +/**
    1.23 + * Evaluates this Expr based on the given context node and processor state
    1.24 + * @param context the context node for evaluation of this Expr
    1.25 + * @param ps the ProcessorState containing the stack information needed
    1.26 + * for evaluation
    1.27 + * @return the result of the evaluation
    1.28 + * @see Expr
    1.29 +**/
    1.30 +nsresult
    1.31 +LocationStep::evaluate(txIEvalContext* aContext, txAExprResult** aResult)
    1.32 +{
    1.33 +    NS_ASSERTION(aContext, "internal error");
    1.34 +    *aResult = nullptr;
    1.35 +
    1.36 +    nsRefPtr<txNodeSet> nodes;
    1.37 +    nsresult rv = aContext->recycler()->getNodeSet(getter_AddRefs(nodes));
    1.38 +    NS_ENSURE_SUCCESS(rv, rv);
    1.39 +
    1.40 +    txXPathTreeWalker walker(aContext->getContextNode());
    1.41 +
    1.42 +    switch (mAxisIdentifier) {
    1.43 +        case ANCESTOR_AXIS:
    1.44 +        {
    1.45 +            if (!walker.moveToParent()) {
    1.46 +                break;
    1.47 +            }
    1.48 +            // do not break here
    1.49 +        }
    1.50 +        case ANCESTOR_OR_SELF_AXIS:
    1.51 +        {
    1.52 +            nodes->setReverse();
    1.53 +
    1.54 +            do {
    1.55 +                if (mNodeTest->matches(walker.getCurrentPosition(), aContext)) {
    1.56 +                    nodes->append(walker.getCurrentPosition());
    1.57 +                }
    1.58 +            } while (walker.moveToParent());
    1.59 +
    1.60 +            break;
    1.61 +        }
    1.62 +        case ATTRIBUTE_AXIS:
    1.63 +        {
    1.64 +            if (!walker.moveToFirstAttribute()) {
    1.65 +                break;
    1.66 +            }
    1.67 +
    1.68 +            do {
    1.69 +                if (mNodeTest->matches(walker.getCurrentPosition(), aContext)) {
    1.70 +                    nodes->append(walker.getCurrentPosition());
    1.71 +                }
    1.72 +            } while (walker.moveToNextAttribute());
    1.73 +            break;
    1.74 +        }
    1.75 +        case DESCENDANT_OR_SELF_AXIS:
    1.76 +        {
    1.77 +            if (mNodeTest->matches(walker.getCurrentPosition(), aContext)) {
    1.78 +                nodes->append(walker.getCurrentPosition());
    1.79 +            }
    1.80 +            // do not break here
    1.81 +        }
    1.82 +        case DESCENDANT_AXIS:
    1.83 +        {
    1.84 +            fromDescendants(walker.getCurrentPosition(), aContext, nodes);
    1.85 +            break;
    1.86 +        }
    1.87 +        case FOLLOWING_AXIS:
    1.88 +        {
    1.89 +            if (txXPathNodeUtils::isAttribute(walker.getCurrentPosition())) {
    1.90 +                walker.moveToParent();
    1.91 +                fromDescendants(walker.getCurrentPosition(), aContext, nodes);
    1.92 +            }
    1.93 +            bool cont = true;
    1.94 +            while (!walker.moveToNextSibling()) {
    1.95 +                if (!walker.moveToParent()) {
    1.96 +                    cont = false;
    1.97 +                    break;
    1.98 +                }
    1.99 +            }
   1.100 +            while (cont) {
   1.101 +                if (mNodeTest->matches(walker.getCurrentPosition(), aContext)) {
   1.102 +                    nodes->append(walker.getCurrentPosition());
   1.103 +                }
   1.104 +
   1.105 +                fromDescendants(walker.getCurrentPosition(), aContext, nodes);
   1.106 +
   1.107 +                while (!walker.moveToNextSibling()) {
   1.108 +                    if (!walker.moveToParent()) {
   1.109 +                        cont = false;
   1.110 +                        break;
   1.111 +                    }
   1.112 +                }
   1.113 +            }
   1.114 +            break;
   1.115 +        }
   1.116 +        case FOLLOWING_SIBLING_AXIS:
   1.117 +        {
   1.118 +            while (walker.moveToNextSibling()) {
   1.119 +                if (mNodeTest->matches(walker.getCurrentPosition(), aContext)) {
   1.120 +                    nodes->append(walker.getCurrentPosition());
   1.121 +                }
   1.122 +            }
   1.123 +            break;
   1.124 +        }
   1.125 +        case NAMESPACE_AXIS: //-- not yet implemented
   1.126 +#if 0
   1.127 +            // XXX DEBUG OUTPUT
   1.128 +            cout << "namespace axis not yet implemented"<<endl;
   1.129 +#endif
   1.130 +            break;
   1.131 +        case PARENT_AXIS :
   1.132 +        {
   1.133 +            if (walker.moveToParent() &&
   1.134 +                mNodeTest->matches(walker.getCurrentPosition(), aContext)) {
   1.135 +                nodes->append(walker.getCurrentPosition());
   1.136 +            }
   1.137 +            break;
   1.138 +        }
   1.139 +        case PRECEDING_AXIS:
   1.140 +        {
   1.141 +            nodes->setReverse();
   1.142 +
   1.143 +            bool cont = true;
   1.144 +            while (!walker.moveToPreviousSibling()) {
   1.145 +                if (!walker.moveToParent()) {
   1.146 +                    cont = false;
   1.147 +                    break;
   1.148 +                }
   1.149 +            }
   1.150 +            while (cont) {
   1.151 +                fromDescendantsRev(walker.getCurrentPosition(), aContext, nodes);
   1.152 +
   1.153 +                if (mNodeTest->matches(walker.getCurrentPosition(), aContext)) {
   1.154 +                    nodes->append(walker.getCurrentPosition());
   1.155 +                }
   1.156 +
   1.157 +                while (!walker.moveToPreviousSibling()) {
   1.158 +                    if (!walker.moveToParent()) {
   1.159 +                        cont = false;
   1.160 +                        break;
   1.161 +                    }
   1.162 +                }
   1.163 +            }
   1.164 +            break;
   1.165 +        }
   1.166 +        case PRECEDING_SIBLING_AXIS:
   1.167 +        {
   1.168 +            nodes->setReverse();
   1.169 +
   1.170 +            while (walker.moveToPreviousSibling()) {
   1.171 +                if (mNodeTest->matches(walker.getCurrentPosition(), aContext)) {
   1.172 +                    nodes->append(walker.getCurrentPosition());
   1.173 +                }
   1.174 +            }
   1.175 +            break;
   1.176 +        }
   1.177 +        case SELF_AXIS:
   1.178 +        {
   1.179 +            if (mNodeTest->matches(walker.getCurrentPosition(), aContext)) {
   1.180 +                nodes->append(walker.getCurrentPosition());
   1.181 +            }
   1.182 +            break;
   1.183 +        }
   1.184 +        default: // Children Axis
   1.185 +        {
   1.186 +            if (!walker.moveToFirstChild()) {
   1.187 +                break;
   1.188 +            }
   1.189 +
   1.190 +            do {
   1.191 +                if (mNodeTest->matches(walker.getCurrentPosition(), aContext)) {
   1.192 +                    nodes->append(walker.getCurrentPosition());
   1.193 +                }
   1.194 +            } while (walker.moveToNextSibling());
   1.195 +            break;
   1.196 +        }
   1.197 +    }
   1.198 +
   1.199 +    // Apply predicates
   1.200 +    if (!isEmpty()) {
   1.201 +        rv = evaluatePredicates(nodes, aContext);
   1.202 +        NS_ENSURE_SUCCESS(rv, rv);
   1.203 +    }
   1.204 +
   1.205 +    nodes->unsetReverse();
   1.206 +
   1.207 +    NS_ADDREF(*aResult = nodes);
   1.208 +
   1.209 +    return NS_OK;
   1.210 +}
   1.211 +
   1.212 +void LocationStep::fromDescendants(const txXPathNode& aNode,
   1.213 +                                   txIMatchContext* aCs,
   1.214 +                                   txNodeSet* aNodes)
   1.215 +{
   1.216 +    txXPathTreeWalker walker(aNode);
   1.217 +    if (!walker.moveToFirstChild()) {
   1.218 +        return;
   1.219 +    }
   1.220 +
   1.221 +    do {
   1.222 +        const txXPathNode& child = walker.getCurrentPosition();
   1.223 +        if (mNodeTest->matches(child, aCs)) {
   1.224 +            aNodes->append(child);
   1.225 +        }
   1.226 +        fromDescendants(child, aCs, aNodes);
   1.227 +    } while (walker.moveToNextSibling());
   1.228 +}
   1.229 +
   1.230 +void LocationStep::fromDescendantsRev(const txXPathNode& aNode,
   1.231 +                                      txIMatchContext* aCs,
   1.232 +                                      txNodeSet* aNodes)
   1.233 +{
   1.234 +    txXPathTreeWalker walker(aNode);
   1.235 +    if (!walker.moveToLastChild()) {
   1.236 +        return;
   1.237 +    }
   1.238 +
   1.239 +    do {
   1.240 +        const txXPathNode& child = walker.getCurrentPosition();
   1.241 +        fromDescendantsRev(child, aCs, aNodes);
   1.242 +
   1.243 +        if (mNodeTest->matches(child, aCs)) {
   1.244 +            aNodes->append(child);
   1.245 +        }
   1.246 +
   1.247 +    } while (walker.moveToPreviousSibling());
   1.248 +}
   1.249 +
   1.250 +Expr::ExprType
   1.251 +LocationStep::getType()
   1.252 +{
   1.253 +  return LOCATIONSTEP_EXPR;
   1.254 +}
   1.255 +
   1.256 +
   1.257 +TX_IMPL_EXPR_STUBS_BASE(LocationStep, NODESET_RESULT)
   1.258 +
   1.259 +Expr*
   1.260 +LocationStep::getSubExprAt(uint32_t aPos)
   1.261 +{
   1.262 +    return PredicateList::getSubExprAt(aPos);
   1.263 +}
   1.264 +
   1.265 +void
   1.266 +LocationStep::setSubExprAt(uint32_t aPos, Expr* aExpr)
   1.267 +{
   1.268 +    PredicateList::setSubExprAt(aPos, aExpr);
   1.269 +}
   1.270 +
   1.271 +bool
   1.272 +LocationStep::isSensitiveTo(ContextSensitivity aContext)
   1.273 +{
   1.274 +    return (aContext & NODE_CONTEXT) ||
   1.275 +           mNodeTest->isSensitiveTo(aContext) ||
   1.276 +           PredicateList::isSensitiveTo(aContext);
   1.277 +}
   1.278 +
   1.279 +#ifdef TX_TO_STRING
   1.280 +void
   1.281 +LocationStep::toString(nsAString& str)
   1.282 +{
   1.283 +    switch (mAxisIdentifier) {
   1.284 +        case ANCESTOR_AXIS :
   1.285 +            str.AppendLiteral("ancestor::");
   1.286 +            break;
   1.287 +        case ANCESTOR_OR_SELF_AXIS :
   1.288 +            str.AppendLiteral("ancestor-or-self::");
   1.289 +            break;
   1.290 +        case ATTRIBUTE_AXIS:
   1.291 +            str.Append(char16_t('@'));
   1.292 +            break;
   1.293 +        case DESCENDANT_AXIS:
   1.294 +            str.AppendLiteral("descendant::");
   1.295 +            break;
   1.296 +        case DESCENDANT_OR_SELF_AXIS:
   1.297 +            str.AppendLiteral("descendant-or-self::");
   1.298 +            break;
   1.299 +        case FOLLOWING_AXIS :
   1.300 +            str.AppendLiteral("following::");
   1.301 +            break;
   1.302 +        case FOLLOWING_SIBLING_AXIS:
   1.303 +            str.AppendLiteral("following-sibling::");
   1.304 +            break;
   1.305 +        case NAMESPACE_AXIS:
   1.306 +            str.AppendLiteral("namespace::");
   1.307 +            break;
   1.308 +        case PARENT_AXIS :
   1.309 +            str.AppendLiteral("parent::");
   1.310 +            break;
   1.311 +        case PRECEDING_AXIS :
   1.312 +            str.AppendLiteral("preceding::");
   1.313 +            break;
   1.314 +        case PRECEDING_SIBLING_AXIS :
   1.315 +            str.AppendLiteral("preceding-sibling::");
   1.316 +            break;
   1.317 +        case SELF_AXIS :
   1.318 +            str.AppendLiteral("self::");
   1.319 +            break;
   1.320 +        default:
   1.321 +            break;
   1.322 +    }
   1.323 +    NS_ASSERTION(mNodeTest, "mNodeTest is null, that's verboten");
   1.324 +    mNodeTest->toString(str);
   1.325 +
   1.326 +    PredicateList::toString(str);
   1.327 +}
   1.328 +#endif

mercurial