2005-11-02 10:42:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2005-11-02 10:34:13 +03:00
|
|
|
|
2005-11-02 20:34:14 +03:00
|
|
|
#include "txExpr.h"
|
2005-11-02 10:41:38 +03:00
|
|
|
#include "txNodeSet.h"
|
2005-11-02 10:37:50 +03:00
|
|
|
#include "txNodeSetContext.h"
|
|
|
|
#include "txSingleNodeContext.h"
|
2005-11-02 20:34:14 +03:00
|
|
|
#include "txXMLUtils.h"
|
2006-04-01 06:12:15 +04:00
|
|
|
#include "txXPathTreeWalker.h"
|
2005-11-02 10:34:13 +03:00
|
|
|
|
|
|
|
//------------/
|
|
|
|
//- PathExpr -/
|
|
|
|
//------------/
|
|
|
|
|
|
|
|
/**
|
2005-11-02 10:36:37 +03:00
|
|
|
* Adds the Expr to this PathExpr
|
2005-11-02 10:34:13 +03:00
|
|
|
* @param expr the Expr to add to this PathExpr
|
|
|
|
**/
|
2005-11-02 10:41:11 +03:00
|
|
|
nsresult
|
2006-11-10 23:31:59 +03:00
|
|
|
PathExpr::addExpr(Expr* aExpr, PathOperator aPathOp)
|
2005-11-02 10:36:37 +03:00
|
|
|
{
|
2006-11-10 23:31:59 +03:00
|
|
|
NS_ASSERTION(!mItems.IsEmpty() || aPathOp == RELATIVE_OP,
|
2005-11-02 10:36:59 +03:00
|
|
|
"First step has to be relative in PathExpr");
|
2006-11-10 23:31:59 +03:00
|
|
|
PathExprItem* pxi = mItems.AppendElement();
|
2005-11-02 10:41:11 +03:00
|
|
|
if (!pxi) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2006-11-10 23:31:59 +03:00
|
|
|
pxi->expr = aExpr;
|
|
|
|
pxi->pathOp = aPathOp;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2005-11-02 10:36:37 +03:00
|
|
|
//-----------------------------/
|
|
|
|
//- Virtual methods from Expr -/
|
|
|
|
//-----------------------------/
|
2005-11-02 10:34:13 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Evaluates this Expr based on the given context node and processor state
|
|
|
|
* @param context the context node for evaluation of this Expr
|
|
|
|
* @param ps the ContextState containing the stack information needed
|
|
|
|
* for evaluation
|
|
|
|
* @return the result of the evaluation
|
|
|
|
**/
|
2005-11-02 10:40:48 +03:00
|
|
|
nsresult
|
|
|
|
PathExpr::evaluate(txIEvalContext* aContext, txAExprResult** aResult)
|
2005-11-02 10:36:37 +03:00
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
*aResult = nullptr;
|
2005-11-02 10:34:13 +03:00
|
|
|
|
2006-01-26 04:50:30 +03:00
|
|
|
// We need to evaluate the first step with the current context since it
|
|
|
|
// can depend on the context size and position. For example:
|
|
|
|
// key('books', concat('book', position()))
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<txAExprResult> res;
|
2006-11-10 23:31:59 +03:00
|
|
|
nsresult rv = mItems[0].expr->evaluate(aContext, getter_AddRefs(res));
|
2005-11-02 10:40:48 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2005-11-02 10:34:13 +03:00
|
|
|
|
2006-01-26 04:50:30 +03:00
|
|
|
NS_ENSURE_TRUE(res->getResultType() == txAExprResult::NODESET,
|
|
|
|
NS_ERROR_XSLT_NODESET_EXPECTED);
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<txNodeSet> nodes = static_cast<txNodeSet*>
|
2007-07-08 11:08:04 +04:00
|
|
|
(static_cast<txAExprResult*>
|
|
|
|
(res));
|
2006-01-26 04:50:30 +03:00
|
|
|
if (nodes->isEmpty()) {
|
2016-07-21 01:19:00 +03:00
|
|
|
res.forget(aResult);
|
2006-01-26 04:50:30 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-07-30 18:20:58 +04:00
|
|
|
res = nullptr; // To allow recycling
|
2006-01-26 04:50:30 +03:00
|
|
|
|
|
|
|
// Evaluate remaining steps
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t i, len = mItems.Length();
|
2006-11-10 23:31:59 +03:00
|
|
|
for (i = 1; i < len; ++i) {
|
|
|
|
PathExprItem& pxi = mItems[i];
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<txNodeSet> tmpNodes;
|
2005-11-02 10:37:50 +03:00
|
|
|
txNodeSetContext eContext(nodes, aContext);
|
|
|
|
while (eContext.hasNext()) {
|
|
|
|
eContext.next();
|
2005-11-02 10:41:43 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<txNodeSet> resNodes;
|
2006-11-10 23:31:59 +03:00
|
|
|
if (pxi.pathOp == DESCENDANT_OP) {
|
2005-11-02 10:40:48 +03:00
|
|
|
rv = aContext->recycler()->getNodeSet(getter_AddRefs(resNodes));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2006-11-10 23:31:59 +03:00
|
|
|
rv = evalDescendants(pxi.expr, eContext.getContextNode(),
|
2005-11-02 10:41:43 +03:00
|
|
|
&eContext, resNodes);
|
2005-11-02 10:40:48 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2005-11-02 10:34:13 +03:00
|
|
|
}
|
|
|
|
else {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<txAExprResult> res;
|
2006-11-10 23:31:59 +03:00
|
|
|
rv = pxi.expr->evaluate(&eContext, getter_AddRefs(res));
|
2005-11-02 10:40:48 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (res->getResultType() != txAExprResult::NODESET) {
|
2005-11-02 10:36:11 +03:00
|
|
|
//XXX ErrorReport: report nonnodeset error
|
2005-11-02 10:40:48 +03:00
|
|
|
return NS_ERROR_XSLT_NODESET_EXPECTED;
|
2005-11-02 10:36:11 +03:00
|
|
|
}
|
2007-07-08 11:08:04 +04:00
|
|
|
resNodes = static_cast<txNodeSet*>
|
|
|
|
(static_cast<txAExprResult*>
|
|
|
|
(res));
|
2005-11-02 10:34:13 +03:00
|
|
|
}
|
2005-11-02 10:36:11 +03:00
|
|
|
|
2005-11-02 10:36:37 +03:00
|
|
|
if (tmpNodes) {
|
2005-11-02 10:40:48 +03:00
|
|
|
if (!resNodes->isEmpty()) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<txNodeSet> oldSet;
|
2005-11-02 10:40:48 +03:00
|
|
|
oldSet.swap(tmpNodes);
|
|
|
|
rv = aContext->recycler()->
|
|
|
|
getNonSharedNodeSet(oldSet, getter_AddRefs(tmpNodes));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2005-11-02 10:41:43 +03:00
|
|
|
oldSet.swap(resNodes);
|
|
|
|
rv = aContext->recycler()->
|
|
|
|
getNonSharedNodeSet(oldSet, getter_AddRefs(resNodes));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
tmpNodes->addAndTransfer(resNodes);
|
2005-11-02 10:40:48 +03:00
|
|
|
}
|
2005-11-02 10:36:11 +03:00
|
|
|
}
|
2005-11-02 10:40:48 +03:00
|
|
|
else {
|
2005-11-02 10:36:11 +03:00
|
|
|
tmpNodes = resNodes;
|
2005-11-02 10:40:48 +03:00
|
|
|
}
|
2005-11-02 10:34:13 +03:00
|
|
|
}
|
|
|
|
nodes = tmpNodes;
|
2005-11-02 10:40:48 +03:00
|
|
|
if (nodes->isEmpty()) {
|
|
|
|
break;
|
|
|
|
}
|
2005-11-02 10:34:13 +03:00
|
|
|
}
|
2005-11-02 10:40:48 +03:00
|
|
|
|
|
|
|
*aResult = nodes;
|
|
|
|
NS_ADDREF(*aResult);
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2005-11-02 10:40:48 +03:00
|
|
|
return NS_OK;
|
2005-11-02 10:34:13 +03:00
|
|
|
} //-- evaluate
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Selects from the descendants of the context node
|
2005-11-02 10:36:37 +03:00
|
|
|
* all nodes that match the Expr
|
2005-11-02 10:34:13 +03:00
|
|
|
**/
|
2005-11-02 10:40:48 +03:00
|
|
|
nsresult
|
2005-11-02 10:41:43 +03:00
|
|
|
PathExpr::evalDescendants(Expr* aStep, const txXPathNode& aNode,
|
|
|
|
txIMatchContext* aContext, txNodeSet* resNodes)
|
2005-11-02 10:34:13 +03:00
|
|
|
{
|
2005-11-02 10:37:50 +03:00
|
|
|
txSingleNodeContext eContext(aNode, aContext);
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<txAExprResult> res;
|
2005-11-02 10:40:48 +03:00
|
|
|
nsresult rv = aStep->evaluate(&eContext, getter_AddRefs(res));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (res->getResultType() != txAExprResult::NODESET) {
|
2005-11-02 10:36:11 +03:00
|
|
|
//XXX ErrorReport: report nonnodeset error
|
2005-11-02 10:40:48 +03:00
|
|
|
return NS_ERROR_XSLT_NODESET_EXPECTED;
|
2005-11-02 10:36:11 +03:00
|
|
|
}
|
2005-11-02 10:41:43 +03:00
|
|
|
|
2007-07-08 11:08:04 +04:00
|
|
|
txNodeSet* oldSet = static_cast<txNodeSet*>
|
|
|
|
(static_cast<txAExprResult*>(res));
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<txNodeSet> newSet;
|
2005-11-02 10:41:43 +03:00
|
|
|
rv = aContext->recycler()->getNonSharedNodeSet(oldSet,
|
|
|
|
getter_AddRefs(newSet));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
resNodes->addAndTransfer(newSet);
|
2005-11-02 10:36:11 +03:00
|
|
|
|
2017-03-22 23:05:11 +03:00
|
|
|
bool filterWS;
|
|
|
|
rv = aContext->isStripSpaceAllowed(aNode, filterWS);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2005-11-02 10:39:22 +03:00
|
|
|
|
2005-11-02 10:41:43 +03:00
|
|
|
txXPathTreeWalker walker(aNode);
|
|
|
|
if (!walker.moveToFirstChild()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
2005-11-02 10:42:49 +03:00
|
|
|
const txXPathNode& node = walker.getCurrentPosition();
|
|
|
|
if (!(filterWS && txXPathNodeUtils::isText(node) &&
|
|
|
|
txXPathNodeUtils::isWhitespace(node))) {
|
|
|
|
rv = evalDescendants(aStep, node, aContext, resNodes);
|
2005-11-02 10:40:48 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
2005-11-02 10:41:43 +03:00
|
|
|
} while (walker.moveToNextSibling());
|
|
|
|
|
2005-11-02 10:40:48 +03:00
|
|
|
return NS_OK;
|
2005-11-02 10:36:11 +03:00
|
|
|
} //-- evalDescendants
|
2005-11-02 10:34:13 +03:00
|
|
|
|
2006-11-10 23:31:59 +03:00
|
|
|
Expr::ExprType
|
|
|
|
PathExpr::getType()
|
|
|
|
{
|
|
|
|
return PATH_EXPR;
|
|
|
|
}
|
|
|
|
|
2006-01-26 04:50:30 +03:00
|
|
|
TX_IMPL_EXPR_STUBS_BASE(PathExpr, NODESET_RESULT)
|
|
|
|
|
|
|
|
Expr*
|
2012-08-22 19:56:38 +04:00
|
|
|
PathExpr::getSubExprAt(uint32_t aPos)
|
2006-01-26 04:50:30 +03:00
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
return aPos < mItems.Length() ? mItems[aPos].expr.get() : nullptr;
|
2006-01-26 04:50:30 +03:00
|
|
|
}
|
|
|
|
void
|
2012-08-22 19:56:38 +04:00
|
|
|
PathExpr::setSubExprAt(uint32_t aPos, Expr* aExpr)
|
2006-01-26 04:50:30 +03:00
|
|
|
{
|
2006-11-10 23:31:59 +03:00
|
|
|
NS_ASSERTION(aPos < mItems.Length(), "setting bad subexpression index");
|
|
|
|
mItems[aPos].expr.forget();
|
|
|
|
mItems[aPos].expr = aExpr;
|
2006-01-26 04:50:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2006-01-26 04:50:30 +03:00
|
|
|
PathExpr::isSensitiveTo(ContextSensitivity aContext)
|
|
|
|
{
|
2006-11-10 23:31:59 +03:00
|
|
|
if (mItems[0].expr->isSensitiveTo(aContext)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2006-01-26 04:50:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// We're creating a new node/nodeset so we can ignore those bits.
|
|
|
|
Expr::ContextSensitivity context =
|
|
|
|
aContext & ~(Expr::NODE_CONTEXT | Expr::NODESET_CONTEXT);
|
|
|
|
if (context == NO_CONTEXT) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2006-01-26 04:50:30 +03:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t i, len = mItems.Length();
|
2006-11-10 23:31:59 +03:00
|
|
|
for (i = 0; i < len; ++i) {
|
|
|
|
NS_ASSERTION(!mItems[i].expr->isSensitiveTo(Expr::NODESET_CONTEXT),
|
2006-01-26 04:50:30 +03:00
|
|
|
"Step cannot depend on nodeset-context");
|
2006-11-10 23:31:59 +03:00
|
|
|
if (mItems[i].expr->isSensitiveTo(context)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2006-01-26 04:50:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2006-01-26 04:50:30 +03:00
|
|
|
}
|
|
|
|
|
2005-11-02 10:42:10 +03:00
|
|
|
#ifdef TX_TO_STRING
|
|
|
|
void
|
|
|
|
PathExpr::toString(nsAString& dest)
|
2005-11-02 10:36:37 +03:00
|
|
|
{
|
2006-11-10 23:31:59 +03:00
|
|
|
if (!mItems.IsEmpty()) {
|
|
|
|
NS_ASSERTION(mItems[0].pathOp == RELATIVE_OP,
|
2005-11-02 10:36:59 +03:00
|
|
|
"First step should be relative");
|
2006-11-10 23:31:59 +03:00
|
|
|
mItems[0].expr->toString(dest);
|
2005-11-02 10:36:59 +03:00
|
|
|
}
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t i, len = mItems.Length();
|
2006-11-10 23:31:59 +03:00
|
|
|
for (i = 1; i < len; ++i) {
|
|
|
|
switch (mItems[i].pathOp) {
|
2005-11-02 10:36:59 +03:00
|
|
|
case DESCENDANT_OP:
|
2005-11-02 10:42:33 +03:00
|
|
|
dest.AppendLiteral("//");
|
2005-11-02 10:34:13 +03:00
|
|
|
break;
|
2005-11-02 10:36:59 +03:00
|
|
|
case RELATIVE_OP:
|
2014-01-04 19:02:17 +04:00
|
|
|
dest.Append(char16_t('/'));
|
2005-11-02 10:34:13 +03:00
|
|
|
break;
|
|
|
|
}
|
2006-11-10 23:31:59 +03:00
|
|
|
mItems[i].expr->toString(dest);
|
2005-11-02 10:34:13 +03:00
|
|
|
}
|
2005-11-02 10:42:10 +03:00
|
|
|
}
|
|
|
|
#endif
|