This source file includes following definitions.
- createExpression
- evaluate
#include "config.h"
#include "core/xml/XPathExpression.h"
#include "bindings/v8/ExceptionState.h"
#include "core/dom/ExceptionCode.h"
#include "core/xml/XPathExpressionNode.h"
#include "core/xml/XPathNSResolver.h"
#include "core/xml/XPathParser.h"
#include "core/xml/XPathResult.h"
#include "core/xml/XPathUtil.h"
#include "wtf/text/WTFString.h"
namespace WebCore {
using namespace XPath;
PassRefPtrWillBeRawPtr<XPathExpression> XPathExpression::createExpression(const String& expression, PassRefPtrWillBeRawPtr<XPathNSResolver> resolver, ExceptionState& exceptionState)
{
RefPtrWillBeRawPtr<XPathExpression> expr = XPathExpression::create();
Parser parser;
expr->m_topExpression = parser.parseStatement(expression, resolver, exceptionState);
if (!expr->m_topExpression)
return nullptr;
return expr.release();
}
XPathExpression::~XPathExpression()
{
delete m_topExpression;
}
PassRefPtrWillBeRawPtr<XPathResult> XPathExpression::evaluate(Node* contextNode, unsigned short type, XPathResult*, ExceptionState& exceptionState)
{
if (!contextNode) {
exceptionState.throwDOMException(NotSupportedError, "The context node provided is null.");
return nullptr;
}
if (!isValidContextNode(contextNode)) {
exceptionState.throwDOMException(NotSupportedError, "The node provided is '" + contextNode->nodeName() + "', which is not a valid context node type.");
return nullptr;
}
EvaluationContext& evaluationContext = Expression::evaluationContext();
evaluationContext.node = contextNode;
evaluationContext.size = 1;
evaluationContext.position = 1;
evaluationContext.hadTypeConversionError = false;
RefPtrWillBeRawPtr<XPathResult> result = XPathResult::create(&contextNode->document(), m_topExpression->evaluate());
evaluationContext.node = nullptr;
if (evaluationContext.hadTypeConversionError) {
exceptionState.throwDOMException(SyntaxError, "Type conversion failed while evaluating the expression.");
return nullptr;
}
if (type != XPathResult::ANY_TYPE) {
result->convertTo(type, exceptionState);
if (exceptionState.hadException())
return nullptr;
}
return result;
}
}