forked from youtube/cobalt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.cc
76 lines (66 loc) · 2.48 KB
/
search.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Copyright 2015 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "cobalt/webdriver/search.h"
namespace cobalt {
namespace webdriver {
template <>
util::CommandResult<protocol::ElementId> Search::PopulateFindResults(
const ElementVector& found_elements, ElementMapping* element_mapping) {
typedef util::CommandResult<protocol::ElementId> CommandResult;
// Return NoSuchElement if there are no results.
if (found_elements.empty()) {
return CommandResult(protocol::Response::kNoSuchElement);
}
// Grab the first result from the list and return it.
protocol::ElementId id =
element_mapping->ElementToId(found_elements.front().get());
return CommandResult(id);
}
template <>
util::CommandResult<std::vector<protocol::ElementId> >
Search::PopulateFindResults(const ElementVector& found_elements,
ElementMapping* element_mapping) {
typedef util::CommandResult<ElementIdVector> CommandResult;
// Create a new ElementDriver for each result and return it.
ElementIdVector id_vector;
for (int i = 0; i < found_elements.size(); ++i) {
protocol::ElementId id =
element_mapping->ElementToId(found_elements[i].get());
id_vector.push_back(id);
}
return CommandResult(id_vector);
}
void Search::HTMLCollectionToElementVector(
const scoped_refptr<dom::HTMLCollection>& html_collection,
ElementVector* element_vector) {
if (html_collection) {
for (uint32 i = 0; i < html_collection->length(); ++i) {
element_vector->push_back(html_collection->Item(i).get());
}
}
}
void Search::NodeListToElementVector(
const scoped_refptr<dom::NodeList>& node_list,
ElementVector* element_vector) {
if (node_list) {
for (uint32 i = 0; i < node_list->length(); ++i) {
scoped_refptr<dom::Element> element = node_list->Item(i)->AsElement();
if (element) {
element_vector->push_back(element.get());
}
}
}
}
} // namespace webdriver
} // namespace cobalt