1 /*
2 * Copyright (C) 2005 Frerich Raabe <raabe@kde.org>
3 * Copyright (C) 2006, 2009 Apple Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 #include "third_party/blink/renderer/core/xml/xpath_expression.h"
28
29 #include "third_party/blink/renderer/core/xml/xpath_expression_node.h"
30 #include "third_party/blink/renderer/core/xml/xpath_ns_resolver.h"
31 #include "third_party/blink/renderer/core/xml/xpath_parser.h"
32 #include "third_party/blink/renderer/core/xml/xpath_result.h"
33 #include "third_party/blink/renderer/core/xml/xpath_util.h"
34 #include "third_party/blink/renderer/platform/bindings/exception_state.h"
35 #include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
36
37 namespace blink {
38
39 XPathExpression::XPathExpression() = default;
40
CreateExpression(const String & expression,XPathNSResolver * resolver,ExceptionState & exception_state)41 XPathExpression* XPathExpression::CreateExpression(
42 const String& expression,
43 XPathNSResolver* resolver,
44 ExceptionState& exception_state) {
45 auto* expr = MakeGarbageCollected<XPathExpression>();
46 xpath::Parser parser;
47
48 expr->top_expression_ =
49 parser.ParseStatement(expression, resolver, exception_state);
50 if (!expr->top_expression_)
51 return nullptr;
52
53 return expr;
54 }
55
Trace(Visitor * visitor)56 void XPathExpression::Trace(Visitor* visitor) {
57 visitor->Trace(top_expression_);
58 ScriptWrappable::Trace(visitor);
59 }
60
evaluate(Node * context_node,uint16_t type,const ScriptValue &,ExceptionState & exception_state)61 XPathResult* XPathExpression::evaluate(Node* context_node,
62 uint16_t type,
63 const ScriptValue&,
64 ExceptionState& exception_state) {
65 if (!xpath::IsValidContextNode(context_node)) {
66 exception_state.ThrowDOMException(
67 DOMExceptionCode::kNotSupportedError,
68 "The node provided is '" + context_node->nodeName() +
69 "', which is not a valid context node type.");
70 return nullptr;
71 }
72
73 bool had_type_conversion_error = false;
74 xpath::EvaluationContext evaluation_context(*context_node,
75 had_type_conversion_error);
76 auto* result = MakeGarbageCollected<XPathResult>(
77 evaluation_context, top_expression_->Evaluate(evaluation_context));
78
79 if (had_type_conversion_error) {
80 // It is not specified what to do if type conversion fails while evaluating
81 // an expression.
82 exception_state.ThrowDOMException(
83 DOMExceptionCode::kSyntaxError,
84 "Type conversion failed while evaluating the expression.");
85 return nullptr;
86 }
87
88 if (type != XPathResult::kAnyType) {
89 result->ConvertTo(type, exception_state);
90 if (exception_state.HadException())
91 return nullptr;
92 }
93
94 return result;
95 }
96
97 } // namespace blink
98