// 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');272829/**30* Find an element by using a CSS selector31*32* @param {string} target The selector to search for.33* @param {!(Document|Element)} root The document or element to perform the34* search under.35* @return {Element} The first matching element found in the DOM, or null if no36* such element could be found.37*/38bot.locators.css.single = function (target, root) {39if (!goog.isFunction(root['querySelector']) &&40// IE8 in non-compatibility mode reports querySelector as an object.41goog.userAgent.IE && bot.userAgent.isEngineVersion(8) &&42!goog.isObject(root['querySelector'])) {43throw Error('CSS selection is not supported');44}4546if (!target) {47throw new bot.Error(bot.ErrorCode.INVALID_SELECTOR_ERROR,48'No selector specified');49}5051target = goog.string.trim(target);5253var element;54try {55element = root.querySelector(target);56} catch (e) {57throw new bot.Error(bot.ErrorCode.INVALID_SELECTOR_ERROR,58'An invalid or illegal selector was specified');59}6061return element && element.nodeType == goog.dom.NodeType.ELEMENT ?62/**@type {Element}*/ (element) : null;63};646566/**67* Find all elements matching a CSS selector.68*69* @param {string} target The selector to search for.70* @param {!(Document|Element)} root The document or element to perform the71* search under.72* @return {!IArrayLike} All matching elements, or an empty list.73*/74bot.locators.css.many = function (target, root) {75if (!goog.isFunction(root['querySelectorAll']) &&76// IE8 in non-compatibility mode reports querySelector as an object.77goog.userAgent.IE && bot.userAgent.isEngineVersion(8) &&78!goog.isObject(root['querySelector'])) {79throw Error('CSS selection is not supported');80}8182if (!target) {83throw new bot.Error(bot.ErrorCode.INVALID_SELECTOR_ERROR,84'No selector specified');85}8687target = goog.string.trim(target);8889try {90return root.querySelectorAll(target);91} catch (e) {92throw new bot.Error(bot.ErrorCode.INVALID_SELECTOR_ERROR,93'An invalid or illegal selector was specified');94}95};969798