Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/atoms/locators/id.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.id');
19
20
goog.require('bot.dom');
21
goog.require('goog.array');
22
goog.require('goog.dom');
23
24
25
/**
26
* Tests whether the standardized W3C Selectors API are available on an
27
* element and the target locator meets CSS requirements.
28
* @param {!(Document|Element)} root The document or element to test for CSS
29
* selector support.
30
* @param {string} target The id to search for.
31
* @return {boolean} Whether or not the root supports query selector APIs.
32
* @see http://www.w3.org/TR/selectors-api/
33
* @private
34
*/
35
bot.locators.id.canUseQuerySelector_ = function (root, target) {
36
return !!(root.querySelectorAll && root.querySelector) && !/^\d.*/.test(target);
37
};
38
39
40
/**
41
* Find an element by using the value of the ID attribute.
42
* @param {string} target The id to search for.
43
* @param {!(Document|Element)} root The document or element to perform the
44
* search under.
45
* @return {Element} The first matching element found in the DOM, or null if no
46
* such element could be found.
47
*/
48
bot.locators.id.single = function (target, root) {
49
var dom = goog.dom.getDomHelper(root);
50
51
var e = dom.getElement(target);
52
if (!e) {
53
return null;
54
}
55
56
// On IE getting by ID returns the first match by id _or_ name.
57
if (bot.dom.getAttribute(e, 'id') == target &&
58
root != e && goog.dom.contains(root, e)) {
59
return e;
60
}
61
62
var elements = dom.getElementsByTagNameAndClass('*');
63
var element = goog.array.find(elements, function (element) {
64
return bot.dom.getAttribute(element, 'id') == target &&
65
root != element && goog.dom.contains(root, element);
66
});
67
return /**@type{Element}*/ (element);
68
};
69
70
71
/**
72
* Find many elements by using the value of the ID attribute.
73
* @param {string} target The id to search for.
74
* @param {!(Document|Element)} root The document or element to perform the
75
* search under.
76
* @return {!IArrayLike} All matching elements, or an empty list.
77
*/
78
bot.locators.id.many = function (target, root) {
79
if (!target) {
80
return [];
81
}
82
if (bot.locators.id.canUseQuerySelector_(root, target)) {
83
try {
84
// Need to escape the ID for use in a CSS selector.
85
return root.querySelectorAll('#' + bot.locators.id.cssEscape_(target));
86
} catch (e) {
87
return [];
88
}
89
}
90
var dom = goog.dom.getDomHelper(root);
91
var elements = dom.getElementsByTagNameAndClass('*', null, root);
92
return goog.array.filter(elements, function (e) {
93
return bot.dom.getAttribute(e, 'id') == target;
94
});
95
};
96
97
/**
98
* Given a string, escapes all the characters that have special meaning in CSS.
99
* https://mathiasbynens.be/notes/css-escapes
100
*
101
* An ID can contain anything but spaces, but we also escape whitespace because
102
* some webpages use spaces, and getElementById allows spaces in every browser.
103
* http://www.w3.org/TR/html5/dom.html#the-id-attribute
104
*
105
* This could be further improved, perhaps by using
106
* http://dev.w3.org/csswg/cssom/#the-css.escape()-method , where implemented,
107
* or a polyfill such as https://github.com/mathiasbynens/CSS.escape.
108
*
109
* @param {!string} s String to escape CSS meaningful characters in.
110
* @return {!string} Escaped string.
111
* @private
112
*/
113
bot.locators.id.cssEscape_ = function (s) {
114
// One backslash escapes things in a regex statement; we need two in a string.
115
return s.replace(/([\s'"\\#.:;,!?+<>=~*^$|%&@`{}\-\/\[\]\(\)])/g, '\\$1');
116
};
117
118