XPathExpression.cpp [plain text]
#include "config.h"
#include "XPathExpression.h"
#if ENABLE(XPATH)
#include "Document.h"
#include "ExceptionCode.h"
#include "Node.h"
#include "PlatformString.h"
#include "XPathExpressionNode.h"
#include "XPathNSResolver.h"
#include "XPathParser.h"
#include "XPathResult.h"
#include "XPathUtil.h"
namespace WebCore {
using namespace XPath;
PassRefPtr<XPathExpression> XPathExpression::createExpression(const String& expression, XPathNSResolver* resolver, ExceptionCode& ec)
{
RefPtr<XPathExpression> expr = XPathExpression::create();
Parser parser;
expr->m_topExpression = parser.parseStatement(expression, resolver, ec);
if (!expr->m_topExpression)
return 0;
return expr.release();
}
XPathExpression::~XPathExpression()
{
delete m_topExpression;
}
PassRefPtr<XPathResult> XPathExpression::evaluate(Node* contextNode, unsigned short type, XPathResult*, ExceptionCode& ec)
{
if (!isValidContextNode(contextNode)) {
ec = NOT_SUPPORTED_ERR;
return 0;
}
EventTargetNode* eventTarget = contextNode->ownerDocument()
? contextNode->ownerDocument()
: static_cast<EventTargetNode*>(contextNode);
EvaluationContext& evaluationContext = Expression::evaluationContext();
evaluationContext.node = contextNode;
evaluationContext.size = 1;
evaluationContext.position = 1;
RefPtr<XPathResult> result = XPathResult::create(eventTarget, m_topExpression->evaluate());
evaluationContext.node = 0;
if (type != XPathResult::ANY_TYPE) {
ec = 0;
result->convertTo(type, ec);
if (ec)
return 0;
}
return result;
}
}
#endif // ENABLE(XPATH)