#include "config.h"
#include "XPathPath.h"
#if XPATH_SUPPORT
#include "Document.h"
#include "XPathPredicate.h"
#include "XPathStep.h"
#include "XPathValue.h"
namespace WebCore {
namespace XPath {
Filter::Filter(Expression* expr, const Vector<Predicate*>& predicates)
: m_expr(expr), m_predicates(predicates)
{
}
Filter::~Filter()
{
delete m_expr;
deleteAllValues(m_predicates);
}
Value Filter::doEvaluate() const
{
Value v = m_expr->evaluate();
if (!v.isNodeVector())
return v;
NodeVector inNodes = v.toNodeVector(), outNodes;
for (unsigned i = 0; i < m_predicates.size(); i++) {
outNodes.clear();
Expression::evaluationContext().size = inNodes.size();
Expression::evaluationContext().position = 0;
for (unsigned j = 0; j < inNodes.size(); j++) {
Node* node = inNodes[j].get();
Expression::evaluationContext().node = node;
++Expression::evaluationContext().position;
if (m_predicates[i]->evaluate())
outNodes.append(node);
}
inNodes = outNodes;
}
return outNodes;
}
LocationPath::LocationPath()
: m_absolute(false)
{
}
LocationPath::~LocationPath()
{
deleteAllValues(m_steps);
}
void LocationPath::optimize()
{
for (unsigned i = 0; i < m_steps.size(); i++)
m_steps[i]->optimize();
}
Value LocationPath::doEvaluate() const
{
NodeVector inDomNodes, outDomNodes;
Node* context = Expression::evaluationContext().node.get();
if (m_absolute && context->nodeType() != Node::DOCUMENT_NODE)
context = context->ownerDocument();
inDomNodes.append(context);
for (unsigned i = 0; i < m_steps.size(); i++) {
Step* step = m_steps[i];
for (unsigned j = 0; j < inDomNodes.size(); j++) {
NodeVector matches = step->evaluate(inDomNodes[j].get());
outDomNodes.append(matches);
}
inDomNodes = outDomNodes;
outDomNodes.clear();
}
return inDomNodes;
}
Path::Path(Filter* filter, LocationPath* path)
: m_filter(filter),
m_path(path)
{
}
Path::~Path()
{
delete m_filter;
delete m_path;
}
Value Path::doEvaluate() const
{
return Value();
}
}
}
#endif // XPATH_SUPPORT