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_result.h"
28 
29 #include "third_party/blink/renderer/core/dom/document.h"
30 #include "third_party/blink/renderer/core/xml/xpath_evaluator.h"
31 #include "third_party/blink/renderer/core/xml/xpath_expression_node.h"
32 #include "third_party/blink/renderer/platform/bindings/exception_state.h"
33 
34 namespace blink {
35 
XPathResult(xpath::EvaluationContext & context,const xpath::Value & value)36 XPathResult::XPathResult(xpath::EvaluationContext& context,
37                          const xpath::Value& value)
38     : value_(value), node_set_position_(0), dom_tree_version_(0) {
39   switch (value_.GetType()) {
40     case xpath::Value::kBooleanValue:
41       result_type_ = kBooleanType;
42       return;
43     case xpath::Value::kNumberValue:
44       result_type_ = kNumberType;
45       return;
46     case xpath::Value::kStringValue:
47       result_type_ = kStringType;
48       return;
49     case xpath::Value::kNodeSetValue:
50       result_type_ = kUnorderedNodeIteratorType;
51       node_set_position_ = 0;
52       node_set_ = xpath::NodeSet::Create(value_.ToNodeSet(&context));
53       document_ = &context.node->GetDocument();
54       dom_tree_version_ = document_->DomTreeVersion();
55       return;
56   }
57   NOTREACHED();
58 }
59 
Trace(Visitor * visitor)60 void XPathResult::Trace(Visitor* visitor) {
61   visitor->Trace(value_);
62   visitor->Trace(node_set_);
63   visitor->Trace(document_);
64   ScriptWrappable::Trace(visitor);
65 }
66 
ConvertTo(uint16_t type,ExceptionState & exception_state)67 void XPathResult::ConvertTo(uint16_t type, ExceptionState& exception_state) {
68   switch (type) {
69     case kAnyType:
70       break;
71     case kNumberType:
72       result_type_ = type;
73       value_ = value_.ToNumber();
74       break;
75     case kStringType:
76       result_type_ = type;
77       value_ = value_.ToString();
78       break;
79     case kBooleanType:
80       result_type_ = type;
81       value_ = value_.ToBoolean();
82       break;
83     case kUnorderedNodeIteratorType:
84     case kUnorderedNodeSnapshotType:
85     case kAnyUnorderedNodeType:
86     // This is correct - singleNodeValue() will take care of ordering.
87     case kFirstOrderedNodeType:
88       if (!value_.IsNodeSet()) {
89         exception_state.ThrowTypeError(
90             "The result is not a node set, and therefore cannot be converted "
91             "to the desired type.");
92         return;
93       }
94       result_type_ = type;
95       break;
96     case kOrderedNodeIteratorType:
97       if (!value_.IsNodeSet()) {
98         exception_state.ThrowTypeError(
99             "The result is not a node set, and therefore cannot be converted "
100             "to the desired type.");
101         return;
102       }
103       GetNodeSet().Sort();
104       result_type_ = type;
105       break;
106     case kOrderedNodeSnapshotType:
107       if (!value_.IsNodeSet()) {
108         exception_state.ThrowTypeError(
109             "The result is not a node set, and therefore cannot be converted "
110             "to the desired type.");
111         return;
112       }
113       value_.ToNodeSet(nullptr).Sort();
114       result_type_ = type;
115       break;
116   }
117 }
118 
resultType() const119 uint16_t XPathResult::resultType() const {
120   return result_type_;
121 }
122 
numberValue(ExceptionState & exception_state) const123 double XPathResult::numberValue(ExceptionState& exception_state) const {
124   if (resultType() != kNumberType) {
125     exception_state.ThrowTypeError("The result type is not a number.");
126     return 0.0;
127   }
128   return value_.ToNumber();
129 }
130 
stringValue(ExceptionState & exception_state) const131 String XPathResult::stringValue(ExceptionState& exception_state) const {
132   if (resultType() != kStringType) {
133     exception_state.ThrowTypeError("The result type is not a string.");
134     return String();
135   }
136   return value_.ToString();
137 }
138 
booleanValue(ExceptionState & exception_state) const139 bool XPathResult::booleanValue(ExceptionState& exception_state) const {
140   if (resultType() != kBooleanType) {
141     exception_state.ThrowTypeError("The result type is not a boolean.");
142     return false;
143   }
144   return value_.ToBoolean();
145 }
146 
singleNodeValue(ExceptionState & exception_state) const147 Node* XPathResult::singleNodeValue(ExceptionState& exception_state) const {
148   if (resultType() != kAnyUnorderedNodeType &&
149       resultType() != kFirstOrderedNodeType) {
150     exception_state.ThrowTypeError("The result type is not a single node.");
151     return nullptr;
152   }
153 
154   const xpath::NodeSet& nodes = value_.ToNodeSet(nullptr);
155   if (resultType() == kFirstOrderedNodeType)
156     return nodes.FirstNode();
157   return nodes.AnyNode();
158 }
159 
invalidIteratorState() const160 bool XPathResult::invalidIteratorState() const {
161   if (resultType() != kUnorderedNodeIteratorType &&
162       resultType() != kOrderedNodeIteratorType)
163     return false;
164 
165   DCHECK(document_);
166   return document_->DomTreeVersion() != dom_tree_version_;
167 }
168 
snapshotLength(ExceptionState & exception_state) const169 unsigned XPathResult::snapshotLength(ExceptionState& exception_state) const {
170   if (resultType() != kUnorderedNodeSnapshotType &&
171       resultType() != kOrderedNodeSnapshotType) {
172     exception_state.ThrowTypeError("The result type is not a snapshot.");
173     return 0;
174   }
175 
176   return value_.ToNodeSet(nullptr).size();
177 }
178 
iterateNext(ExceptionState & exception_state)179 Node* XPathResult::iterateNext(ExceptionState& exception_state) {
180   if (resultType() != kUnorderedNodeIteratorType &&
181       resultType() != kOrderedNodeIteratorType) {
182     exception_state.ThrowTypeError("The result type is not an iterator.");
183     return nullptr;
184   }
185 
186   if (invalidIteratorState()) {
187     exception_state.ThrowDOMException(
188         DOMExceptionCode::kInvalidStateError,
189         "The document has mutated since the result was returned.");
190     return nullptr;
191   }
192 
193   if (node_set_position_ + 1 > GetNodeSet().size())
194     return nullptr;
195 
196   Node* node = GetNodeSet()[node_set_position_];
197 
198   node_set_position_++;
199 
200   return node;
201 }
202 
snapshotItem(unsigned index,ExceptionState & exception_state)203 Node* XPathResult::snapshotItem(unsigned index,
204                                 ExceptionState& exception_state) {
205   if (resultType() != kUnorderedNodeSnapshotType &&
206       resultType() != kOrderedNodeSnapshotType) {
207     exception_state.ThrowTypeError("The result type is not a snapshot.");
208     return nullptr;
209   }
210 
211   const xpath::NodeSet& nodes = value_.ToNodeSet(nullptr);
212   if (index >= nodes.size())
213     return nullptr;
214 
215   return nodes[index];
216 }
217 
218 }  // namespace blink
219