Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/javascript/atoms/locators/link_text.js
3991 views
1
// Licensed to the Software Freedom Conservancy (SFC) under one
2
// or more contributor license agreements. See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership. The SFC licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License. You may obtain a copy of the License at
8
//
9
// http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied. See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
goog.provide('bot.locators.linkText');
19
goog.provide('bot.locators.partialLinkText');
20
21
goog.require('bot');
22
goog.require('bot.dom');
23
goog.require('bot.locators.css');
24
goog.require('goog.array');
25
goog.require('goog.dom');
26
goog.require('goog.dom.TagName');
27
28
29
/**
30
* Find an element by using the text value of a link
31
* @param {string} target The link text to search for.
32
* @param {!(Document|Element)} root The document or element to perform the
33
* search under.
34
* @param {boolean=} opt_isPartial Whether the link text needs to be matched
35
* only partially.
36
* @return {Element} The first matching element found in the DOM, or null if no
37
* such element could be found.
38
* @private
39
*/
40
bot.locators.linkText.single_ = function (target, root, opt_isPartial) {
41
var elements;
42
try {
43
elements = bot.locators.css.many('a', root);
44
} catch (e) {
45
// Old versions of browsers don't support CSS. They won't have XHTML
46
// support. Sorry.
47
elements = goog.dom.getDomHelper(root).getElementsByTagNameAndClass(
48
goog.dom.TagName.A, /*className=*/null, root);
49
}
50
51
var element = goog.array.find(elements, function (element) {
52
var text = bot.dom.getVisibleText(element);
53
// getVisibleText replaces non-breaking spaces with plain
54
// spaces, so if these are present at the beginning or end
55
// of the link text, we need to trim the regular spaces off
56
// to be spec compliant for matching on link text.
57
text = text.replace(/^[\s]+|[\s]+$/g, '');
58
return (opt_isPartial && text.indexOf(target) != -1) || text == target;
59
});
60
return /**@type{Element}*/ (element);
61
};
62
63
64
/**
65
* Find many elements by using the value of the link text
66
* @param {string} target The link text to search for.
67
* @param {!(Document|Element)} root The document or element to perform the
68
* search under.
69
* @param {boolean=} opt_isPartial Whether the link text needs to be matched
70
* only partially.
71
* @return {!IArrayLike} All matching elements, or an empty list.
72
* @private
73
*/
74
bot.locators.linkText.many_ = function (target, root, opt_isPartial) {
75
var elements;
76
try {
77
elements = bot.locators.css.many('a', root);
78
} catch (e) {
79
// Old versions of browsers don't support CSS. They won't have XHTML
80
// support. Sorry.
81
elements = goog.dom.getDomHelper(root).getElementsByTagNameAndClass(
82
goog.dom.TagName.A, /*className=*/null, root);
83
}
84
85
return goog.array.filter(elements, function (element) {
86
var text = bot.dom.getVisibleText(element);
87
// getVisibleText replaces non-breaking spaces with plain
88
// spaces, so if these are present at the beginning or end
89
// of the link text, we need to trim the regular spaces off
90
// to be spec compliant for matching on link text.
91
text = text.replace(/^[\s]+|[\s]+$/g, '');
92
return (opt_isPartial && text.indexOf(target) != -1) || text == target;
93
});
94
};
95
96
97
/**
98
* Find an element by using the text value of a link
99
* @param {string} target The link text to search for.
100
* @param {!(Document|Element)} root The document or element to perform the
101
* search under.
102
* @return {Element} The first matching element found in the DOM, or null if no
103
* such element could be found.
104
*/
105
bot.locators.linkText.single = function (target, root) {
106
return bot.locators.linkText.single_(target, root, false);
107
};
108
109
110
/**
111
* Find many elements by using the value of the link text
112
* @param {string} target The link text to search for.
113
* @param {!(Document|Element)} root The document or element to perform the
114
* search under.
115
* @return {IArrayLike} All matching elements, or an empty list.
116
*/
117
bot.locators.linkText.many = function (target, root) {
118
return bot.locators.linkText.many_(target, root, false);
119
};
120
121
122
/**
123
* Find an element by using part of the text value of a link.
124
* @param {string} target The link text to search for.
125
* @param {!(Document|Element)} root The document or element to perform the
126
* search under.
127
* @return {Element} The first matching element found in the DOM, or null if no
128
* such element could be found.
129
*/
130
bot.locators.partialLinkText.single = function (target, root) {
131
return bot.locators.linkText.single_(target, root, true);
132
};
133
134
135
/**
136
* Find many elements by using part of the value of the link text.
137
* @param {string} target The link text to search for.
138
* @param {!(Document|Element)} root The document or element to perform the
139
* search under.
140
* @return {IArrayLike} All matching elements, or an empty list.
141
*/
142
bot.locators.partialLinkText.many = function (target, root) {
143
return bot.locators.linkText.many_(target, root, true);
144
};
145
146