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(*contextNode);
    RefPtrWillBeRawPtr<XPathResult> result = XPathResult::create(evaluationContext, m_topExpression->evaluate(evaluationContext));

    if (evaluationContext.hadTypeConversionError) {
        // It is not specified what to do if type conversion fails while evaluating an expression.
        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;
}
Example #2
0
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; // Do not hold a reference to the context node, as this may prevent the whole document from being destroyed in time.

    if (evaluationContext.hadTypeConversionError) {
        // It is not specified what to do if type conversion fails while evaluating an expression.
        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;
}