Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/atoms/locators/link_text.js
2884 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
27
28
/**
29
* Find an element by using the text value of a link
30
* @param {string} target The link text to search for.
31
* @param {!(Document|Element)} root The document or element to perform the
32
* search under.
33
* @param {boolean=} opt_isPartial Whether the link text needs to be matched
34
* only partially.
35
* @return {Element} The first matching element found in the DOM, or null if no
36
* such element could be found.
37
* @private
38
*/
39
bot.locators.linkText.single_ = function (target, root, opt_isPartial) {
40
var elements;
41
try {
42
elements = bot.locators.css.many('a', root);
43
} catch (e) {
44
// Old versions of browsers don't support CSS. They won't have XHTML
45
// support. Sorry.
46
elements = goog.dom.getDomHelper(root).getElementsByTagNameAndClass(
47
goog.dom.TagName.A, /*className=*/null, root);
48
}
49
50
var element = goog.array.find(elements, function (element) {
51
var text = bot.dom.getVisibleText(element);
52
// getVisibleText replaces non-breaking spaces with plain
53
// spaces, so if these are present at the beginning or end
54
// of the link text, we need to trim the regular spaces off
55
// to be spec compliant for matching on link text.
56
text = text.replace(/^[\s]+|[\s]+$/g, '');
57
return (opt_isPartial && text.indexOf(target) != -1) || text == target;
58
});
59
return /**@type{Element}*/ (element);
60
};
61
62
63
/**
64
* Find many elements by using the value of the link text
65
* @param {string} target The link text to search for.
66
* @param {!(Document|Element)} root The document or element to perform the
67
* search under.
68
* @param {boolean=} opt_isPartial Whether the link text needs to be matched
69
* only partially.
70
* @return {!IArrayLike} All matching elements, or an empty list.
71
* @private
72
*/
73
bot.locators.linkText.many_ = function (target, root, opt_isPartial) {
74
var elements;
75
try {
76
elements = bot.locators.css.many('a', root);
77
} catch (e) {
78
// Old versions of browsers don't support CSS. They won't have XHTML
79
// support. Sorry.
80
elements = goog.dom.getDomHelper(root).getElementsByTagNameAndClass(
81
goog.dom.TagName.A, /*className=*/null, root);
82
}
83
84
return goog.array.filter(elements, function (element) {
85
var text = bot.dom.getVisibleText(element);
86
// getVisibleText replaces non-breaking spaces with plain
87
// spaces, so if these are present at the beginning or end
88
// of the link text, we need to trim the regular spaces off
89
// to be spec compliant for matching on link text.
90
text = text.replace(/^[\s]+|[\s]+$/g, '');
91
return (opt_isPartial && text.indexOf(target) != -1) || text == target;
92
});
93
};
94
95
96
/**
97
* Find an element by using the text value of a link
98
* @param {string} target The link text to search for.
99
* @param {!(Document|Element)} root The document or element to perform the
100
* search under.
101
* @return {Element} The first matching element found in the DOM, or null if no
102
* such element could be found.
103
*/
104
bot.locators.linkText.single = function (target, root) {
105
return bot.locators.linkText.single_(target, root, false);
106
};
107
108
109
/**
110
* Find many elements by using the value of the link text
111
* @param {string} target The link text to search for.
112
* @param {!(Document|Element)} root The document or element to perform the
113
* search under.
114
* @return {IArrayLike} All matching elements, or an empty list.
115
*/
116
bot.locators.linkText.many = function (target, root) {
117
return bot.locators.linkText.many_(target, root, false);
118
};
119
120
121
/**
122
* Find an element by using part of the text value of a link.
123
* @param {string} target The link text to search for.
124
* @param {!(Document|Element)} root The document or element to perform the
125
* search under.
126
* @return {Element} The first matching element found in the DOM, or null if no
127
* such element could be found.
128
*/
129
bot.locators.partialLinkText.single = function (target, root) {
130
return bot.locators.linkText.single_(target, root, true);
131
};
132
133
134
/**
135
* Find many elements by using part of the value of the link text.
136
* @param {string} target The link text to search for.
137
* @param {!(Document|Element)} root The document or element to perform the
138
* search under.
139
* @return {IArrayLike} All matching elements, or an empty list.
140
*/
141
bot.locators.partialLinkText.many = function (target, root) {
142
return bot.locators.linkText.many_(target, root, true);
143
};
144
145