// Licensed to the Software Freedom Conservancy (SFC) under one1// or more contributor license agreements. See the NOTICE file2// distributed with this work for additional information3// regarding copyright ownership. The SFC licenses this file4// to you under the Apache License, Version 2.0 (the5// "License"); you may not use this file except in compliance6// with the License. You may obtain a copy of the License at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing,11// software distributed under the License is distributed on an12// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13// KIND, either express or implied. See the License for the14// specific language governing permissions and limitations15// under the License.1617// TODO: Add support for using sizzle to locate elements1819goog.provide('bot.locators.css');2021goog.require('bot.Error');22goog.require('bot.ErrorCode');23goog.require('bot.userAgent');24goog.require('goog.dom.NodeType');25goog.require('goog.string');26goog.require('goog.userAgent');27goog.require('goog.utils');282930/**31* Find an element by using a CSS selector32*33* @param {string} target The selector to search for.34* @param {!(Document|Element)} root The document or element to perform the35* search under.36* @return {Element} The first matching element found in the DOM, or null if no37* such element could be found.38*/39bot.locators.css.single = function (target, root) {40if (typeof root['querySelector'] !== 'function' &&41// IE8 in non-compatibility mode reports querySelector as an object.42goog.userAgent.IE && bot.userAgent.isEngineVersion(8) &&43!goog.utils.isObject(root['querySelector'])) {44throw Error('CSS selection is not supported');45}4647if (!target) {48throw new bot.Error(bot.ErrorCode.INVALID_SELECTOR_ERROR,49'No selector specified');50}5152target = goog.string.trim(target);5354var element;55try {56element = root.querySelector(target);57} catch (e) {58throw new bot.Error(bot.ErrorCode.INVALID_SELECTOR_ERROR,59'An invalid or illegal selector was specified');60}6162return element && element.nodeType == goog.dom.NodeType.ELEMENT ?63/**@type {Element}*/ (element) : null;64};656667/**68* Find all elements matching a CSS selector.69*70* @param {string} target The selector to search for.71* @param {!(Document|Element)} root The document or element to perform the72* search under.73* @return {!IArrayLike} All matching elements, or an empty list.74*/75bot.locators.css.many = function (target, root) {76if (typeof root['querySelectorAll'] !== 'function' &&77// IE8 in non-compatibility mode reports querySelector as an object.78goog.userAgent.IE && bot.userAgent.isEngineVersion(8) &&79!goog.utils.isObject(root['querySelector'])) {80throw Error('CSS selection is not supported');81}8283if (!target) {84throw new bot.Error(bot.ErrorCode.INVALID_SELECTOR_ERROR,85'No selector specified');86}8788target = goog.string.trim(target);8990try {91return root.querySelectorAll(target);92} catch (e) {93throw new bot.Error(bot.ErrorCode.INVALID_SELECTOR_ERROR,94'An invalid or illegal selector was specified');95}96};979899